P-hacking from the inside
P-hacking rarely feels like cheating. It feels like cleaning data and thinking harder. Learn the usual moves, what they cost, and how to answer when someone asks you for a significant result.
What runs in CodeSplash
- Two-sample t-test (Welch / Student, or z test) Verified in Python and R
You will be able to
- Tell cleaning a dataset apart from searching for significance
- Recognize the common moves, and the sentences they hide behind
- Answer a request for a significant result with something more useful
Read first
The same edit, twice
PredictPredict first
Your test gives . You notice two extreme values, remove them, and get . Have you done anything wrong?
Reveal
It depends on why. If the instrument failed, and you'd have removed those values whatever the test said, that's cleaning. If you went looking because p was , that's p-hackingP-hackingLetting the p-value steer analysis choices, then reporting the result as if those choices were planned., even when the values do look odd. The edit is identical, and the reason is what differs.
The usual moves
ReadEach move has a sentence that goes with it, and each sentence can be true.
| Move | What it sounds like |
|---|---|
| Drop outliers | "These look wrong." |
| Add covariates | "We should adjust for age." |
| Try subgroups | "It works for new users." |
| Switch the outcome | "Retention is the better metric anyway." |
| Switch the test | "These data aren't really normal." |
| Stop at a good moment | "It's significant, so we can stop." |
| Keep collecting | "We're nearly there." |
| Recode | "Let's merge those two categories." |
P-hacking is when the p-value decided which sentence you said.
Stop at a good moment
ExploreNo simulated study here has any effect. Each one checks p as the data arrive, and stops at the first . Change how often you look, then find a schedule that puts the error rate above 25%.
BExplorable · Optional stopping
What happens to the false-positive rate if you check the p-value as data arrive and stop when it is significant?
With this peeking schedule, what share of no-effect studies will end 'significant'?
Commit your prediction above to see the result
Make your prediction to see the result.
One look at the end stays near 5%. Nothing about the data changed, only the rule for when to stop. The sequential looks lesson in Inference in Depth covers honest ways to look early.
One move costs little
ReflectThe earlier garden showed that one fork adds only a few points to the error rate. That is the trap. Each step feels too small to matter, so you never decide to p-hack. You make several reasonable-looking decisions in a row.
Think of your last analysis that came out as you hoped. How many of the moves in the table did you make? Would you have made them if the first p-value had already been below 0.05?
The last question is the useful one, and you can ask it of any analysis.
import numpy as np
from scipy import stats
rng = np.random.default_rng(1)
n, reps, plain, hacked = 30, 5000, 0, 0
def welch(a, b):
return stats.ttest_ind(a, b, equal_var=False).pvalue
for _ in range(reps):
a, b = rng.normal(size=n), rng.normal(size=n) # no real difference
p = welch(a, b)
plain += p <= 0.05
y = np.r_[a, b]
m, s = y.mean(), y.std(ddof=1)
# The move: try again without |z| > 2, and keep the smaller p.
trimmed = welch(a[abs(a - m) <= 2 * s], b[abs(b - m) <= 2 * s])
hacked += min(p, trimmed) <= 0.05
print(plain / reps, hacked / reps)set.seed(1)
n <- 30; reps <- 5000
res <- replicate(reps, {
a <- rnorm(n); b <- rnorm(n) # no real difference
p <- t.test(a, b)$p.value
y <- c(a, b); m <- mean(y); s <- sd(y)
# The move: try again without |z| > 2, and keep the smaller p.
trimmed <- t.test(a[abs(a - m) <= 2 * s], b[abs(b - m) <= 2 * s])$p.value
c(plain = p <= 0.05, hacked = min(p, trimmed) <= 0.05)
})
rowMeans(res)Spot the move
CheckpointCheck yourself · choose all that apply
Which of these are p-hacking? Choose all that apply.
Show the answer
Answer: b, c, e
What matters is whether the result steered the choice, and whether the reader can see that. A rule fixed in advance is fine, and so is exploration that's labeled and reported in full.
Check yourself
You tried many analyses. Can you apply Bonferroni at the end and report the best one?
Show the answer
Answer: b
An adjustment fixes the tests it counts. It can't count forks you never wrote down, so it doesn't turn a search into a confirmation.
Someone asks for significance
CheckpointCheck yourself
Your manager looks at and says, "Can you find a way to get this over the line?" What is the most useful answer?
Show the answer
Answer: c
The refusal is the easy part, and it doesn't help anyone decide. Offer what the data do say, and what it would take to know more.
Ask the assistant to do it
In the appRun a test, and press Write with AI on its card. In Instructions, type make it significant, then press Write. Nothing goes to a model. You see a notice beginning "Not sent", with the phrase it matched, the reason, and a plan to follow instead.
This only reads the wording of AI requests, so it's a reminder and not a lock. You can still run any test by hand. The ledger is what keeps the record.
Recap
Recap- The same edit can be cleaning or p-hacking. What differs is whether the result steered it.
- The moves arrive disguised as good sentences, and each costs little, so the total sneaks up on you.
- When asked for significance, offer the estimate, its interval, and what it would take to know more.
Concepts in this lesson