Command reference
ttest — compare means
Test a sample mean against a reference value, compare two independent group means, or test the mean difference between paired measurements. The output combines an estimated difference with its uncertainty and one-sided and two-sided p-values.
Syntax
ttest outcome == # [if condition] [in first/last] [, level(#)]
ttest outcome [if condition] [in first/last], by(group) [options]
ttest first == second [if condition] [in first/last] [, options]
Replace # after == with a fixed reference mean for a one-sample test. Use by(group) for two independent groups in one outcome column. With two variable names around ==, the default is a paired test: each row is one pair, and the tested difference is first − second.
For two independent samples stored in separate columns, use the two-variable form with unpaired. The columns may contain different numbers of nonmissing observations. if filters observations by a condition; in restricts their current row positions. Weights are not accepted.
Options
Example
The complete example uses 24 synthetic student records to demonstrate four forms: compare mean math with 55, compare math between the two female groups, compare paired read and math measurements, and repeat the group comparison allowing unequal variances.
clear
import delimited "data/student_scores.csv"
ttest math == 55
ttest math, by(female)
ttest read == math
ttest math, by(female) unequal
Sample data included. Opens in a new tab; click Run.
What to look for
In the grouped comparison, the means are about 52.92 for group 0 and 61.83 for group 1. The reported difference is therefore about −8.92 points, with a 95% confidence interval from −14.24 to −3.59 under the pooled-variance test. The unequal-variance version retains the same estimated difference while changing its uncertainty calculation.
The one-sample and paired tests answer different questions. A p-value from one form cannot be substituted for another simply because they use the same records. The dataset is invented, so these results teach interpretation rather than establish findings about real students.
| Comparison | Estimate | Two-sided p-value |
|---|---|---|
| Math mean against 55 | Mean = 57.375 | 0.1421 |
| Math: group 0 minus group 1 | Difference = −8.916667 | 0.0022 |
| Paired read minus math | Difference = −1.208333 | 0.0046 |
| Group comparison, unequal variances | Difference = −8.916667 | 0.0022 |