Skip to content

Simulate sampling variability

Run the same study again and you get different numbers. Simulation shows how different, and that this is normal.

Introductory ~9 min7 steps

What runs in CodeSplash

You will be able to

  • Simulate repeated samples from a known population
  • Predict how much a statistic moves between identical studies
  • Avoid reading a change between two studies as a real change

Read first

01

One study is one draw

Read

Your dataset is one of the many samples you could have drawn. A different draw would have given a different mean, a different difference and a different p-value, with no change in the underlying truth.

That movement is sampling variabilitySampling variabilityHow much a statistic changes from one sample to another, even when nothing about the population changes.. It isn't a mistake, and it doesn't go away. Statistics exists largely to measure it.

02

Two labs, one truth

Predict

Predict first

Two labs run the same experiment, with a real effect of half a standard deviation and 32 subjects per group. How likely is it that both get p < .05?

Very likely: the effect is realAbout a one-in-four chanceAlmost impossible
Reveal

About one in four. With this design each lab has roughly a coin flip's chance, 0.5×0.5=0.250.5 \times 0.5 = 0.25. So one significant and one not is the most common way for two faithful replications to differ.

03

Watch the p-values dance

Explore

Every dot below is one replication of the same study, with the same true effect. Draw new replications with the Replications drawn control, and watch Smallest p and Largest p.

Then raise n per group. What happens to the dance?

BExplorable · The dance of the p-values

If you ran the same experiment again, how different could the p-value be?

What share of these replications will reach p < .05?

0.50
How sure?
0.50
32
25
p = .0510⁻510⁻410⁻30.010.11

Each dot is one exact replication of the same study: same truth, same n. Colored dots reached p ≤ .05.

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

04

Simulate it yourself

Explore

A simulation needs a population you define and a loop. Run this in a notebook and change the sample size.

Python
import numpy as np

rng = np.random.default_rng(1)
for study in range(5):
    sample = rng.normal(loc=20, scale=7, size=30)
    print(f"study {study + 1}: mean = {sample.mean():.2f}")
R
set.seed(1)
for (study in 1:5) {
  sample <- rnorm(30, mean = 20, sd = 7)
  cat(sprintf("study %d: mean = %.2f\n", study, mean(sample)))
}

Each language has its own generator, so their numbers differ. Both scatter around the true mean of 20.

05

Check your reading

Checkpoint

Check yourself

You draw five samples of 30 from the same population. Their means are all different. What does that tell you?

Show the answer

Answer: c

Identical means would be the surprising result. How much they vary is what the next lessons measure.

Check yourself

In the simulation, you raise the sample size from 30 to 300. What happens to the study means?

Show the answer

Answer: b

Larger samples vary less, but they never stop varying. The clustering tightens with the square root of the sample size.

06

Last month against this month

Reflect

A colleague says: "Last month's test gave p = 0.03. This month's rerun gave p = 0.20. The effect must have gone away."

What would you ask before agreeing? Think about the size of each estimate and how wide each interval is.

07

Recap

Recap
  • Your sample is one draw. Another draw gives other numbers.
  • p-values move a lot between identical studies, far more than most people expect.
  • Compare estimates and their intervals, not whether two studies landed on the same side of 0.05.

Concepts in this lesson