Simulate sampling variability
Run the same study again and you get different numbers. Simulation shows how different, and that this is normal.
What runs in CodeSplash
- Two-sample t-test (Welch / Student, or z test) Verified in Python and R
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
One study is one draw
ReadYour 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.
Two labs, one truth
PredictPredict 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?
Reveal
About one in four. With this design each lab has roughly a coin flip's chance, . So one significant and one not is the most common way for two faithful replications to differ.
Watch the p-values dance
ExploreEvery 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?
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 to see the result.
Simulate it yourself
ExploreA simulation needs a population you define and a loop. Run this in a notebook and change the sample size.
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}")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.
Check your reading
CheckpointCheck 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.
Last month against this month
ReflectA 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.
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