ttest: compare means between two groups
Estimate a difference in group means, quantify its uncertainty, and separate statistical evidence from causal interpretation.
How to run a t-test in Stata-compatible syntax
The example compares math scores between the two values of female in a 24-row synthetic teaching dataset.
clear
import delimited "data/student_scores.csv"
ttest math, by(female)
With two independent groups, the default command estimates their means and tests the null hypothesis that the population mean difference is zero under an equal-variance model.
Output from stats.camp
Two-sample t test with equal variances
------------------------------------------------------------------------------
Group | Obs Mean Std. err. Std. dev. [95% conf. interval]
-------------+----------------------------------------------------------------
0 | 12 52.916667 1.835914 6.359793 48.875847 56.957486
1 | 12 61.833333 1.795758 6.220689 57.880896 65.78577
-------------+----------------------------------------------------------------
Combined | 24 57.375 1.562482 7.654567 54.14276 60.60724
-------------+----------------------------------------------------------------
diff | -8.916667 2.568137 -14.242657 -3.590676
------------------------------------------------------------------------------
diff = mean(0) - mean(1) t = -3.4720
H0: diff = 0 Degrees of freedom = 22
Ha: diff < 0 Ha: diff != 0 Ha: diff > 0
Pr(T < t) = 0.0011 Pr(|T| > |t|) = 0.0022 Pr(T > t) = 0.9989
How to read the result
Group 0 has a mean of about 52.92 and group 1 has a mean of about 61.83. Because the reported difference is mean(0) - mean(1), the estimate is −8.92 points. Its 95% confidence interval is approximately −14.24 to −3.59, and the two-sided p-value is 0.0022.
The data are invented, and the grouping variable was not randomly assigned. The result demonstrates command interpretation; it is not evidence about real students and does not identify a causal effect.
Choose the t-test form from the study design
ttest math == 55
ttest math, by(female)
ttest read == math
ttest math, by(female) unequal
- One-sample
ttest math == 55compares one sample mean with a fixed reference value.- Two independent samples
ttest math, by(female)compares the two values of a grouping variable.- Paired measurements
ttest read == mathtests the mean within-row difference.- Unequal variances
- Add
unequalorwelchto use the supported unequal-variance calculation.
One-sample: mean(math) = 57.375; two-sided p = 0.1421 Paired: mean(read - math) = -1.208333; two-sided p = 0.0046 Unequal variances: mean(0) - mean(1) = -8.916667 Satterthwaite's degrees of freedom = 21.989249; two-sided p = 0.0022
These results answer different questions even though they use the same dataset. Choose the form before examining its p-value; do not substitute a paired test for independent observations or choose a variance assumption because it produces a preferred result.
Assumptions, variants, and limits
- Check that observations are independent and that a mean-based comparison is appropriate for the outcome and sample size.
- The default two-sample form assumes equal population variances. stats.camp also supports
unequalandwelchalternatives for the corresponding model. - One-sample and paired tests answer different questions; choose the form from the study design rather than the observed p-value.
- A small p-value is not an effect size, a measure of practical importance, or proof of causality.
See the current ttest entry and Stata's ttest manual for an external reference.
Related guides
Run this exact example
Clone the example repository in stats.camp and open the exact do-file shown on this page. No stats.camp account is required.
Open this example