pairs <- matrix(c(510, 5, 16, 90), nrow = 2,
dimnames = list(A = c("survived", "died"), B = c("survived", "died")))
pairs B
A survived died
survived 510 16
died 5 90
The chi-square test of Chapter 8 compares two independent groups. Many studies in medicine use pairs. A study can match each patient to a similar patient, or measure the same person two times. The two members of a pair are not independent, so the test must use the pairs. McNemar’s test does this for a 0/1 outcome, in the same way that the paired t-test of Chapter 4 does it for a mean.
Rosner describes a study design to compare two chemotherapy regimens for breast cancer after mastectomy (Fundamentals of Biostatistics, 8th edn, Cengage). The example is for instruction only. It does not come from a published trial.
The patients are matched in pairs on age (within 5 years) and clinical condition. In each pair, one random member received regimen A: chemotherapy in the week after surgery and for 6 more months. The other member received regimen B: chemotherapy in the week after surgery only. The outcome is survival at 5 years.
pairs <- matrix(c(510, 5, 16, 90), nrow = 2,
dimnames = list(A = c("survived", "died"), B = c("survived", "died")))
pairs B
A survived died
survived 510 16
died 5 90
n_pairs <- sum(pairs)
surv_A <- sum(pairs["survived", ])
surv_B <- sum(pairs[, "survived"])
n_AB <- pairs["survived", "died"] # A survived, B died
n_BA <- pairs["died", "survived"] # A died, B survived
n_D <- n_AB + n_BAEach cell counts pairs, not patients. In 510 pairs both patients survived, and in 90 pairs both died. In 16 pairs only the patient on regimen A survived. In 5 pairs only the patient on regimen B survived. The survival is 526/621 = 0.847 with A and 515/621 = 0.829 with B.
Does the longer regimen A improve survival at 5 years?
We can put the 1242 patients in an ordinary 2 × 2 table of treatment against outcome. Then we can do the chi-square test of Chapter 8 on it.
unpaired <- matrix(c(surv_A, n_pairs - surv_A, surv_B, n_pairs - surv_B), nrow = 2,
dimnames = list(outcome = c("survived", "died"), regimen = c("A", "B")))
unpaired regimen
outcome A B
survived 526 515
died 95 106
wrong <- chisq.test(unpaired)
wrong
Pearson's Chi-squared test with Yates' continuity correction
data: unpaired
X-squared = 0.59357, df = 1, p-value = 0.441
The test gives \(X^2 =\) 0.59 and p = 0.44. It finds no difference between the regimens. This test assumes that the two columns are independent samples. Here each patient on A has a matched partner on B, so this assumption is false.
When both patients in a pair survive, the pair gives no information about which regimen is better. The same is true when both patients die. Only the 21 pairs with different outcomes can show a difference. These are the discordant pairs. In 16 of them the A patient survived, and in 5 of them the B patient survived.
Suppose that the two regimens are equally effective. In a discordant pair, one patient survived and one died. The regimen labels A and B in that pair are then arbitrary. A swap of the two labels inside the pair gives an equally probable result. Each discordant pair is then a coin flip. The count of pairs in which A survived is binomial, with \(n =\) 21 and \(p = 1/2\).
set.seed(621)
shuffled <- rbinom(20000, n_D, 0.5) # each discordant pair: swap A and B with probability 1/2
p_shuffle <- mean(abs(shuffled - n_D / 2) >= abs(n_AB - n_D / 2))
p_shuffle[1] 0.02685
After 20,000 shuffles, a split at least as uneven as 5 to 16 occurs in a proportion 0.027 of the shuffles. The exact binomial calculation gives p = 0.027. Figure 10.1 shows the full distribution.
tibble(k = 0:n_D, prob = dbinom(k, n_D, 0.5)) |>
mutate(tail = k <= lo | k >= hi) |>
ggplot(aes(k, prob, fill = tail)) +
geom_col(width = 0.8) +
scale_fill_manual(values = c(`FALSE` = "grey70", `TRUE` = "firebrick"), guide = "none") +
labs(x = "discordant pairs in which the A patient survived", y = "probability")
The observed split is far in the tails. The unpaired test used all 1242 patients and found no difference. The paired analysis uses only 21 pairs and finds a difference, because the matched design removes the variation between patients.
Let \(n_A\) and \(n_B\) be the numbers of type A and type B pairs, and \(n_D = n_A + n_B\). Let \(p\) be the probability that a discordant pair is type A. The null hypothesis is \(H_0: p = 1/2\), which means that the two regimens are equally effective. If A is more effective, type A pairs are rare and \(p < 1/2\). Here \(n_A =\) 5, \(n_B =\) 16 and \(n_D =\) 21.
This is the sign test of Chapter 3, done on the discordant pairs. The concordant pairs give no sign, in the same way that a zero difference gives no sign.
For a large \(n_D\), the binomial count \(n_A\) is close to normal, with mean \(n_D/2\) and variance \(n_D/4\). The square of the standardised count gives McNemar’s statistic:
\[ X^2 = \frac{(n_A - n_B)^2}{n_D}, \qquad X^2_{\text{corrected}} = \frac{(|n_A - n_B| - 1)^2}{n_D}. \]
Under \(H_0\), both follow a chi-square distribution with 1 degree of freedom (df). The second form includes a continuity correction, because the count is discrete. Use the normal approximation only when \(n_D \geq 20\). For a smaller \(n_D\), use the exact binomial test.
x2_plain <- (n_BA - n_AB)^2 / n_D
x2_corr <- (abs(n_BA - n_AB) - 1)^2 / n_D
c(X2 = x2_plain, p = pchisq(x2_plain, 1, lower.tail = FALSE),
X2_corrected = x2_corr, p_corrected = pchisq(x2_corr, 1, lower.tail = FALSE)) X2 p X2_corrected p_corrected
5.76190476 0.01637731 4.76190476 0.02909633
mcnemar.test() takes the table of pairs. It uses only the two off-diagonal cells.
mc <- mcnemar.test(pairs)
mc
McNemar's Chi-squared test with continuity correction
data: pairs
McNemar's chi-squared = 4.7619, df = 1, p-value = 0.0291
McNemar's Chi-squared test with continuity correction: R applies the correction by default.McNemar's chi-squared: 4.76. It is our corrected value, \((|5 - 16| - 1)^2 / 21\).df: 1. The test conditions on the observed \(n_D\), so the statistic has one free count, \(n_A\).p-value: 0.029. With correct = FALSE, R gives \(X^2 =\) 5.76 and p = 0.016.The exact test is a binomial test on the discordant pairs:
bt <- binom.test(n_BA, n_D, p = 0.5)
bt
Exact binomial test
data: n_BA and n_D
number of successes = 5, number of trials = 21, p-value = 0.0266
alternative hypothesis: true probability of success is not equal to 0.5
95 percent confidence interval:
0.08217588 0.47165983
sample estimates:
probability of success
0.2380952
The exact p-value is 0.027, close to the corrected McNemar p-value. All three p-values are below 0.05. Regimen A gives better survival at 5 years than regimen B. The unpaired chi-square test gave p = 0.44 on the same patients and missed this difference.
beef <- matrix(c(136, 69, 92, 240), nrow = 2,
dimnames = list(survey1 = c("<= 1/week", "> 1/week"),
survey2 = c("<= 1/week", "> 1/week")))
beef survey2
survey1 <= 1/week > 1/week
<= 1/week 136 92
> 1/week 69 240
A paired table also shows reproducibility. Rosner gives a diet questionnaire that 537 female nurses in the USA answered two times, several months apart. One question asked how much beef they ate: at most 1 portion per week, or more. The two answers of each nurse form a pair.
Does the questionnaire give the same answer the second time?
The observed agreement \(p_o\) is the proportion of nurses on the diagonal. Here \(p_o =\) (136 + 240)/537 = 0.700. Some agreement occurs by chance. Suppose that the two answers are independent but each keeps its own margins. Then the expected agreement is
\[ p_e = \sum_i \frac{\text{row}_i \times \text{column}_i}{n^2}. \]
Here \(p_e =\) 0.518. Cohen’s kappa scales the agreement above chance by the largest possible agreement above chance:
\[ \kappa = \frac{p_o - p_e}{1 - p_e}. \]
The questionnaire gives \(\kappa =\) 0.38. It covers 38% of the distance from chance agreement (\(\kappa = 0\)) to perfect agreement (\(\kappa = 1\)). A negative \(\kappa\) shows less agreement than chance. Fleiss gives a guide: above 0.75 is excellent, 0.40 to 0.75 is fair to good, and below 0.40 is poor.
Rosner calls values below 0.40 marginal. By either name, the beef question has low reproducibility.
c(p_o = p_o, p_e = p_e, kappa = kappa) p_o p_e kappa
0.7001862 0.5178365 0.3781906
vcd::Kappa(beef) value ASE z Pr(>|z|)
Unweighted 0.3782 0.04045 9.351 8.705e-21
Weighted 0.3782 0.04045 9.351 8.705e-21
vcd::Kappa() gives the same value in the Unweighted row. Its small p-value tests \(\kappa = 0\). Any \(\kappa\) above 0 passes this test, so the p-value gives little information about reproducibility.
McNemar’s test and kappa use the same kind of table, and they answer different questions. McNemar’s test uses only the off-diagonal cells, and it asks whether the margins differ. Kappa uses the diagonal, and it asks how often the two answers agree.
mcnemar.test() and binom.test().