This calculator runs McNemar's test, the paired analogue of the chi-square / Fisher's test for a 2×2 table. Use it when the same subjects are measured twice on a binary outcome (before-after, matched case-control, or two raters on the same items) and you want to know whether the proportion changed. McNemar's test focuses on the off-diagonal discordant cells — the subjects whose outcome flipped — and tests for marginal homogeneity. It widely used in clinical trials, diagnostic-test comparisons, and pre/post intervention studies. for a quick example.
Paired 2×2 table: rows are the Before outcome and columns are the After outcome for the same subjects measured twice. Enter counts of subjects in each combination. The b and c cells (the off-diagonal / discordant cells, where the outcome flipped) drive the test. You can edit the variable and category names by clicking directly on the headers or row labels.
\ | ||
|---|---|---|
Enter counts of the same subjects measured twice. b and c (the off-diagonal/discordant cells) drive the test.
McNemar's Test assesses whether the proportion of a binary outcome changes between two paired measurements on the same subjects — for example, before vs after an intervention, or cases vs matched controls. Formally it tests marginal homogeneity in a paired 2×2 table: the null hypothesis is that the two marginal proportions are equal. Because the pairs are dependent, only the discordant pairs (those that changed) carry information about the direction and size of the change.
For the paired 2×2 table, label the cells:
| Before \ After | After + | After − |
|---|---|---|
| Before + | a | b |
| Before − | c | d |
The uncorrected chi-square statistic (1 df):
With Yates' continuity correction:
When is small (commonly < 25), use the exact binomial test instead, comparing against .
Discordant odds ratio (effect size):
Diagnostic outcome before and after a treatment for the same patients:
| Before \ After | Positive | Negative | Total |
|---|---|---|---|
| Positive | 4 (a) | 5 (b) | 9 |
| Negative | 2 (c) | 5 (d) | 7 |
| Total | 6 | 10 | 16 |
Concordant cells a (4) and d (5) are ignored. Only the discordant cells matter: b = 5 (Positive → Negative) and c = 2 (Negative → Positive), for discordant pairs.
Because is small, the exact binomial test is preferred over the chi-square approximation. The continuity-corrected chi-square would be:
The exact two-sided p-value tests b against , giving .
The discordant odds ratio is . Since the p-value (0.453) (0.05), we fail to reject : there is no significant change in the positive rate between before and after.
Note:
# Create a paired 2x2 contingency table
# Rows = Before outcome, Columns = After outcome
data <- matrix(c(4, 5, 2, 5), nrow = 2, byrow = TRUE,
dimnames = list(
Before = c("Positive", "Negative"),
After = c("Positive", "Negative")
))
# Perform McNemar's test (base R)
result <- mcnemar.test(data, correct = TRUE)
# Exact binomial version (recommended for small discordant counts)
# b and c are the off-diagonal (discordant) cells
b <- data[1, 2]
c <- data[2, 1]
exact <- binom.test(b, b + c, p = 0.5)
print(result)
print(exact)import numpy as np
from statsmodels.stats.contingency_tables import mcnemar
# Paired 2x2 table: rows = Before outcome, columns = After outcome
table = np.array([
[4, 5], # Before Positive -> (After Positive, After Negative)
[2, 5] # Before Negative -> (After Positive, After Negative)
])
# exact=True uses the exact binomial test (good for small samples)
# exact=False with correction=True uses chi-square with continuity correction
result = mcnemar(table, exact=True, correction=True)
print(f'statistic: {result.statistic}')
print(f'p-value: {result.pvalue:.4f}')Choosing the right test for categorical data:
When the number of discordant pairs (b + c) is small — commonly fewer than 25 — the chi-square approximation is unreliable, so the exact binomial test is recommended. This calculator reports both and marks the one it used.
The concordant cells (subjects whose outcome did not change) carry no information about change. McNemar's test asks only whether changes in one direction outnumber changes in the other, which depends solely on b and c.
It is b/c — the ratio of changes in one direction to changes in the other. A value above 1 indicates more flips from the first category to the second; below 1 indicates the reverse.