ttest: compare means between two groups
Estimate a difference in group means, quantify its uncertainty, and separate statistical evidence from causal interpretation.
Independent software. Not affiliated with, sponsored by, or endorsed by StataCorp LLC.
Complete example
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.
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