Many tests and p-hacking
Last updated
A p-value below 0.05 means a result that would turn up by luck about 1 time in 20. So what happens when you run 20 tests? Or 40, slicing the data a different way each time until something works? In this lesson you'll see how many tests make false discoveries almost certain, how an analyst can fool themselves without meaning to, and the corrections (Bonferroni and the false discovery rate) that keep many tests honest.
By the end of this lesson you can
- Explain why running many tests makes false positives likely
- Recognise p-hacking and the garden of forking paths
- Correct p-values with Bonferroni and Benjamini–Hochberg, using statsmodels
- Say what pre-registration is for
Twenty tests at 0.05
A sweet comes in 20 colours. Someone wonders whether any colour is good for your health, and tests each one: 30 people who ate it against 30 who didn't. (The idea is from an xkcd comic, "Significant".) In this made-up data no colour does anything. Run the first cell.
Lime: p = 0.019. "Lime sweets linked to better health!" Yet the data was made with no effect at all. With 20 tests, each with a 5% chance of a false alarm, the chance that at least one comes out significant is
The second cell checks it: in 10,000 rounds of 20 tests, 63.9% had at least one "significant" colour. Try it, then switch on the Bonferroni correction from step 4:
Each colour is tested against people who didn't eat it, at p < 0.05. No colour does anything, so every “significant” result is a false alarm.
Without the correction, about two rounds in three produce a headline. With it, about one in twenty: the same as a single test at 0.05.
The garden of forking paths
Nobody runs twenty tests and reports only the lucky one on purpose, surely? It happens more innocently. The Tindahan owners played a radio ad on 10 days in February and want to know if it helped. (Here the ad days were picked at random, so it can't have done anything.) Watch what happens as the analysis goes on:
Each question sounded reasonable on its own, and the analyst only ever "looked at the data". But each new way of slicing it was another test, and with 41 tests of an effect that isn't there, a couple of p-values below 0.05 were almost certain. Andrew Gelman and Eric Loken called this the garden of forking paths: there are so many reasonable choices (which stores, which products, which days, which outliers to drop) that an analyst who makes them after seeing the data can nearly always find something.
Searching through analyses until one is significant, on purpose or not, is called p-hacking. A 2011 paper showed that with just four common, flexible choices, researchers could make a false result significant more than 60% of the time.
Wrong way: test until it works
Here's that analysis in code. Run the cells.
- All stores and products together: p = 0.614. The ad did nothing.
- The loop runs 36 tests: each store and product, on all days, weekdays and weekends. Two come out significant: Tanauan's oil and sugar, on weekdays.
The tempting report is "The radio ad raised oil and sugar sales in Tanauan on weekdays (p = 0.005)". Every number in it is correct. It's still wrong, because it leaves out the 34 other tests. Nobody reading it could tell that this was the best of 36 tries, and that about 2 of 36 tests would come out "significant" by luck alone.
The check: count your tests. Every slice, subgroup, outcome and version of the analysis you tried counts, including the ones you dropped. Then correct for them (step 4), and report them all.
Bonferroni and the false discovery rate
There are two common corrections for running tests:
- Bonferroni makes each test meet a stricter threshold, . With 36 tests that's . It keeps the chance of even one false alarm, across all the tests, below 0.05. It's simple and safe, but strict: real effects can fail to pass.
- Benjamini–Hochberg (BH, 1995) controls the false discovery rate instead: among the results you call significant, the expected share of false alarms is at most 5%. Sort the p-values from smallest to largest; the -th smallest passes if it's below (and so do all the ones before it). It finds more real effects than Bonferroni when there are many.
statsmodels does both with multipletests: it returns whether each test passes, and the adjusted p-values. Run the cells.
- The radio ad: 2 tests had p < 0.05; after Bonferroni, 0; after BH, 0. Tanauan's p = 0.0045 isn't small enough for 36 tests.
- A hunt with real effects in it: the second cell tests whether each weekday is different from the rest, for every store and product: 84 tests. The shops really are busier on Saturdays and quieter on Tuesdays. 45 tests have p < 0.05; Bonferroni keeps 17 and BH keeps 42. BH keeps most of the real effects while still controlling false discoveries; Bonferroni throws many away.
Use Bonferroni when even one false alarm would be costly, or there are only a few tests. Use BH when you're screening many things (products, stores, genes) and will follow up on the hits.
Decide first: pre-registration
Corrections only work if you count every test, and the garden of forking paths makes that hard: you can't count analyses you only might have done. The fix is to decide before you look.
Pre-registration means writing down, before seeing the results, exactly what you'll test: the question, the data, the groups, the test and what counts as success. Medical trials have had to do this for years, and many journals now accept it for other studies too. For a shop it can be one paragraph in a shared document, dated: "We'll compare total kilos on ad days and other days, all stores together, with Welch's t-test."
Then there are two kinds of analysis, and both are fine as long as they're labelled:
- Confirmatory: the test you planned. Its p-value means what it says.
- Exploratory: everything you looked at afterwards. Useful for ideas, but treat its "findings" as questions to test next time on new data, not as results.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- Munroe, Significant, xkcd 882: the jelly bean comic: twenty colours, one “significant” by chance
- Gelman and Loken, The Statistical Crisis in Science, American Scientist 102(6), 2014: the garden of forking paths: many possible analyses, even without fishing on purpose
- Simmons, Nelson and Simonsohn, False-Positive Psychology, Psychological Science 22(11), 2011: how flexible choices in an analysis make almost anything significant
- Benjamini and Hochberg, Controlling the False Discovery Rate, Journal of the Royal Statistical Society B 57(1), 1995: the false discovery rate and the procedure that controls it
- statsmodels: statsmodels.stats.multitest.multipletests: Bonferroni, Benjamini–Hochberg and other corrections for many tests
- Nosek et al., The preregistration revolution, PNAS 115(11), 2018: why and how to write down an analysis before seeing the data