EProportions · Guide test 064
McNemar test
Tests equal marginal probabilities for paired binary outcomes using discordant pairs.
Do it in CodeSplash
In the app, set it up in Analyze with this method:
- McNemar's test (paired proportions) — Did the share of an outcome change between two paired measurements?
Do it in CodeSplash Run this test on your own data, in Python or R.
Bench
Change the design, drag the data, move α. The statistic, its reference distribution and the verdict follow.
Bench · McNemar test
Your data
Edit the counts| After: yes | After: no | Total | |
|---|---|---|---|
| Before: yes | 30E = 21.0 | 18E = 27.0 | 48 |
| Before: no | 5E = 14.0 | 27E = 18.0 | 32 |
| Total | 35 | 45 | 80 |
E is the count expected if rows and columns were unrelated.
If H₀ were true
- The reference: how χ² varies across samples when H₀ is true (χ² approximation with continuity correction).
- Your statistic, from the data above.
- The p-value: the area at least as extreme as yours (p = .012).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- pairs that switched yes → no
- pairs that switched no → yes
- Yates' continuity correction (R's default)
The verdict
χ²(1) = 6.26, p = .012
p ≤ α = .050: reject H₀
Only the discordant pairs carry information about change; pairs that stayed the same drop out entirely.
- Statistic
- 6.261
- p-value
- 0.0123
McNemar test on a 2 × 2 table of 80 counts: χ²(1) = 6.26, p = .012. The p-value is below α = .050, so the data are hard to reconcile with H₀.
Question and hypotheses
H0: paired binary marginal probabilities are equal, equivalently p01=p10.
Assumptions
Independent pairs; binary outcomes observed twice or in matched units.
Formula
Parameters and notation
b,c are discordant cell counts. Concordant pairs do not contribute to the conditional test.
Structure of the method
Reduce a paired binary table to its two discordant counts. Under marginal equality, the conditional direction of discordance is a fair binomial outcome; the large-sample version standardizes the difference between discordant counts and uses a one-degree-of-freedom chi-square reference.
Estimated and nuisance parameters
The informative sample size for the conditional test is b+c, not the number of all pairs. The discordant-direction probability is fixed at one half under the null. Marginal response rates and paired risk differences are useful separate descriptive estimates.
Settings and design choices
Prespecify exact binomial versus asymptotic calibration, two-sided ordering, and continuity correction. The supplied examples intentionally use the uncorrected asymptotic version. Keep which outcome is before/after or treatment/control fixed when interpreting the direction of change.
Fitting and adaptation
Recompute the discordant pair counts for each new matched sample. Resampling must preserve the pair: swapping labels within independent pairs can implement a suitable randomization null, whereas independently permuting all observations destroys the design. There is no training or fine-tuning stage.
Sensitivity and robustness
Many concordant pairs cannot compensate for very few discordances. Missing one response removes or changes the paired sampling unit and can create selection bias. Multiple observations per participant require more than simple pairing; an exact discordance calculation does not account for higher-level clustering.
How to use and calibrate
Use χ²1 asymptotically or the exact binomial test on discordant pairs. State any continuity correction.
Example application
Compare a diagnostic outcome before and after an intervention in the same participants.
Interpretation and pitfalls
Watch out
A test treating the two margins as independent ignores pairing. Few discordant pairs imply low information even if total n is large.
What changed in edition 2
- New sections: structure of the method, estimated and nuisance parameters, settings and design choices, fitting and adaptation, sensitivity and robustness.
Python and R
For McNemar the table represents paired before/after categories; other entries use independent observations. The same numbers illustrate different sampling designs.
Validated — Executed successfully in the validation environment · needs numpy, scipy
import numpy as np
from scipy import stats
rng = np.random.default_rng(2026)
tab = np.array([[18,12],[9,21]])
print(__import__("statsmodels.stats.contingency_tables",fromlist=["mcnemar"]).mcnemar(tab,exact=False,correction=False))
Output
pvalue 0.5126907602619235 statistic 0.42857142857142855
Validated — Executed successfully in the validation environment · needs Base/standard library
tab <- matrix(c(18,12,9,21),nrow=2,byrow=TRUE)
print(mcnemar.test(tab,correct=FALSE))
Output
McNemar's Chi-squared test data: tab McNemar's chi-squared = 0.42857, df = 1, p-value = 0.5127
Sources
- statsmodels — statsmodels · stats.contingency_tables.mcnemarstatsmodels.org
- statsmodels.stats.contingency_tables.mcnemarstatsmodels.org
- Statistics guide v4 (historical):
- Python: Executed successfully in the validation environment · R: Executed successfully in the validation environment
- Reference's record:
- Edition 2 review: Python: skip · R: not executed (Rscript unavailable); why not run: Python: Unavailable local dependencies: statsmodels. Python: Not run in the reference's review: Unavailable local dependencies: statsmodels. No installation attempted. R: Reviewed but not run: R wasn't available to the reference's authors.
From the CodeSplash statistics guide (v4), entry 064, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT064.