Chi-square and Fisher tests assume that observations contributing to the contingency table are independent. That assumption does not hold when the same participants are measured twice.
For paired binary data, McNemar’s test evaluates whether the probability of changing in one direction differs from the probability of changing in the opposite direction.
12.1 Example: SBP category before and after treatment
Classify SBP as below 130 mmHg or at least 130 mmHg at baseline and week 12.
mcn_dat <- dat |> drop_na(sbp_baseline_mmHg,sbp_week12_mmHg) |>
mutate(
Baseline=if_else(sbp_baseline_mmHg<130,"<130",">=130"),
Week12=if_else(sbp_week12_mmHg<130,"<130",">=130")
)
mcn_tab <- table(Baseline=mcn_dat$Baseline,Week12=mcn_dat$Week12)
kbl(as.data.frame.matrix(mcn_tab), digits=0,
caption="Paired SBP category at baseline and week 12")| <130 | >=130 |
|---|---|
| 163 | 18 |
| 54 | 118 |
The paired \(2\times2\) table can be written as:
| Baseline SBP | Week 12 < 130 | Week 12 ≥ 130 |
|---|---|---|
| Baseline < 130 | \(a\) | \(b\) |
| Baseline ≥ 130 | \(c\) | \(d\) |
The diagonal cells contain participants who remained in the same category. The off-diagonal cells contain participants who changed category.
- \(b\) is the number changing from the first category to the second: changed from below 130 to at least 130
- \(c\) is the number changing in the opposite direction: changed from at least 130 to below 130
McNemar’s statistic without continuity correction is
\[ \chi^2=\frac{(b-c)^2}{b+c}. \]
Only the discordant pairs, \(b\) and \(c\), contribute to the test.
mcnemar.test(mcn_tab, correct=FALSE)
#>
#> McNemar's Chi-squared test
#>
#> data: mcn_tab
#> McNemar's chi-squared = 18, df = 1, p-value = 0.00002
b <- mcn_tab[1,2]
c <- mcn_tab[2,1]
if(b+c>0) binom.test(b,b+c,p=.5)
#>
#> Exact binomial test
#>
#> data: b and b + c
#> number of successes = 18, number of trials = 72, p-value = 0.00003
#> alternative hypothesis: true probability of success is not equal to 0.5
#> 95 percent confidence interval:
#> 0.1554 0.3660
#> sample estimates:
#> probability of success
#> 0.25McNemar’s test evaluates
\[ H_0:P(\text{change in one direction}) =P(\text{change in the opposite direction}). \]
When the number of discordant pairs is small, the exact binomial calculation provides an exact alternative.
12.2 Selecting the method
The analysis can be summarized by three decisions:
| Question | Choice |
|---|---|
| Are the categorical observations independent? | chi-square or Fisher |
| Are expected counts adequate? | chi-square if yes; Fisher if no |
| Are binary measurements paired within participants? | McNemar |
| Are several related hypotheses being tested? | consider multiplicity adjustment |