# compute confidence interval for indepedent t test

I'm actually reading Andy Feld book and i'm trying to better understand how statistics works and especially t test.

I have two independent groups and i would like to compute 95% confidence interval in order to plot it as error bar on a bar plot graphic in R.

First would like to now my formula is correct. I have computed the standard error for each of my group and, as each group have a size of 12, i think i should refer to a student distribution (with a df of 11) in order to compute the lower and the upper bound and multiply this value with the SE. Is it right?

@BruceET thank you very much for your long and detailed comment :)

Maybe my problem wasn't clear enough. I already made a Welsh t-test to compare my two mean. The t test indicates that there is no significant difference between them.

    Welch Two Sample t-test

data:  Anxiety by Group
t = -1.7612, df = 21.246, p-value = 0.0926
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-16.168163   1.334829
sample estimates:
mean in group Picture mean in group Real Spiders
40.00000                   47.41667


What i want to do, is to plot the data and compute the 95% confidence interval as error bar. As you mantioned it, standard error of the mean seems to be a bad way to do it, so i would like to take good habits. :)

So, i want to know wich confidence interval i should plot. For me (but maybe i'm wrong, tis what i want to know), it doesn't make sense to plot the interval given by the Welsh T-test as it is the confidence interval for the distrbution of the mean of the difference between the two groups.

If i understand correclty, i should compute the confidence interval from the distribution of the mean for each of my group.

So for me it is something like this :

lower_bound_real <- - 1.7612 * sd(Real) / sqrt(length(Real))
upper_bound_real <- + 1.7612 * sd(Real) / sqrt(length(Real))

lower_bound_picture <- - 1.7612 * sd(Picture) / sqrt(length(Picture))
lower_bound_picture <- + 1.7612 * sd(Picture) / sqrt(length(Picture))


Is it right?

Also, if it is, i would loke to know how to plot a different error bar for each og the group on a same plot.

Concerning the "Notches", i'm not sure what it is? Is this the "error bar" of the boxplot? Also, it tell smothing about the difference of the medians, but not the means right?

Thank you one again

• Whose CI and SE? CI and SE of means, different of means or t-value? – user158565 Jul 21 at 17:43

If you are comparing two independent samples, then you need a two-sample t test. Here are descriptive statistics for two samples of size 12, sampled in R from normal populations.

set.seed(123)  # for reproducibility
x1 = round(rnorm(12, 70, 3),2)
x2 = round(rnorm(12, 74, 3),2)
summary(x1); sd(x1)
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
66.20   68.58   70.30   70.58   71.95   75.15
[1] 2.777619
summary(x2); sd(x2)
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
68.10   71.59   72.97   73.36   75.27   79.36
[1] 2.963682


Boxplots illustrate the sample values overlap, but perhaps not by enough to keep $$\bar X_1 = 70.58$$ from being significantly different from $$\bar X_2 = 73.36.$$ (Sample means are shown as solid red dots.)

boxplot(x1, x2, col="skyblue2")
points(1:2, c(mean(x1), mean(x2)), pch=19, col="red")


If you want to test the null hypothesis $$H_0: \mu_1 = \mu_2$$ that the two population means are the same against the alternative $$H_a: \mu_1 \ne \mu_2$$ that they are not, you might use a pooled 2-sample t test. The P-value of this test is $$0.02684 < 0.05$$ so the difference between the sample means is significant at the 5% level. Notice that the confidence interval for the difference in means does not include $$0$$ (its two endpoints have the same sign).

t.test(x1, x2, var.eq=T)

Two Sample t-test

data:  x1 and x2
t = -2.3723, df = 22, p-value = 0.02684
alternative hypothesis:
true difference in means is not equal to 0
95 percent confidence interval:
-5.213393 -0.349940
sample estimates:
mean of x mean of y
70.58250  73.36417


The $$T$$-statistic is computed as $$T = (\bar X_1 - \bar X_2)/SE,$$ where $$SE = S_p\sqrt{1/12 + 1/12},$$ and the pooled variance estimate $$S_p^2 = \frac{(n_1-1)S_1^2 + (n_2-1)S_2^2}{n_1 + n_2 - 2} = (S_1^2 + S_2^2)/2$$ combines information from the two sample standard deviations. (The second, simplified expression for $$S_p^2$$ is possible because this is a 'balanced design' with the two sample sizes equal.) This pooled estimate is based on the assumption that the two population variances are equal: $$\sigma_1^2 = \sigma_2^2 = \sigma^2,$$ so that $$S_p^2$$ estimates $$\sigma^2.$$

However, the standard error SE would not necessarily be appropriate for making error bars in a bar graph. There is some disagreement how best to illustrate statistical significance in a bar graph. The relevant Wikipedia page, although very brief, makes a couple of suggestions.

I know that such error-bar methods are often used, but overlap of error bars can occur even when means are significantly different. I wonder whether any error-bar method will be a useful graphical description of your data.

Notes: (1) For larger datasets than yours, one graphical method for showing significant differences between samples is a 'notched' boxplot. Notches (in the sides of boxes) are nonparametric confidence intervals, calibrated to facilitate comparing two samples. If the notches don't overlap, the two sample medians differ significantly at the 5% level.

Especially for small sample sizes, the notches can extend beyond the boxes, giving a result that may be confusing. When notched boxplots are available, they are often superior to barcharts for describing numerical samples.

Here is a notched-boxplot comparison of two samples, each of size 30.

set.seed(123)
x1 = round(rnorm(30, 70, 3),2)
x2 = round(rnorm(30, 74, 3),2)
boxplot(x1, x2, col="skyblue2", notch=T)


(2) The pooled 2-sample t test assumes that the two samples are from populations with equal variances. In practice it is often difficult to know whether variances are equal. The Welch 2-sample t test uses a t statistic that has approximately Student's t distribution, but typically with a reduced number of degrees of freedom.

Absent solid prior knowledge that population variances are equal, it is good practice to use the Welch test as the default two-sample t test.

Here are results from the Welch test using the original two samples $$(n_1 = n_2 = 12).$$ Notice the absence of the parameter var.eq=T in t.test, and in the output, df = 21.908, instead of df = 22 in the pooled test shown above. (The reduction in degrees of freedom is very slight here because we simulated data from populations with equal variances.)

t.test(x1, x2)

Welch Two Sample t-test

data:  x1 and x2
t = -2.3723, df = 21.908, p-value = 0.02688
alternative hypothesis:
true difference in means is not equal to 0
95 percent confidence interval:
-5.2139845 -0.3493488
sample estimates:
mean of x mean of y
70.58250  73.36417