McNemar's Test
McNemar's Test
Definition
Core Statement
McNemar's Test is a statistical test used on paired nominal data (typically 2x2 contingency tables). It tests if the proportions of "discordant" (changing) pairs are equal. It is essentially a paired version of the Chi-Square test.
Purpose
- Before/After Studies: Did the treatment change the Pass/Fail status?
- Algorithm Comparison: Do two classifiers disagree on the same test set significantly?
- Matched Pairs: Testing differences in discordant pairs in case-control studies.
When to Use
Use McNemar's When...
- Outcome is Binary (Yes/No).
- measure is Paired/Repeated (Same subject Before & After).
- You want to know if the change in one direction (No
Yes) is more frequent than the other (Yes No).
Do Not Use When...
- Samples are independent (Use Chi-Square Test of Independence or Fisher's Exact Test).
- Outcome is continuous (Use Paired T-Test).
Theoretical Background
The Contingency Table (Paired)
| After: Positive (+) | After: Negative (-) | |
|---|---|---|
| Before: Positive (+) | ||
| Before: Negative (-) |
- Concordant pairs (
): Didn't change. The test ignores these. - Discordant pairs (
): Changed status.
The Statistic
Under
Worked Example: Effect of Ad Campaign
Problem
You survey 100 people Before and After seeing an ad.
Question: "Will you buy the product?" (Yes/No).
Data:
- Yes/Yes (
): 20 (Loyal) - Yes
No ( ): 5 (Lost customers) - No
Yes ( ): 25 (Gained customers) - No/No (
): 50 (Never interested)
Analysis:
We only care about the changers: 25 people switched TO the product, 5 switched AWAY. Is this net gain significant?
Calculation:
- Hypothesis:
. - Statistic:
- Critical Value:
. - Decision:
. Reject . - Conclusion: The ad campaign significantly increased purchase intent (Net gain is real).
Assumptions
Limitations
Pitfalls
- Ignoring Concordance: It can feel weird that 90% of your data (
and ) is ignored, but that's correct—if they didn't change, they provide no evidence of an effect. - Confusion with Chi-Square: If you format this as a standard 2x2 and run Chi-Square test of Independence, it answers "Is Before state associated with After state?" (Yes, obviously). It does NOT answer "Did the rate change?".
Python Implementation
from statsmodels.stats.contingency_tables import mcnemar
# Table layout: [[Yes/Yes, Yes/No], [No/Yes, No/No]]
# Note: Layout conventions vary!
# Standard Statsmodels: [[a, b], [c, d]]
table = [[20, 5],
[25, 50]]
# McNemar Test
result = mcnemar(table, exact=False, correction=True)
print(f"statistic={result.statistic}, p-value={result.pvalue}")
if result.pvalue < 0.05:
print("Significant change (Marginal Homogeneity rejected)")
Related Concepts
- Paired T-Test - Continuous equivalent.
- Chi-Square Test of Independence - Unpaired equivalent.
- Wilcoxon Signed-Rank Test - Paired ordinal equivalent.