McNemar's Test

Introduction
McNemar's Test checks whether a binary outcome changed significantly for the same subjects measured at two points in time, or under two matched conditions. If you are asking questions like "did patients' test results change after treatment?", "did voters switch their yes/no opinion after watching the debate?", or "do two raters agree on a pass/fail decision for the same items?"-and each subject contributes exactly one before-and-after pair on a two-category outcome-this is usually the test you are looking for.

By the end of this article you will be able to state exactly when McNemar's Test applies, compute one completely by hand on two worked examples-a small classic 2x2 table and a larger sample-interpret the result alongside the odds ratio effect size, know when to switch to the exact binomial version, and run the same test in a few lines of Python with statsmodels.stats.contingency_tables.mcnemar.
What Is McNemar's Test?
McNemar's Test compares the two discordant cells of a paired 2x2 contingency table-the subjects who switched from one category to the other-against each other. If a binary outcome were truly unaffected by whatever happened between the two measurements, subjects should be roughly as likely to switch from "Yes" to "No" as from "No" to "Yes." The bigger the imbalance between those two switching counts, the stronger the evidence that something systematically shifted the outcome in one direction.
It exists because a great deal of real-world before/after and matched-pair data is binary rather than continuous-pass/fail, improved/not improved, agree/disagree, converted/not converted-and the paired t-test, built for continuous paired differences, simply does not apply. McNemar's Test fills that gap directly, working entirely with paired categorical outcomes.
When to Use It
Use McNemar's Test when you have a single binary outcome measured twice on the same subjects-or on matched pairs-and you want to know whether the outcome changed asymmetrically between the two measurements. Unlike the Chi-Square Test of Independence, the two "variables" here are really the same variable measured at two time points or under two conditions, and the subjects are not independent across the table-they are linked pair by pair.
| Scenario | Measurement 1 (before / condition A) | Measurement 2 (after / condition B) |
|---|---|---|
| Clinical treatment study | Disease present before treatment (Yes / No) | Disease present after treatment (Yes / No) |
| Political opinion shift | Supports policy before debate (Yes / No) | Supports policy after debate (Yes / No) |
| Marketing A/B redesign | Converted on old landing page (Yes / No) | Converted on new landing page (Yes / No) |
| Diagnostic test comparison | Test A result (Positive / Negative) | Test B result on the same patient (Positive / Negative) |
Key Assumptions
- Paired (dependent) observations: each row of the data is the same subject, or a matched pair, measured under both conditions-no subject appears in only one measurement.
- Binary outcome: the outcome variable must have exactly two categories at each measurement (e.g., Yes/No, Positive/Negative, Pass/Fail).
- Independence across pairs: different subjects' pairs must be independent of one another-one subject's before/after result gives no information about another subject's.
- Sufficiently large discordant count: the number of discordant pairs \( (b + c) \) should generally be 25 or more for the chi-square approximation to be reliable-see the exact test section below for smaller samples.
Hypotheses
McNemar's Test formally tests whether switching between the two outcome categories is symmetric:
- Null Hypothesis (\( H_0 \)): the probability of switching from category 1 to category 2 equals the probability of switching from category 2 to category 1-any imbalance observed between the two discordant cells is due to chance (this is often called marginal homogeneity).
- Alternative Hypothesis (\( H_1 \)): the two switching probabilities are not equal-the outcome shifted systematically in one direction more than the other.
(A significant McNemar's Test tells you the switching was lopsided, but on its own it does not tell you the size of that imbalance in practical terms-pairing the p-value with the odds ratio effect size below gives a fuller picture.)
The Formula, Explained
For \( N \) paired subjects, each measured on the same binary outcome twice, arrange the results into a 2x2 table:
| After: Positive | After: Negative | |
|---|---|---|
| Before: Positive | a | b |
| Before: Negative | c | d |
Here \( a \) and \( d \) are the concordant cells (no change), while \( b \) and \( c \) are the discordant cells (switched from one category to the other). The procedure is:
- Identify the two discordant cells: \( b \) (Positive then Negative) and \( c \) (Negative then Positive).
- Ignore the concordant cells \( a \) and \( d \) entirely-they carry no information about directional switching.
- Compare \( b \) against \( c \): if the true switching rates in both directions were equal, \( b \) and \( c \) should be close to each other.
The (Yates-corrected) test statistic commonly reported is:
\[ \chi^2 = \frac{(|b - c| - 1)^2}{b + c} \]Some software and textbooks instead report the uncorrected version:
\[ \chi^2 = \frac{(b - c)^2}{b + c} \]Under \( H_0 \), \( \chi^2 \) follows approximately a chi-square distribution with 1 degree of freedom. Intuitively, the further \( b \) and \( c \) drift apart relative to their total, the stronger the evidence that switching happened preferentially in one direction.
Worked Example 1: A 2x2 Table by Hand
Suppose a clinic tests 100 patients for a condition, treats them, then re-tests the same 100 patients afterward. The paired before/after results are:
| After: Positive | After: Negative | Row Total | |
|---|---|---|---|
| Before: Positive | a = 20 | b = 30 | 50 |
| Before: Negative | c = 10 | d = 40 | 50 |
| Column Total | 30 | 70 | 100 |
Step 1: Identify the discordant cells
\( b = 30 \) patients tested Positive before and Negative after (improved), while \( c = 10 \) patients tested Negative before and Positive after (worsened). The concordant cells \( a = 20 \) and \( d = 40 \) are set aside.
Step 2: Compute the test statistic
\[ \chi^2 = \frac{(|b - c| - 1)^2}{b + c} = \frac{(|30 - 10| - 1)^2}{30 + 10} = \frac{19^2}{40} = \frac{361}{40} = 9.025 \]Step 3: Compare against the chi-square distribution
With \( df = 1 \), the critical value at \( \alpha = 0.05 \) is \( \chi^2_{crit} = 3.841 \). Since \( 9.025 > 3.841 \), the result is statistically significant. The exact p-value works out to approximately \( p \approx 0.00266 \).
Step 4: Conclusion
Since \( p < 0.05 \), we reject \( H_0 \). Significantly more patients switched from Positive to Negative (improved) than from Negative to Positive (worsened)-the treatment appears associated with a real, directional shift in test outcome, not merely random fluctuation between the two measurements.
Worked Example 2: A Larger Sample
Now suppose a UX team A/B-tests a redesigned checkout flow on 500 returning customers, recording whether each customer converted (completed a purchase) on the old flow and again on the new flow:
| New Flow: Converted | New Flow: Not Converted | Row Total | |
|---|---|---|---|
| Old Flow: Converted | a = 180 | b = 40 | 220 |
| Old Flow: Not Converted | c = 85 | d = 195 | 280 |
| Column Total | 265 | 235 | 500 |
Step 1: Identify the discordant cells
\( b = 40 \) customers converted on the old flow but not on the new flow, while \( c = 85 \) customers did not convert on the old flow but did convert on the new flow.
Step 2: Compute the test statistic
\[ \chi^2 = \frac{(|b - c| - 1)^2}{b + c} = \frac{(|40 - 85| - 1)^2}{40 + 85} = \frac{44^2}{125} = \frac{1936}{125} = 15.488 \]Step 3: Compare against the chi-square distribution
With \( df = 1 \), \( \chi^2 = 15.488 \) far exceeds the critical value of 3.841, giving \( p \approx 0.0000834 \).
Step 4: Conclusion
Since \( p < 0.05 \), we reject \( H_0 \). Significantly more customers switched from "not converted" to "converted" than the reverse-the new checkout flow is associated with a real net gain in conversions among customers whose behavior changed at all. Both hand calculations above are verified against Python in the Python section below.
Effect Size: Odds Ratio
Like the chi-square statistic elsewhere on this site, McNemar's \( \chi^2 \) grows with sample size even for a modest underlying imbalance, so it should be paired with an effect size. For paired binary data, the conventional choice is the odds ratio of the two discordant cells:
\[ OR = \frac{b}{c} \]This is the ratio of "switched one way" to "switched the other way." An odds ratio of 1 means switching was perfectly balanced (matching \( H_0 \)); values further from 1 in either direction indicate a stronger directional effect.
For Worked Example 1:
\[ OR = \frac{b}{c} = \frac{30}{10} = 3.0 \]Patients were three times as likely to switch from Positive to Negative as the reverse. For Worked Example 2:
\[ OR = \frac{b}{c} = \frac{40}{85} \approx 0.47 \]Customers were roughly half as likely to switch from "converted" to "not converted" as the reverse-i.e., switching favored conversion on the new flow, consistent with the significant, positive result above. An approximate 95% confidence interval for the log odds ratio uses:
\[ \ln(OR) \pm 1.96 \sqrt{\frac{1}{b} + \frac{1}{c}} \]exponentiating the resulting bounds back to the odds ratio scale.
Exact McNemar Test for Small Samples
When the number of discordant pairs \( (b + c) \) is small-conventionally below 25-the chi-square approximation becomes unreliable, mirroring the same small-sample caveat the Chi-Square Test of Independence faces with Fisher's Exact Test. The fix here is the exact McNemar test, which treats the discordant count \( b \) as a draw from a Binomial\( (n = b+c,\ p = 0.5) \) distribution under \( H_0 \), and computes an exact two-sided p-value directly from that binomial distribution rather than relying on the chi-square approximation.
For example, with only \( b = 2 \) and \( c = 9 \) discordant pairs (\( b + c = 11 \), well below 25), the exact binomial p-value is the more trustworthy choice over the chi-square approximation, which statsmodels.stats.contingency_tables.mcnemar computes automatically when its exact=True argument is used-see the Python section below.
Python Example
You can run this test in a couple of lines using statsmodels.stats.contingency_tables.mcnemar:
import numpy as np
from statsmodels.stats.contingency_tables import mcnemar
# Worked Example 1: Before (rows) vs After (columns) test result
table = np.array([
[20, 30], # Before: Positive -> After: Positive, After: Negative
[10, 40] # Before: Negative -> After: Positive, After: Negative
])
# Chi-square version with Yates' continuity correction (default)
result = mcnemar(table, exact=False, correction=True)
print(f"Chi-square statistic: {result.statistic:.3f}")
print(f"p-value: {result.pvalue:.6f}")
# Odds ratio effect size
b, c = table[0, 1], table[1, 0]
odds_ratio = b / c
print(f"Odds ratio (b/c): {odds_ratio:.3f}")
Output:
Chi-square statistic: 9.025
p-value: 0.002662
Odds ratio (b/c): 3.000
These figures match Worked Example 1 exactly. For Worked Example 2:
table_2 = np.array([
[180, 40], # Old Flow: Converted -> New Flow: Converted, Not Converted
[85, 195] # Old Flow: Not Converted -> New Flow: Converted, Not Converted
])
result_2 = mcnemar(table_2, exact=False, correction=True)
print(f"Chi-square statistic: {result_2.statistic:.3f}")
print(f"p-value: {result_2.pvalue:.8f}")
Chi-square statistic: 15.488
p-value: 0.00008338
Both match the hand-calculated values above exactly. For the small-sample case discussed in the exact test section, switch to the exact binomial version by setting exact=True:
small_table = np.array([
[50, 2], # b = 2 discordant
[9, 60] # c = 9 discordant
])
exact_result = mcnemar(small_table, exact=True)
print(f"Exact p-value: {exact_result.pvalue:.4f}")
# Compare against the (less reliable) chi-square approximation
approx_result = mcnemar(small_table, exact=False, correction=True)
print(f"Chi-square-approx p-value: {approx_result.pvalue:.4f}")
Exact p-value: 0.0654
Chi-square-approx p-value: 0.0455
Notice the two methods disagree on statistical significance at \( \alpha = 0.05 \) precisely because \( b + c = 11 \) is well below the recommended threshold of 25-exactly the scenario where the exact test is the trustworthy choice.
How to Interpret Results
A significant McNemar's Test tells you that switching between the two outcome categories was asymmetric- significantly more subjects moved in one direction than the other. It does not, by itself, tell you the practical size of that imbalance; report the odds ratio (or, equivalently, \( b \) and \( c \) directly) alongside the p-value so readers can judge both statistical and practical significance, exactly as Worked Example 1 and Worked Example 2 demonstrate above.
It is also worth explicitly reporting the concordant cells \( a \) and \( d \), even though they play no role in the test statistic-they show how many subjects were unaffected altogether, which is often the majority of the sample and important context for interpreting the discordant switching.
Checking Assumptions in Practice
Before running McNemar's Test, confirm that every row of your data really does represent the same subject (or a deliberately matched pair) measured under both conditions-mistakenly applying McNemar's Test to two independent groups will produce a misleading result, since the test's whole logic depends on the pairing.
Next, tally \( b + c \), the total discordant count. If it is comfortably above 25, the chi-square version (with or without Yates' correction) is appropriate. If it is smaller, switch to the exact binomial version, shown in the Python section above, to avoid an unreliable p-value.
Advantages
- Purpose-built for paired binary data, correctly accounting for the dependence between the two measurements rather than treating them as independent samples.
- Simple to compute by hand-only the two discordant cells matter, making the logic easy to explain to non-technical audiences.
- Has a well-established exact version for small discordant counts, avoiding the large-sample pitfalls that plague the chi-square approximation in that regime.
- Pairs naturally with an intuitive effect size (the odds ratio of the discordant cells) for reporting practical significance alongside the p-value.
Limitations
- Only applies to a strictly binary outcome-ordinal or multi-category paired outcomes require a different test, such as the Stuart-Maxwell test or the Friedman Test.
- Requires a sufficiently large discordant count (conventionally 25 or more) for the chi-square approximation to be trustworthy-otherwise the exact binomial version is necessary.
- Ignores the concordant cells entirely, so it says nothing about the overall proportion of subjects who changed versus stayed the same-only about the direction of change among those who did switch.
- Like other chi-square-based tests, a significant result establishes asymmetric switching but not its practical magnitude, making an accompanying effect size such as the odds ratio essential.
When NOT to Use It
- Chi-Square Test of Independence: use instead when the two categorical measurements come from independent subjects rather than the same subjects measured twice.
- Paired T-Test or Wilcoxon Signed-Rank Test: use instead when the paired outcome is continuous (or ordinal with meaningful magnitude) rather than binary.
- Cochran's Q Test: use instead when you have three or more related binary measurements on the same subjects, rather than exactly two.
- Stuart-Maxwell test: use instead when the paired outcome has more than two categories rather than exactly two.
McNemar's Test vs Chi-Square Test of Independence vs Paired T-Test vs Cochran's Q
17.1 McNemar's Test vs Chi-Square Test of Independence
| Aspect | McNemar's Test | Chi-Square Test of Independence |
|---|---|---|
| Sample structure | The same subjects measured on a binary outcome twice | Independent subjects cross-classified on two variables |
| Typical use | Change in a binary outcome before vs after an intervention | Association between two categorical variables |
| Table cells used | Only the discordant (off-diagonal) cells drive the test | All cells contribute |
17.2 McNemar's Test vs Paired T-Test
| Aspect | McNemar's Test | Paired T-Test |
|---|---|---|
| Outcome type | Binary (two categories) | Continuous, approximately normal differences |
| What is compared | Counts of directional switches (\( b \) vs \( c \)) | Mean of the paired differences |
| Test statistic distribution | Chi-square with 1 degree of freedom | t-distribution with \( n - 1 \) degrees of freedom |
17.3 McNemar's Test vs Cochran's Q Test
| Aspect | McNemar's Test | Cochran's Q Test |
|---|---|---|
| Number of related measurements | Exactly two | Three or more |
| Outcome type | Binary | Binary |
| Relationship | Special case of Cochran's Q with only two conditions | Generalizes McNemar's Test to more than two conditions |
Common Misconceptions
- "McNemar's Test is just the Chi-Square Test of Independence applied to a 2x2 table." No-the two tests use the data completely differently. McNemar's Test uses only the discordant cells \( b \) and \( c \); the Chi-Square Test of Independence uses all four cells and assumes independent subjects rather than paired ones.
- "A significant McNemar's result proves the intervention caused the change." No-the test only detects asymmetric switching; without a proper control group or study design, confounding factors unrelated to the intervention could also produce the observed asymmetry.
- "The concordant cells don't matter at all, so I can ignore them in reporting." They don't enter the test statistic, but reporting \( a \) and \( d \) still matters for context-they show how many subjects were unaffected, which is often the majority of the sample.
- "McNemar's Test works fine with any number of discordant pairs." It requires a sufficiently large discordant count-typically 25 or more-to trust the chi-square approximation; smaller samples call for the exact binomial version instead.
- "A non-significant result proves nothing changed." Failing to reject \( H_0 \) only means there was not enough evidence of asymmetric switching with this sample-it does not prove the true switching probabilities are exactly equal.
Interview Questions
- Walk through why McNemar's Test uses only the discordant cells of a 2x2 table and ignores the concordant cells.
- Why does McNemar's Test require the number of discordant pairs \( (b + c) \) to be at least around 25, and what happens to the test's reliability when this is violated?
- Explain the difference between McNemar's Test and the Chi-Square Test of Independence, particularly in terms of sample structure.
- What does it mean for McNemar's Test to detect "asymmetric switching," and how does the odds ratio \( b/c \) help quantify that?
- Under what circumstances would you switch from the chi-square version of McNemar's Test to the exact binomial version?
- How would you generalize McNemar's Test to a study with three or more related binary measurements per subject?
- What does Yates' continuity correction adjust for in McNemar's Test, and why is the correction commonly applied by default?
- Why can a significant McNemar's Test result not, by itself, be used to claim that an intervention caused the observed change?
- Explain what marginal homogeneity means in the context of McNemar's null hypothesis.
- How would you extend McNemar's Test logic to a paired outcome with more than two categories, and what test would you reach for instead?
Frequently Asked Questions
- McNemar's Test is used to determine whether a binary outcome-such as pass/fail, yes/no, or improved/not improved-changed significantly for the same subjects measured at two points in time, or under two matched conditions, such as before and after an intervention, or two raters scoring the same items.
- Arrange the paired outcomes into a 2x2 table of before-vs-after (or condition A vs condition B) results. Label the two discordant cells b (positive then negative) and c (negative then positive)-the concordant cells a and d are ignored. The test statistic is chi-square = (b - c)^2 / (b + c), following approximately a chi-square distribution with 1 degree of freedom under the null hypothesis that switching happens equally often in both directions.
- McNemar's Test applies to paired, dependent samples-the same subjects contribute both measurements-and its statistic depends only on the two off-diagonal discordant cells. The Chi-Square Test of Independence applies to independent subjects cross-classified on two separate categorical variables, and its statistic uses every cell in the table.
- The two measurements must come from the same subjects or from matched pairs, the outcome must be binary, each pair must be independent of every other pair, and the total number of discordant pairs (b + c) should generally be at least 25 for the chi-square approximation to hold reliably-below that, the exact binomial version of the test is the standard alternative.
- If the p-value is below your chosen significance level (commonly alpha = 0.05), you reject the null hypothesis of symmetric switching and conclude the outcome changed significantly more in one direction than the other. If the p-value is at or above alpha, there is not enough evidence that the direction of switching was asymmetric.
- Use the exact McNemar test, which computes an exact p-value from the binomial distribution using the discordant count b and total discordant pairs (b + c), whenever (b + c) is small-conventionally below 25-since the chi-square distribution's large-sample approximation becomes unreliable in that situation.
- No. McNemar's Test deliberately ignores the two concordant cells (subjects who gave the same answer both times) because they carry no information about directional change. Only the two discordant cells-where a subject's answer switched-drive the test statistic.
- The standard McNemar's Test compares exactly two related binary measurements. When there are three or more related measurements on a binary outcome for the same subjects, Cochran's Q Test is the standard generalization; for an ordinal outcome with more than two related measurements, the Friedman Test is typically used instead.
Key Takeaways
- McNemar's Test checks whether a binary outcome changed asymmetrically for the same subjects measured twice, by comparing the two discordant cells of a paired 2x2 table.
- It is ideal for before/after studies, matched-pair comparisons, and any two-condition binary comparison on the same subjects-where the Chi-Square Test of Independence's independence assumption does not hold.
- Core assumptions: paired dependent observations, a strictly binary outcome, independence across pairs, and a discordant count generally at or above 25-when violated, the exact binomial version is the standard fallback.
- In Python,
statsmodels.stats.contingency_tables.mcnemar(...)gets you the chi-square statistic and p-value in one call, with anexact=Trueoption for small discordant counts; pair it with the odds ratio \( b/c \) for a complete effect size picture. - If \( p < 0.05 \), switching between categories was significantly asymmetric; if \( p \geq 0.05 \), there isn't enough evidence of asymmetric switching-but the size of the discordant counts, not just the p-value, determines whether the chi-square approximation or the exact test is appropriate.
- Always report the discordant counts \( b \) and \( c \) (and ideally the odds ratio) alongside the p-value, and switch to the exact test whenever \( b + c \) is small, as demonstrated in the worked examples above.
McNemar's Test earns its place as the standard tool for paired binary comparisons precisely because so much real-world before/after data-diagnostic test results, yes/no opinions, pass/fail outcomes, converted/not converted-comes from the same subjects measured twice rather than from two independent groups. Its logic is refreshingly direct: build a paired 2x2 table, ignore the subjects who didn't change, and measure how lopsided the switching was between the two possible directions.
The two worked examples above, a small clinical before/after table and a larger checkout-conversion A/B comparison, show both ends of this test's range, and a useful lesson in between: what matters for choosing between the chi-square approximation and the exact binomial version is not the total sample size \( N \), but specifically the discordant count \( b + c \). Reporting the chi-square statistic and p-value alongside the odds ratio effect size, and switching to the exact test whenever discordant pairs are few, as demonstrated throughout this guide, gives a complete and honest picture that the omnibus p-value by itself cannot.