Is one model really better? Paired comparisons
A leaderboard's order isn't evidence of a difference. Compare fold by fold, correct for overlapping training rows, and ask what gap would matter in practice.
What runs in CodeSplash
- Compare two ROC curves (paired) Verified in Python and R
- McNemar's test (paired proportions) Verified in Python and R
You will be able to
- Explain why comparisons should be paired on shared folds
- Say why a plain paired t test on folds is too optimistic, and what corrects it
- Read McNemar, DeLong, the region of practical equivalence and the leaderboard's chips
Same folds, same questions
ReadEvery model in an experiment uses one split plan. So on each fold, two models answer the same questions, and their fold scores are paired.
Pairing removes the difference between easy and hard folds, which is often larger than the difference between the models. Two runs that look tied on average might be consistently ordered, fold by fold.
View data
| patient | visit | score |
|---|---|---|
| P01 | baseline | 34.700 |
| P01 | week 4 | 31.400 |
| P01 | week 12 | 24.200 |
| P02 | baseline | 34.900 |
| P02 | week 4 | 34.300 |
| P02 | week 12 | 36.500 |
| P03 | baseline | 34.500 |
| P03 | week 4 | 36.200 |
| P03 | week 12 | 40.100 |
| P04 | baseline | 30.400 |
| P04 | week 4 | 28.900 |
| P04 | week 12 | 27.400 |
| P05 | baseline | 37.200 |
| P05 | week 4 | 33.100 |
| P05 | week 12 | 32.100 |
| P06 | baseline | 26.500 |
| P06 | week 4 | 22.500 |
| P06 | week 12 | 18.900 |
| P07 | baseline | 26.600 |
| P07 | week 4 | 21.800 |
| P07 | week 12 | 16.700 |
| P08 | baseline | 26.600 |
| P08 | week 4 | 28.700 |
| P08 | week 12 | 20.900 |
| P09 | baseline | 16.500 |
| P09 | week 4 | 14.900 |
| P09 | week 12 | 12.800 |
| P10 | baseline | 24.200 |
| P10 | week 4 | 24 |
| P10 | week 12 | 24.100 |
| P11 | baseline | 34.300 |
| P11 | week 4 | 35.700 |
| P11 | week 12 | 36.100 |
| P12 | baseline | 30 |
| P12 | week 4 | 26 |
| P12 | week 12 | 24.600 |
| P13 | baseline | 28.400 |
| P13 | week 4 | 23.100 |
| P13 | week 12 | 22 |
| P14 | baseline | 12.900 |
| P14 | week 4 | 11.300 |
| P14 | week 12 | 6 |
| P15 | baseline | 26.100 |
| P15 | week 4 | 23.400 |
| P15 | week 12 | 25.900 |
| P16 | baseline | 28.400 |
| P16 | week 4 | 28.300 |
| P16 | week 12 | 28.200 |
| P17 | baseline | 30.100 |
| P17 | week 4 | 30.400 |
| P17 | week 12 | 32.300 |
| P18 | baseline | 20.900 |
| P18 | week 4 | 18.900 |
| P18 | week 12 | 15 |
| P19 | baseline | 19.800 |
| P19 | week 4 | 20.700 |
| P19 | week 12 | 16 |
| P20 | baseline | 31.900 |
| P20 | week 4 | 31.400 |
| P20 | week 12 | 30.200 |
First 60 of 72 rows.
Linear and boosted models on Pima: 0.857 and 0.843. Is that gap real?
A plain t test lies
PredictPredict first
You run a paired t test on 5 fold differences. The folds share most of their training rows, so their differences are correlated. What does that do to the plain test's standard error?
Reveal
It's too small, which makes the p-value too small, so the test finds gaps that aren't there. Fold scores aren't independent draws: with 5 folds, any two training sets share three quarters of their rows.
The correction
ReadThe Nadeau–Bengio corrected resampled t inflates the variance for that overlap:
is the number of folds. With 5 folds, , so becomes . The variance more than doubles, so the standard error grows by times.
The Holm-adjusted version of this test drives the leaderboard's chips: ≈ best (tied), worse, or unpaired (folds differ).
Tests on test rows
ReadOn the locked test rows, two models have predictions for the same rows.
- McNemar looks only at rows where one is right and the other wrong.
- DeLong tests the difference in two AUCs on the same rows. For example, glucose against BMI as a marker: AUC 0.794 against 0.681, a difference of 0.113, p < .001.
- A paired bootstrap resamples test rows and gives an interval for the difference in the primary metric.
Only the discordant rows count
ExploreMcNemar's test ignores rows where both models agree. Raise the two agreeing cells and see that nothing changes. Then move the two discordant cells.
EExplorable · McNemar's discordant pairs
For paired yes/no data, which pairs carry the evidence?
Add 1,000 people who answered yes both times. What happens to the p-value?
| Yes after | No after | |
|---|---|---|
| Yes before | 40 no change | 18 changed |
| No before | 6 changed | 36 no change |
Only the highlighted pairs count. p = .025 whatever the gray cells hold. The change in proportions is 12.0 points out of 100 people.
Commit your prediction above to see the result
Make your prediction to see the result.
Read each cell as model A's and model B's verdict on a row: right or wrong, where "before" and "after" become "A" and "B".
Would the gap matter?
ReflectA significance test asks whether a gap is zero. You probably want to know something else: would this gap change what you do?
The Bayesian correlated t test answers with a region of practical equivalenceRegion of practical equivalence (ROPE)A band around zero of differences too small to matter; decisions compare the credible interval with it. (ROPE). You choose how small a gap doesn't matter. The default is 0.01 for bounded metrics. P(better) is the probability that the other model is better by more than that, and P(equivalent) that the gap lies inside it.
For a model you're working on, what size of gap would make you switch?
Check your understanding
CheckpointCheck yourself
10-fold cross-validation, so and . What is ? Give two decimals.
Show the answer
Answer: 0.21
, about twice the plain . More folds raise the fit count, but they don't make the correction go away.
Check yourself
On 1,000 test rows, A and B agree on 950. Of the other 50, A alone is right on 30, and B alone on 20. Which rows does McNemar use?
Show the answer
Answer: b
Rows where both are right or both are wrong say nothing about which is better.
Check yourself
A leaderboard row reads ≈ best. What should you do?
Show the answer
Answer: a
A tie means the folds couldn't separate them. That isn't proof of equality, and a ROPE lets you ask about equivalence directly.
Check yourself
The leaderboard shows unpaired beside a run. What does that mean?
Show the answer
Answer: b
Paired comparison needs identical folds. Re-run with the experiment's current plan to compare fairly.
Compare runs
In the appOn the Leaderboard, tick two rows and press Compare 2. The table lists p (corrected t), p (Holm), P(better), P(equivalent), McNemar p (test), DeLong p (test) and a test-set interval. Below, one line per fold shows the pairs. Do this before believing that a lead is real.
import numpy as np
import pandas as pd
from scipy import stats
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import StratifiedKFold, cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
df = pd.read_csv("pima.csv")
X, y = df.drop(columns="diabetic"), df["diabetic"]
cv = StratifiedKFold(5, shuffle=True, random_state=1)
a = cross_val_score(make_pipeline(StandardScaler(), LogisticRegression()), X, y, cv=cv, scoring="roc_auc")
b = cross_val_score(RandomForestClassifier(300, random_state=1), X, y, cv=cv, scoring="roc_auc")
d = a - b
J, ratio = len(d), 1 / 4
t_plain = d.mean() / np.sqrt(d.var(ddof=1) / J)
t_corr = d.mean() / np.sqrt(d.var(ddof=1) * (1 / J + ratio))
for name, t in [("plain", t_plain), ("corrected", t_corr)]:
print(name, round(t, 2), round(2 * stats.t.sf(abs(t), J - 1), 3))library(ranger)
df <- rbind(MASS::Pima.tr, MASS::Pima.te)
set.seed(1)
fold <- sample(rep(1:5, length.out = nrow(df)))
d <- sapply(1:5, function(k) {
tr <- df[fold != k, ]; te <- df[fold == k, ]
a <- predict(glm(type ~ ., binomial, tr), te, type = "response")
b <- predict(ranger(type ~ ., tr, num.trees = 300, probability = TRUE, seed = 1), te)$predictions[, "Yes"]
as.numeric(pROC::auc(te$type, a, quiet = TRUE)) - as.numeric(pROC::auc(te$type, b, quiet = TRUE))
})
J <- length(d)
t_plain <- mean(d) / sqrt(var(d) / J)
t_corr <- mean(d) / sqrt(var(d) * (1 / J + 1 / 4))
round(c(plain = t_plain, p_plain = 2 * pt(-abs(t_plain), J - 1), corrected = t_corr, p_corrected = 2 * pt(-abs(t_corr), J - 1)), 3)Recap
Recap- Compare on shared folds, and treat fold scores as pairs.
- A plain t on folds is too optimistic. The corrected t and the leaderboard's chips account for the overlap.
- Ask what gap would matter, with a ROPE, and not only whether it's zero.
Concepts in this lesson