Scott OatleySociology / UCL
← Guides

Statistics with Stata: an interactive guide

The core ideas of the course as simulations you can change, each with the Stata command that does it

Companion to Introduction to Statistics in Stata (Quantitative Statistics, Advanced Quantitative Statistics and the Getting Started with Stata do-files).

Statistics is about learning from a sample about a population we cannot see in full. The simulations below create a population whose true values we know, draw samples from it, and show how the statistics behave. That is how you build intuition for standard errors, confidence intervals, p-values and model coefficients. Every simulation runs in your browser; press a button again for a fresh draw.

1Sampling distributions and the central limit theorem

A sample mean varies from sample to sample. Its spread across repeated samples is the standard error, σ/√n. The central limit theorem says that sample means are close to normally distributed once n is moderately large, even when the population is not normal.

Draw 2,000 samples and plot their means

Try thisChoose the skewed population with n = 2, then n = 50. The means become symmetric and their spread shrinks by √n.

ci mean height

2Confidence intervals

A 95% confidence interval is built by a method that captures the true mean in 95% of samples. Any single interval either contains it or does not; the 95% describes the method, not one interval.

100 samples of heights (true mean 167.2 cm, SD 9.6)

Try thisRaise n: intervals get narrower but coverage stays near the level. Raise the level: intervals get wider.

ci mean height, level(95)

3Hypothesis tests, errors and power

A significance test asks how surprising the data would be if there were no difference. Two errors are possible: a Type I error (finding a difference that is not there, with probability α) and a Type II error (missing one that is). Power is the chance of detecting a real difference.

1,000 two-group t-tests

Try thisSet the difference to 0: about α of tests are still “significant”. Those are Type I errors. Then find the n per group that gives 80% power for a difference of 0.3 SD.

ttest height, by(sex)

Statistical significance is not practical importance. With a very large sample a tiny, unimportant difference can have a minute p-value; report the size of the difference and its confidence interval as well.

4Correlation and regression

A correlation measures the strength of a linear association. Linear regression estimates how much the outcome changes for each one-unit change in a predictor, and R² is the share of variation in the outcome that the model accounts for.

Simulated sample
corr height weight regress height weight

5Confounding

A confounder Z affects both the predictor X and the outcome Y. Leave it out and the coefficient on X picks up part of Z’s effect. Including Z in the model recovers the true effect, if Z is measured well.

Y = β·X + 1·Z + error, with X partly caused by Z
ModelCoefficient on XStata

Try thisWith β = 0, X has no effect at all, yet the model without Z finds one. Set Z’s influence to 0 and the bias disappears.

The course also covers suppression, where leaving out a variable hides an effect because two paths of opposite sign cancel out, and mediation, where Z lies on the path from X to Y. Controlling for a mediator removes part of the effect you want to measure, so the causal ordering matters.

6Interactions

An interaction lets the effect of one variable depend on another. Here the effect of X on Y differs between two groups.

Y = 1·X + interaction·X·Group + error
regress y c.x##i.group margins group, dydx(x)

7Logistic regression: odds ratios and marginal effects

For a yes/no outcome, logistic regression models the log-odds. Its coefficient is often reported as an odds ratio. An odds ratio does not say how much the probability changes: that depends on the baseline probability. The average marginal effect reports the change in probability directly.

Same odds ratio, different change in probability

Try thisKeep the odds ratio at 2 and move the baseline from 0.05 to 0.5. The odds ratio never changes, but the change in probability grows from about 4 to about 17 percentage points.

logit obese i.sex i.racen age logit, or margins, dydx(*)

8Check yourself

9Course files