Matched categories: McNemar and its relatives
When the same people answer twice, only the people who changed carry information. McNemar, Cochran's Q, Stuart–Maxwell, Bhapkar and Bowker build on that idea.
What runs in CodeSplash
- STAT064McNemar test Runs in CodeSplash
- STAT065Cochran Q test for matched binary outcomes Runs in CodeSplash
- STAT066Stuart–Maxwell test Runs in CodeSplash
- STAT067Bhapkar test Runs in CodeSplash
- STAT068Bowker symmetry test Runs in CodeSplash
- McNemar's test (paired proportions) Verified in Python and R
- Cochran's Q test (several paired proportions) Verified in Python and R
- Symmetry and marginal homogeneity (Stuart–Maxwell, Bhapkar, Bowker) Verified in Python and R
You will be able to
- Compute McNemar's statistic from the discordant pairs
- Extend to several matched binary measures with Cochran's Q
- Tell marginal homogeneity from symmetry in a square table
Only changers count
Read1,600 voters rated a leader twice. The table has four kinds of people:
| First \ Second | Approve | Disapprove |
|---|---|---|
| Approve | 794 | 150 |
| Disapprove | 86 | 570 |
People who said the same thing both times tell you nothing about change. If approval were stable, the 236 who changed their minds should split evenly between the two directions.
Compute it
PredictPredict first
McNemar's statistic is . With = 150 and = 86, what is it? Drag to your estimate.
Make your guess between 0 and 30 McNemar χ², then reveal.
Reveal
= 17.36, p < .001. Approval fell from 0.59 to 0.55.
See the discordant pairs
ExploreChange the agreeing cells and notice that nothing happens. Then move people between the discordant cells. Only those change the result, however many agree.
EExplorable · McNemar's discordant pairs
For paired yes/no data, which pairs carry the evidence?
Add 1,000 people who answered yes both times. What happens to the p-value?
| Yes after | No after | |
|---|---|---|
| Yes before | 40 no change | 18 changed |
| No before | 6 changed | 36 no change |
Only the highlighted pairs count. p = .025 whatever the gray cells hold. The change in proportions is 12.0 points out of 100 people.
Commit your prediction above to see the result
Make your prediction to see the result.
Few changers
ReadWith 10 changers, 8 one way and 2 the other, the χ² approximation gives p = .058 and the exact binomial gives p = .109. CodeSplash switches to exact automatically when there are fewer than 25 changers. A mid-p option sits between them: p = .065.
Several measures
ReadCochran's QCochran's Q testTests whether yes/no outcomes differ across three or more matched conditions, or models scored on the same rows. extends McNemar to three or more matched binary outcomes. 46 patients each tried three drugs, responding or not:
Q(2) = 8.47, p = .014. Only the 34 patients with mixed responses carry information; follow up with pairwise McNemar tests.
Square tables
ReadWhen each person falls into one of categories, twice, two different questions arise.
Marginal homogeneity: did the overall distribution change? Stuart–Maxwell and Bhapkar test that.
Symmetry: are moves from A to B as common as moves from B to A, for every pair? Bowker tests that. Symmetry is stronger: it implies marginal homogeneity, but not the reverse.
For 7,477 women's right- and left-eye vision: Stuart–Maxwell 11.96 on 3 df, p = .008. Bhapkar 11.98, p = .007. Bowker 19.11 on 6 df, p = .004.
Check your understanding
CheckpointCheck yourself
Discordant cells are b = 30 and c = 10. What is McNemar's χ², without continuity correction?
Show the answer
Answer: 10
(30 − 10)² / (30 + 10) = 400 / 40 = 10.
Check yourself
You add 1,000 more people who answered the same way twice. What happens to McNemar's p-value?
Show the answer
Answer: a
Concordant pairs don't enter the statistic. They do matter for how you report the change in proportions, since the denominator grows.
Check yourself
You want to know whether the overall mix of brands changed between two purchases, whatever individuals did. Which test fits?
Show the answer
Answer: a
The question is about the margins, so test marginal homogeneity. An independence test on this table would ask whether the purchases are related, which they almost always are.
Check yourself
A study has 500 pairs, of which 12 are discordant. Which p-value should you trust most?
Show the answer
Answer: a
Information comes from the discordant pairs, so the effective sample is 12. CodeSplash's automatic setting goes exact below 25 discordant pairs.
Check yourself
Each patient tries 4 treatments, responding or not. How many degrees of freedom does Cochran's Q have?
Show the answer
Answer: 3
Number of treatments minus 1: 4 − 1 = 3.
Check yourself
Which patients carry information in Cochran's Q?
Show the answer
Answer: c
As with McNemar's test, someone who gives the same answer throughout says nothing about differences between treatments.
Check yourself
Cochran's Q is significant across three drugs. What comes next?
Show the answer
Answer: a
Q says only that the drugs aren't all alike. The follow-up tests must respect the pairing, and they need an adjustment because there are several of them.
Check yourself
How many degrees of freedom does Bowker's test have on a 4 × 4 table?
Show the answer
Answer: 6
One for each pair of categories: 4 × 3 / 2 = 6.
Check yourself
Bowker's test rejects, but Stuart–Maxwell doesn't. What does that suggest?
Show the answer
Answer: a
Symmetry is the stronger claim. Unbalanced swaps between categories can cancel in the margins, leaving the overall distribution unchanged.
Do it in CodeSplash
In the appEach method takes a First measurement and Second measurement, except Cochran's Q, which takes several Measurements. Symmetry's Main test picks the headline, and the card reports all three. Data need one row per person.
import numpy as np
from statsmodels.stats.contingency_tables import SquareTable, mcnemar
print(mcnemar(np.array([[794, 150], [86, 570]]), exact=False, correction=False))
table = np.array([[1520, 266, 124, 66], [234, 1512, 432, 78], [117, 362, 1772, 205], [36, 82, 179, 492]])
print(SquareTable(table).homogeneity(method="stuart_maxwell"))
print(SquareTable(table).symmetry())mcnemar.test(matrix(c(794, 86, 150, 570), 2), correct = FALSE)
table <- matrix(c(1520, 234, 117, 36, 266, 1512, 362, 82, 124, 432, 1772, 179, 66, 78, 205, 492), 4)
mcnemar.test(table) # Bowker's test of symmetry
# install.packages("DescTools")
DescTools::StuartMaxwellTest(table)Recap
Recap- Paired categorical tests use only the people who changed.
- McNemar handles two binary measures; Cochran's Q handles several.
- In square tables, marginal homogeneity and symmetry are different questions.
Concepts in this lesson