Roshan Singh
Roshan Singh

Reputation: 45

how do I calculate t.-statistic value and find two-sided P value using R?

Below is my dataset

dput(ex0112)

Dataset:

structure(list(BP = c(8L, 12L, 10L, 14L, 2L, 0L, 0L, -6L, 0L, 
1L, 2L, -3L, -4L, 2L), Diet = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("FishOil", "RegularOil"
), class = "factor")), class = "data.frame", row.names = c(NA, 
-14L))

To find the t-statistic for entire (column BP) I have achieved it using the below R code.

library(Sleuth3)
t.test(BP~Diet, data=ex0112)

But how do I calculate For the hypothesis that mu is zero and construct the t -statistic for (column BP) for only Regular Oil Diet and also how to Find the two-sided p-value as the proportion of values from a t-distribution farther from 0 than this value using R?

Upvotes: 0

Views: 606

Answers (1)

Duck
Duck

Reputation: 39585

I would suggest next approach. You can use one variable in t.test() which has the options for mu parameter and the two-sided alternative you want. Here the code using your dput() data as df:

#Test
test <- t.test(df$BP[df$Diet=='RegularOil'], mu = 0, alternative = "two.sided")
test
#Extract p-value
test$p.value

Output:

One Sample t-test

data:  df$BP[df$Diet == "RegularOil"]
t = -0.94943, df = 6, p-value = 0.3791
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
 -4.088292  1.802578
sample estimates:
mean of x 
-1.142857 

And p-val:

[1] 0.3790617

Upvotes: 1

Related Questions