ttest
Asks whether two groups really differ on average, or whether a mean differs from a number.
What we have
- two groups, with
by() - comparing a mean with a fixed number —
ttest mpce == 3000
Coming later
- paired t-tests
How you write it
ttest var, by(groupvar) | ttest var == value
Square brackets mean optional. A vertical bar means choose one.
What it does
ttest mpce, by(sector) compares average spending in rural and urban households and reports how likely a gap that large would be if there were really no difference (the p-value). ttest mpce == 3000 compares the average against a fixed number instead.
You get both means, the difference, its margin of error (confidence interval) and the p-value.
Watch out. A small p-value says the difference is unlikely to be chance; it says nothing about whether the difference is large enough to matter. Look at the size of the gap and the confidence interval, not only the p-value.
Try it
Every example below was run after sysuse vira.sample02, clear.
. ttest mpce, by(sector)
Two-sample t test with equal variances Group 1 Obs = 3,987 Mean = 3401.22 Group 2 Obs = 2,013 Mean = 4347.226 diff = -946.0057 t = -24.4358 df = 5,998 Pr(|T| > |t|) = 0.0000
The same thing in pandas and in R
R
t.test(mpce ~ sector, data = df, var.equal = TRUE) # This R was run in a real R (4.6.1) and gave the same numbers.