I want to calculate p value and confidence interval (CI) both manually and by R.
this is my data set:
ex0112 = (
BP Diet
1 8 FishOil
2 13 FishOil
3 10 FishOil
4 14 FishOil
5 2 FishOil
6 1 FishOil
7 0 FishOil
8 -6 RegularOil
9 1 RegularOil
10 1 RegularOil
11 2 RegularOil
12 -3 RegularOil
13 -4 RegularOil
14 3 RegularOil)
Manually:
(from the book statistical sleuth)
1- Compute the sample standard deviations for each group separately. (Let's call them $s_1$ and $s_2$.)
s1 <- sd(ex0112[ex0112$Diet == "FishOil",]$BP)
s2 <- sd(ex0112[ex0112$Diet == "RegularOil",]$BP)
2- Compute the pooled estimate of standard deviation using the formula $$s_p = \sqrt{\frac{(n_1-1)s_1^2 + (n_2-1)s_2^2}{n_1 + n_2 - 2}}$$ where $n_1$ and $n_2$ group sizes.
n1 <- nrow(ex0112[ex0112$Diet == "FishOil",])
n2 <- nrow(ex0112[ex0112$Diet == "RegularOil",])
sp <- sqrt(((n1-1)*(s1^2)+(n2-1)*(s2^2))/(n1+n2-2))
- Compute $\text{SE} (\overline{Y_2} - \overline{Y_1})$ using the formula: $$\text{SE} (\overline{Y_2} - \overline{Y_1}) = s_p \sqrt{\frac{1}{n_1} + \frac{1}{n_2} }$$
se <- sp*sqrt((1/n1)+(1/n2))
- Compute 97.5th percentile of the t-distribution with this many degrees of freedom ($t_{df} (.975)$)
df <- n2+n1-2
t_df <- qt(.975, df)
- Construct a 95 % confidence interval fo $\mu_2 - \mu_1$ using the formula in Section 2.3.3: $$\left( \overline{Y_2} - \overline{Y_1} \right) \pm t_{df} (1-\alpha/2) \text{SE}( \overline{Y_2} - \overline{Y_1} ).$$
y1 <- mean(ex0112[ex0112$Diet == "FishOil",]$BP)
y2 <- mean(ex0112[ex0112$Diet == "RegularOil",]$BP)
LCI <- (y2-y1) - (t_df * se)
RCI <- (y2-y1) + (t_df * se)
- Compute the t-statistic for testing equality: $$\left( \overline{Y_2} - \overline{Y_1} \right) / \text{SE}( \overline{Y_2} - \overline{Y_1} ).$$
t <- (y2-y1)/se
- Find the one-sided p-value (for the alternative hypothesis $\mu_2 - \mu_1 > 0$) by comparing the t-statistics in (f) to the percentiles of the appropriate t-distribution (by reading the appropriate percentile from R.)
p <- pt(t, df)
Results:
LCI = -13.29674
RCI = -2.131835
p = 0.00542278
Computer t-test:
t.test(BP ~ Diet, data = ex0112, alternative = "greater",
var.equal = TRUE, conf.level = 0.95)
Results
Two Sample t-test
data: BP by Diet
t = 3.0109, df = 12, p-value = 0.01085
alternative hypothesis: true difference in means between group FishOil and group RegularOil is not equal to 0
95 percent confidence interval:
2.131835 13.296737
sample estimates:
mean in group FishOil mean in group RegularOil
6.8571429 -0.8571429
As you see, the absolute values of CI are the same using either method; and so are the p-values.
I found this, which seems to ask the same question, but it seems they use another formula to compute t.