Home  /  Resources & Support  /  Introduction to Stata basics  /  Two-sample t-tests calculator

Sometimes, we would like to test the equivalence of two sample means, but we do not have the raw data. We can use summary statistics to calculate a two-sample t test using Stata's ttesti command followed by six numbers:

The number of observations in group 1 (20)
The mean in group 1 (20)
The standard deviation in group 1 (5)
The number of observations in group 2 (32)
The mean in group 2 (15)
The standard deviation in group 2 (4)

. ttesti 20 20 5 32 15 4

Two-sample t test with equal variances
Obs Mean Std. err. Std. dev. [95% conf. interval]
x 20 20 1.118034 5 17.65993 22.34007
y 32 15 .7071068 4 13.55785 16.44215
Combined 52 16.92308 .6943785 5.007235 15.52905 18.3171
diff 5 1.256135 2.476979 7.523021
diff = mean(x) - mean(y) t = 3.9805 H0: diff = 0 Degrees of freedom = 50 Ha: diff < 0 Ha: diff != 0 Ha: diff > 0 Pr(T < t) = 0.9999 Pr(|T| > |t|) = 0.0002 Pr(T > t) = 0.0001

The output gives us a lot of information.

Group 1 is labeled "x", and group 2 is labeled "y". The top of the output echoes the number of observations, mean, and standard deviation in both groups and estimates the standard error and 95% confidence interval for both groups.

The output also reports the same information for the combined groups as well as the difference between the two groups.

The bottom section of the output displays the results of the t test. The t statistic is 3.98 with 50 degrees of freedom. The null hypothesis is that there is no difference between the sample means. The output displays three possible alternative hypotheses. The p-value of 0.9999 on the bottom left is for the one-sided alternative hypothesis that the difference between the means is less than zero. The p-value of 0.0002 in the bottom middle is for the two-sided alternative hypothesis that the difference between the means is not equal to zero. The p-value of 0.0001 on the bottom right is for the one-sided alternative hypothesis that the difference between the means is greater than zero.

There are several options that may be useful under different circumstances. You can use the unequal option to conduct a t test assuming unequal variances in the two groups using Satterthwaite's (1946) approximation formula for the degrees of freedom.

. ttesti 20 20 5 32 15 4, unequal

Two-sample t test with unequal variances
Obs Mean Std. err. Std. dev. [95% conf. interval]
x 20 20 1.118034 5 17.65993 22.34007
y 32 15 .7071068 4 13.55785 16.44215
Combined 52 16.92308 .6943785 5.007235 15.52905 18.3171
diff 5 1.322876 2.311343 7.688657
diff = mean(x) - mean(y) t = 3.7796 H0: diff = 0 Satterthwaite's degrees of freedom = 33.9142 Ha: diff < 0 Ha: diff != 0 Ha: diff > 0 Pr(T < t) = 0.9997 Pr(|T| > |t|) = 0.0006 Pr(T > t) = 0.0003

You can use the welch option to conduct a t test assuming unequal variances in the two groups using Welch's (1947) approximation formula for the degrees of freedom.

. ttesti 20 20 5 32 15 4, welch

Two-sample t test with unequal variances
Obs Mean Std. err. Std. dev. [95% conf. interval]
x 20 20 1.118034 5 17.65993 22.34007
y 32 15 .7071068 4 13.55785 16.44215
Combined 52 16.92308 .6943785 5.007235 15.52905 18.3171
diff 5 1.322876 2.315387 7.684613
diff = mean(x) - mean(y) t = 3.7796 H0: diff = 0 Welch's degrees of freedom = 35.3564 Ha: diff < 0 Ha: diff != 0 Ha: diff > 0 Pr(T < t) = 0.9997 Pr(|T| > |t|) = 0.0006 Pr(T > t) = 0.0003

You can use the level() option to change the level of the confidence interval.

. ttesti 20 20 5 32 15 4, level(90)

Two-sample t test with unequal variances
Obs Mean Std. err. Std. dev. [95% conf. interval]
x 20 20 1.118034 5 18.06677 21.93323
y 32 15 .7071068 4 13.80109 16.19891
Combined 52 16.92308 .6943785 5.007235 15.7598 18.08636
diff 5 1.256135 2.894837 7.105163
diff = mean(x) - mean(y) t = 3.9805 H0: diff = 0 Degrees of freedom = 50 Ha: diff < 0 Ha: diff != 0 Ha: diff > 0 Pr(T < t) = 0.9999 Pr(|T| > |t|) = 0.0002 Pr(T > t) = 0.0001

You can watch a demonstration of these commands by clicking on the link to the YouTube video below. You can read more about these commands by clicking on the links to the Stata manual entries below.

See it in action

Watch Two-sample t-tests calculator.

Tell me more

Read more in the Stata Base Reference Manual; see [R] ttest.