How to Perform McNemar’s Test in R

Spread the love

When working with categorical data that involves paired samples or repeated measurements, it’s common to wonder if significant changes occurred between two time points or conditions. This is where McNemar’s test comes into play. McNemar’s test is a non-parametric method used to determine whether row and column marginal frequencies are equal, that is, to test the homogeneity of marginals.

In this comprehensive guide, we will delve into the essence of McNemar’s test, explain data preparation steps, showcase how to execute the test in R, and offer a thorough interpretation guide.

Understanding McNemar’s Test

McNemar’s test is specifically designed for 2×2 contingency tables with paired samples. Imagine you’ve conducted a survey before and after a particular event, intervention, or treatment, asking participants a “yes” or “no” question. McNemar’s test helps identify if the intervention led to a change in responses.

Mathematically, the test statistic is computed as:


  • b is the number of subjects that responded “Yes” at Time 1 and “No” at Time 2.
  • c is the number of subjects that responded “No” at Time 1 and “Yes” at Time 2.

The test statistic Q follows a chi-square distribution with one degree of freedom.

Preparing Your Data

Your data should typically be structured in a way where you have the before and after responses of each participant. Here’s a hypothetical example:

# Sample data
before <- c("Yes", "No", "Yes", "Yes", "No")
after <- c("No", "No", "Yes", "No", "Yes")

data <- data.frame(before, after)

Performing McNemar’s Test in R

R provides a straightforward method to perform McNemar’s test via the mcnemar.test() function. Using our example data:

# Create the 2x2 table
table_data <- table(data$before, data$after)

# McNemar's Test
result <- mcnemar.test(table_data)

Interpreting the Results

The output of mcnemar.test() will include a chi-squared statistic and a p-value. An example output might be:

McNemar's Chi-squared test with continuity correction

data:  table_data
McNemar's chi-squared = 1, df = 1, p-value = 0.3173

Breaking this down:

  • McNemar’s chi-squared: This is the test statistic QQ. The higher this value, the more evidence against the null hypothesis.
  • p-value: As with most hypothesis tests, the p-value will help you determine the statistical significance of the test. A p-value smaller than a chosen significance level (commonly 0.05) suggests that there’s a significant difference in the paired proportions. In our example, with a p-value of 0.3173, we would fail to reject the null hypothesis at the 0.05 level, suggesting that the intervention didn’t lead to a significant change in responses.

Practical Applications and Considerations

  1. Clinical Trials: McNemar’s test is particularly popular in clinical settings where patients might be tested before and after a treatment.
  2. Only for 2×2 Tables: Remember, this test is designed specifically for 2×2 tables with paired samples. For larger tables or unpaired samples, other tests like the chi-squared test or Fisher’s exact test might be more appropriate.
  3. Small Sample Sizes: If the total of bb and cc is very small (less than 25), it’s advisable to use the exact binomial test.


McNemar’s test offers a robust approach for analyzing categorical paired data. Especially in medical, psychological, or sociological research, where before-and-after scenarios are common, the test provides valuable insights into the effectiveness of interventions. By using R’s mcnemar.test() function, researchers can swiftly determine the impact of their interventions, helping to inform decisions and shape future research directions.

Posted in RTagged

Leave a Reply