McNemar Calculator

Testing Change in Paired Categorical Data

McNemar's test answers a specific before/after question for binary outcomes: among subjects whose classification changed between two measurements, did it change more often in one direction than the other? It's built for paired nominal data laid out in a 2×2 table — the same subjects measured twice — and it deliberately ignores subjects whose classification stayed the same, focusing entirely on the discordant pairs.

The Formula

The test uses a continuity-corrected version when discordant pairs are few, switching to the uncorrected form for larger counts:

With continuity correction (b + c < 25):
χ² = (|b − c| − 1)² / (b + c)

Without correction (b + c ≥ 25):
χ² = (b − c)² / (b + c)

df = 1

Here b and c are the two discordant cells — subjects who changed from one category to the other, in each direction.

Worked Example

Suppose 15 subjects changed from "No" to "Yes" (b = 15) and 5 changed from "Yes" to "No" (c = 5) after an intervention:

McNemar's test result, b = 15, c = 5
QuantityValue
b + c20 (continuity correction applied)
χ²4.05
df1
p-value0.0442

Computed directly from the calculator's continuity-corrected formula.

At p = 0.044, the shift is statistically significant at the conventional 0.05 level — the change from "No" to "Yes" outpaced the reverse by more than chance would predict.

Where This Calculation Matters

  • Pre/post treatment studies — testing whether a treatment shifts subjects from a negative to positive diagnosis (or vice versa) more than chance alone would explain.
  • Matched-pairs survey research — comparing the same respondents' yes/no answers before and after an event, campaign, or intervention.
  • Diagnostic test agreement — checking whether two diagnostic methods applied to the same subjects disagree more in one direction than the other.

How to Use This Calculator

  1. Enter the count for discordant pair b (e.g., Yes before, No after).
  2. Enter the count for discordant pair c (e.g., No before, Yes after).
  3. Select Calculate to get the chi-square statistic and p-value.

Related Calculations

For unpaired categorical comparisons, use the Chi-Square Calculator. For paired continuous measurements, see the Paired T-Test Calculator.

Principles of Paired Categorical Statistics: McNemar's Test

A McNemar's test calculator performs a statistical hypothesis test on paired nominal, dichotomous data arranged in a 2×2 Contingency Table. In epidemiology, medical diagnostic testing, and election polling, McNemar's test evaluates changes in paired binary outcomes (e.g., Disease Positive vs. Negative, Yes vs. No) before and after an intervention on the same subjects.

The 2×2 Paired Contingency Table Structure

Pre-Intervention Status Post-Intervention: Positive (+) Post-Intervention: Negative (-) Total
Pre: Positive (+) a (Concordant +/+) b (Discordant +/- : Lost) a + b
Pre: Negative (-) c (Discordant -/+ : Gained) d (Concordant -/-) c + d
Total a + c b + d N = a + b + c + d

The Fundamental McNemar Test Formulas

Standard Chi-Square Statistic: χ2 = ( b - c )2 / ( b + c )  (df = 1)
With Edwards / Yates Continuity Correction: χ2 = ( | b - c | - 1 )2 / ( b + c )
Note: Concordant cells (a and d) contain zero change information and are mathematically excluded from the test statistic.

Exact Binomial Test for Small Samples (b + c < 25)

When the total number of discordant switching pairs (b + c) is less than 25, the Chi-square approximation loses accuracy. Statisticians apply the Exact Binomial Distribution with success probability p = 0.50 on discordant counts.

Step-by-Step Worked Calculation Example

Example: Evaluating Efficacy of an Educational Campaign on Vaccine Uptake

Problem: 100 hesitant individuals are surveyed before and after an educational workshop:
Cell a (Willing Before & After) = 20  |  Cell b (Willing Before → Hesitant After) = 5
Cell c (Hesitant Before → Willing After) = 35  |  Cell d (Hesitant Before & After) = 40
Total N = 100. Test if the campaign significantly shifted attitudes at α = 0.05 (χ2crit for df = 1 is 3.841).

Step 1: Identify Discordant Pairs:

b = 5 (Lost)  |  c = 35 (Gained) &implies; Total Discordant = 5 + 35 = 40

Step 2: Calculate McNemar Chi-Square Statistic with Continuity Correction:

χ2 = ( | 5 - 35 | - 1 )2 / ( 5 + 35 ) = ( 30 - 1 )2 / 40 = ( 29 )2 / 40 = 841 / 40 = 21.025

Step 3: Evaluate Statistical Significance:

Calculated χ2 = 21.025 > Critical χ2 (3.841) &implies; p < 0.0001 (Highly Significant)

Conclusion: The workshop produced a statistically significant positive shift in vaccine acceptance.

Paired Odds Ratio (OR) and Relative Risk in McNemar Analysis

In clinical epidemiology, beyond testing for statistical significance, McNemar's test provides the Paired Odds Ratio (OR) comparing switching probabilities:

Paired Odds Ratio: OR = b / c
Where b represents positive-to-negative discordant switches and c represents negative-to-positive discordant switches.
  • OR = 1.0: Symmetric discordant transitions — intervention has zero net effect.
  • OR > 1.0 (or < 1.0): Strong asymmetric directional treatment effect.

Extension: The Stuart-Maxwell Test for K×K Contingency Tables

When evaluating categorical variables with more than 2 paired outcome categories (e.g., 3-point rating scales: Worse, Same, Better), McNemar's test generalizes to the Stuart-Maxwell Test for Marginal Homogeneity using matrix quadratic algebra.

Application in Machine Learning Classifier Comparison

In computer science and machine learning research (Dietterich's 5×2 CV standard), McNemar's test is the gold standard for comparing two supervised ML algorithms:

  • Cell b: Test cases classified correctly by Model 1 but misclassified by Model 2.
  • Cell c: Test cases misclassified by Model 1 but classified correctly by Model 2.
  • If McNemar's p < 0.05, one machine learning model is statistically superior to the competing baseline.

Confidence Intervals for Paired Difference in Proportions

To quantify the precision of an observed percentage shift in paired binary data, statisticians compute the Wald Paired Difference Confidence Interval:

Δp = ( b - c ) / N
SE(Δp) = √[ ( ( b + c ) - ( b - c )2 / N ) ] / N
95% CI = Δp ± 1.96 · SE(Δp)

Diagnostic Sensitivity and Specificity Comparison

In pathology and laboratory medicine, McNemar's test evaluates whether two diagnostic test assays (e.g., Rapid Antigen vs. PCR) exhibit statistically different Diagnostic Sensitivities when both tests are evaluated on the exact same patient blood samples against an established clinical gold standard.

Handling Structural Zeros and Small Sample Sparsity

When discordant cell counts are extremely sparse (e.g., b + c < 10):

Asymptotic Chi-square approximations produce excessive Type I error rates. Researchers must report exact two-tailed binomial p-values calculated by summing discrete binomial probabilities across all equal or more extreme discordant outcomes.

Medical Retrospective Case-Control Matching

In cancer epidemiology research, retrospective 1:1 case-control studies match individual oncology patients with healthy control subjects on age and smoking history, using McNemar's test to evaluate paired environmental exposure odds ratios.

A/B Website User Experience Re-Testing

In digital UX design, researchers apply McNemar's test to evaluate whether returning website visitors successfully complete redesigned navigation tasks that they previously failed on the legacy website interface.

Software Implementation in Python and R

In statistical programming, McNemar's test is executed via statsmodels.stats.contingency_tables.mcnemar() in Python and mcnemar.test() in R.