Statistical Pitfalls
Statistical methods are only as good as the way they are used. This lesson collects the most common traps. Each is demonstrated with a simulation, so you can see the problem rather than just read about it.
1. Multiple testing and p-hacking
At α = 0.05, each test of a true null hypothesis has a 5% chance of a false positive. Run many tests and false positives become almost certain:
P(at least one false positive in m tests) = 1 − 0.95^mfor m in [1, 5, 10, 20, 50]:
print(f"{m:2d} tests -> {1 - 0.95 ** m:.0%} chance of at least one false positive") 1 tests -> 5% chance of at least one false positive
5 tests -> 23% chance of at least one false positive
10 tests -> 40% chance of at least one false positive
20 tests -> 64% chance of at least one false positive
50 tests -> 92% chance of at least one false positive
p-hacking is the practice, sometimes unintentional, of trying many analyses (different subgroups, outcomes, outlier rules, stopping times) and reporting only the one that gives p < 0.05. Below, a "researcher" tests whether a useless supplement improves any of 20 health measures. The supplement does nothing: both groups come from the same distribution.
import numpy as np
from scipy import stats
rng = np.random.default_rng(20)
measures = [f"measure_{i:02d}" for i in range(20)]
hits = []
for name in measures:
placebo = rng.normal(0, 1, 30)
supplement = rng.normal(0, 1, 30) # no real effect
p = stats.ttest_ind(supplement, placebo).pvalue
if p < 0.05:
hits.append((name, round(float(p), 4)))
print("'significant' findings:", hits)'significant' findings: [('measure_13', 0.0438), ('measure_16', 0.0442)]
Two of the twenty measures came out "significant", exactly the kind of fluke you expect from 20 tests. A paper reporting only those two "findings" would look convincing, and would be wrong.
Corrections
If you run several tests, adjust for it. The Bonferroni correction tests each hypothesis at α / m. The Holm method is uniformly more powerful, and the Benjamini–Hochberg procedure controls the false discovery rate instead:
import numpy as np
from statsmodels.stats.multitest import multipletests
p_values = np.array([0.001, 0.008, 0.012, 0.030, 0.041, 0.20, 0.47, 0.76])
for method in ["bonferroni", "holm", "fdr_bh"]:
reject, p_adj, _, _ = multipletests(p_values, alpha=0.05, method=method)
print(f"{method:10s} rejects {reject.sum()}: adjusted p = {np.round(p_adj, 3).tolist()}")bonferroni rejects 1: adjusted p = [0.008, 0.064, 0.096, 0.24, 0.328, 1.0, 1.0, 1.0]
holm rejects 1: adjusted p = [0.008, 0.056, 0.072, 0.15, 0.164, 0.6, 0.94, 0.94]
fdr_bh rejects 3: adjusted p = [0.008, 0.032, 0.032, 0.06, 0.066, 0.267, 0.537, 0.76]
The best protection is to pre-register: decide the hypothesis, outcome, sample size and analysis before collecting the data.
2. Peeking: stopping when it looks significant
Checking the p-value after every batch of data and stopping as soon as p < 0.05 also inflates false positives, even with a single outcome:
import numpy as np
from scipy import stats
rng = np.random.default_rng(7)
experiments = 2000
data = rng.normal(0, 1, (experiments, 2, 200)) # groups a and b, no real difference
a, b = data[:, 0], data[:, 1]
significant = np.zeros(experiments, dtype=bool)
for n in range(20, 201, 20): # peek after every 20 users per group
p = stats.ttest_ind(a[:, :n], b[:, :n], axis=1).pvalue # all experiments at once
significant |= p < 0.05 # stop at the first "significant" peek
print(f"false positive rate with peeking: {significant.mean():.1%}")false positive rate with peeking: 19.6%
Instead of the promised 5%, this procedure "finds" an effect several times as often. Fix the sample size in advance (or use sequential methods designed for interim looks).
3. Simpson's paradox
A trend that appears in every subgroup can reverse when the groups are combined, because a lurking variable affects both group sizes and outcomes. Two hospitals treat mild and severe cases:
import pandas as pd
data = pd.DataFrame({
"hospital": ["North", "North", "South", "South"],
"severity": ["mild", "severe", "mild", "severe"],
"patients": [100, 400, 400, 100],
"recovered": [95, 300, 360, 65],
})
data["rate"] = (data["recovered"] / data["patients"]).round(3)
print(data.pivot(index="severity", columns="hospital", values="rate"))
overall = data.groupby("hospital")[["patients", "recovered"]].sum()
overall["rate"] = (overall["recovered"] / overall["patients"]).round(3)
print()
print(overall)hospital North South
severity
mild 0.95 0.90
severe 0.75 0.65
patients recovered rate
hospital
North 500 395 0.79
South 500 425 0.85
North has the better recovery rate for mild and for severe cases, yet South looks better overall, because North treats mostly severe cases. The fair comparison is within severity groups. Whenever you compare groups, ask what else differs between them.
4. Confusing significance with importance
With a large enough sample, even a trivial difference becomes "highly significant":
import numpy as np
from scipy import stats
rng = np.random.default_rng(3)
a = rng.normal(100.0, 15, 200_000)
b = rng.normal(100.3, 15, 200_000) # a 0.3-point difference on a scale with SD 15
res = stats.ttest_ind(a, b)
d = (b.mean() - a.mean()) / np.sqrt((a.var(ddof=1) + b.var(ddof=1)) / 2)
print(f"p-value = {res.pvalue:.1e}")
print(f"difference = {b.mean() - a.mean():.2f} points, Cohen's d = {d:.3f}")p-value = 4.9e-12
difference = 0.33 points, Cohen's d = 0.022
Always report an effect size and a confidence interval next to the p-value, and ask whether the effect matters in practice.
5. More traps in brief
| Trap | What goes wrong | Remedy |
|---|---|---|
| Correlation ≠ causation | confounders create associations | randomised experiments, careful adjustment |
| Survivorship bias | only "survivors" are in the data (successful firms, returning planes) | ask who is missing |
| Regression to the mean | extreme first measurements tend to be followed by less extreme ones | use a control group |
| Base-rate neglect | ignoring how rare something is (see Bayes in Probability Basics) | think in absolute numbers |
| Misleading graphs | truncated axes, 3D effects, cherry-picked ranges | start bars at zero, show all data |
| "Not significant" = "no effect" | small samples miss real effects | look at the confidence interval and power |
Test yourself
A researcher tests 40 independent outcomes of an ineffective treatment at α = 0.05. About how many 'significant' results should they expect by chance?
- 0
- 2
- 5
- 20
What is Simpson's paradox?
- A test that always rejects H₀
- A trend in every subgroup that reverses when the groups are combined
- Correlation without causation
- A p-value above 1
Using Bonferroni with 10 tests at overall α = 0.05, what threshold does each test use?
- 0.05
- 0.5
- 0.005
- 0.0005