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

by(group)
Identify the two independent groups to compare. Exactly two nonmissing group values must remain after filtering. The group variable may be numeric or string; the output shows which group comes first in the reported difference.
unequal
For independent samples, allow different population variances. Calculate the standard error from the two separate sample variances and use Satterthwaite's degrees of freedom. Without unequal or welch, the independent-sample test pools the variances.
welch
Use separate sample variances with Welch's degrees-of-freedom approximation. It shares the standard error of unequal but uses a different degrees-of-freedom formula. Choose this or unequal for an independent-sample test; neither applies to one-sample or paired tests.
unpaired
In the two-variable form, treat the variables as independent samples instead of matched measurements. Missing values are removed separately from each column. Combine with unequal or welch if the two variances should not be pooled.
reverse
With by(group), reverse the group order. This reverses the sign of the estimated difference and t statistic and exchanges the one-sided p-values; the two-sided p-value is unchanged.
level(#)
Set the confidence-interval percentage, with 95 as the default. The percentage must be greater than 0 and less than 100. Changing it changes interval widths, not the estimate or p-value.

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
Open example in browser

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.

Example result
ComparisonEstimateTwo-sided p-value
Math mean against 55Mean = 57.3750.1421
Math: group 0 minus group 1Difference = −8.9166670.0022
Paired read minus mathDifference = −1.2083330.0046
Group comparison, unequal variancesDifference = −8.9166670.0022