Understand the idea
Imagine surveying the same group of customers before and after they watch a product demo, asking a simple yes/no approval question each time. McNemar's test is built specifically for this before/after, same-subjects situation. Cross-tabulate the two answers and you get four groups: people who said no both times, people who said yes both times, people who switched from no to yes, and people who switched from yes to no.
Here is the key insight that makes this test different from an ordinary chi-square test: the people who DIDN'T change their answer (said no both times, or yes both times) carry ZERO information about whether opinions shifted; all the actual evidence lives in the two groups who SWITCHED. McNemar's test literally ignores the 'no change' cells and asks only whether the two switching directions (no-to-yes versus yes-to-no) are roughly balanced or meaningfully lopsided.
A classic trap worth remembering: a study can have thousands of subjects, with 95% of them giving identical answers both times, and McNemar's test result depends ENTIRELY on the handful who switched; two studies with identical switching counts give identical McNemar results no matter how many thousands of unchanged subjects sit in the background.
McNemar's test is sometimes called a paired chi-square test or a discordant pairs test, because it uses only the pairs whose answer changed. Cohen's g is the effect size for McNemar's test: how far the share of changes in one direction is from one half.
Practitioner explanation
KernelStats builds the 2x2 table (a=both negative, b=negative-to-positive switch, c=positive-to-negative switch, d=both positive) and computes chi2 = (|b-c|-1)^2/(b+c) with Edwards' continuity correction (the -1 term) via statsmodels' mcnemar. The choice between the asymptotic chi-square approximation and an EXACT binomial test is made AUTOMATICALLY based on the number of discordant pairs (b+c): the exact test (b ~ Binomial(b+c, 0.5) under H0) is used when discordant pairs number fewer than 25 (the common rule-of-thumb threshold), switching to the chi-square approximation with continuity correction above that; the card always states which route ('auto', 'exact', or 'asymptotic') was actually used, and a manual override is available for either route explicitly.
Two effect sizes are reported: the discordant odds ratio OR=b/c (how many no-to-yes switches occur per yes-to-no switch; OR=1 means no net directional change, and it is reported as UNDEFINED rather than a fabricated number when either discordant cell is exactly zero), and Cohen's g = |b/(b+c) - 0.5| (how far the split of discordant pairs departs from an even 50/50, with Cohen's 1988 guides of 0.05/0.15/0.25 for small/medium/large). A special degenerate case, zero discordant pairs at all (perfect agreement between the two measurements), is handled honestly with a valid card reporting no meaningful statistic (stat=0, p=1, explicitly labeled 'n/a, no discordant pairs') rather than an error or a misleading default number.
The product names the exact alternative test for each specific situation this test does NOT cover, rather than a generic pointer: independent (unpaired) groups need the ordinary chi-square test of independence instead; MORE than two categories on paired data need the Stuart-Maxwell / marginal-homogeneity test (which this function does not implement); and k related conditions on a continuous/ordinal outcome need the Friedman test.
When to use it
Consider it when
You have a yes/no (binary) outcome measured TWICE on the SAME subjects (before vs. after, treatment vs. control on matched pairs, two diagnostic tests on the same patients) and want to know whether the proportion answering 'yes' changed.
Use caution when
The two measurements are on independent, unpaired groups (use the ordinary chi-square test of independence instead), more than two outcome categories are involved on paired data (use the Stuart-Maxwell / marginal-homogeneity test), or there are more than two related conditions on a continuous/ordinal outcome (use the Friedman test).
Formulas and symbols
McNemar's chi-square statistic (with continuity correction)
b, c the two discordant cells (switches in each direction). Used for the asymptotic route when discordant pairs number 25 or more.
Discordant odds ratio
How many no-to-yes switches occur per yes-to-no switch. Undefined when either discordant cell is zero.
Cohen's g
How far the discordant split departs from an even 50/50. Cohen's 1988 guides: 0.05 small, 0.15 medium, 0.25 large.
Assumptions and checks
- McNemar's test is only valid for matched pairs of the same two-category outcome measured twice on the same subject (e.g. before/after); applying it to unrelated groups, or to a non-binary outcome, produces a meaningless result.
In KernelStats: Not checked by KernelStats as a statistical test: assumed from the design; the card states this directly.
- Independence of observations. McNemar's test assumes different subjects/pairs are independent of one another; correlated pairs (e.g. subjects from the same family or cluster) invalidate the test's standard error.
In KernelStats: Not checked by KernelStats: assumed from the study design; stated directly as a standing reminder.
- With fewer than about 25 discordant pairs, the chi-square approximation to McNemar's statistic is unreliable.
In KernelStats: Checked directly: the card counts the discordant pairs and automatically switches to the exact binomial test below that threshold rather than reporting an unreliable chi-square approximation.
Alternatives
- Chi-Square Test of Independence. The two measurements come from independent, unpaired groups rather than the same subjects measured twice.
- Friedman Test. There are more than two related conditions and the outcome is continuous or ordinal, rather than exactly two paired binary measurements.
Worked example
Scenario
Surveying 200 customers' product approval (yes/no) before and after watching a demo: 140 said yes both times, 30 said no both times, 22 switched from no to yes, 8 switched from yes to no.
Result
b=22, c=8, n_discordant=30 (>= 25, asymptotic route with continuity correction), chi2(1) = (|22 - 8| - 1)^2 / 30 = 5.63, p = .018; discordant odds ratio = 2.75; Cohen's g = 0.23 (approaching large).
Interpretation
Of the 30 customers whose answer changed after watching the demo, 22 switched from disapproving to approving and 8 the other way: the data give evidence that more changed in one direction than the other (p = .018), and the effect (Cohen's g near 0.25) is substantial among those who did switch; the 170 customers whose opinion never changed contributed nothing to this result, exactly as the test is designed.
Common misconceptions
Misconception
A large number of subjects who gave the same answer both times (agreement) provides strong evidence that nothing really changed.
Correction
The two concordant cells (subjects who did not change their answer) carry NO information about change at all in McNemar's test: the test's result depends entirely on the two discordant cells (the subjects who DID switch). Two studies with identical discordant counts give identical McNemar results regardless of how many thousands of subjects never changed their answer.
Misconception
The McNemar chi-square test with continuity correction is always the appropriate route to use, regardless of sample size.
Correction
When the number of discordant pairs is small (a common rule of thumb is fewer than 25), the chi-square approximation becomes unreliable, and an EXACT binomial test is the appropriate choice instead. This card switches to the exact test automatically in that situation and states clearly which route was actually used.
References
- Note on the sampling error of the difference between correlated proportions or percentages, McNemar, Q. (1947), Psychometrika, 12(2), 153-157.
- Note on the 'correction for continuity' in testing the significance of the difference between correlated proportions, Edwards, A. L. (1948), Psychometrika, 13(3), 185-187.
