McNemar’s test in R

Performing McNemar’s test in R

To demonstrate McNemar’s test, data was used concerning the presence or absence of cold symptoms reported by the same children at age 12 and age 14. A total of 2638 participants were involved.

Using the epibasix::mcnemar function

Testing for a significant difference in cold symptoms between the two ages using the mcNemar function from the epibasix package can be performed as below. The symptoms for participants at age 12 and age 14 are tabulated and stored as an object, then passed to the mcNemar function. A more complete view of the output is achieved by calling the summary function.

library(epibasix)

X <- table(colds$age12, colds$age14)
epi_mcn <- mcNemar(X)
summary(epi_mcn)

Matched Pairs Analysis: McNemar's Statistic and Odds Ratio (Detailed Summary):
 
     
       No Yes
  No  707 256
  Yes 144 212

Entries in above matrix correspond to number of pairs. 
 
McNemar's Chi^2 Statistic (corrected for continuity) = 30.802 which has a p-value of: 0
Note: The p.value for McNemar's Test corresponds to the hypothesis test: H0: OR = 1 vs. HA: OR != 1
McNemar's Odds Ratio (b/c): 1.778
95% Confidence Limits for the OR are: [1.449, 2.208]
The risk difference is: 0.085
95% Confidence Limits for the rd are: [0.055, 0.115]

Using the stats::mcnemar.test function

McNemar’s test can also be performed using stats::mcnemar.test as shown below, using the same table X as in the previous section.

mcnemar.test(X)

    McNemar's Chi-squared test with continuity correction

data:  X
McNemar's chi-squared = 30.802, df = 1, p-value = 2.857e-08

The result is shown without continuity correction by specifying correct=FALSE.

mcnemar.test(X, correct=FALSE)

    McNemar's Chi-squared test

data:  X
McNemar's chi-squared = 31.36, df = 1, p-value = 2.144e-08

Results

As default, using summary with epibasix::mcNemar gives additional information to the McNemar’s chi-square statistic. This includes a table to view proportions, and odds ratio and risk difference with 95% confidence limits. The result uses Edward’s continuity correction without the option to remove this, which is consistent with other functions within the package.

stats::mcnemar.test uses a continuity correction as default but does allow for this to be removed. This function does not output any other coefficients for agreement or proportions but (if required) these can be achieved within other functions or packages in R.