Home » How to Perform McNemar’s Test in R

How to Perform McNemar’s Test in R

by Tutor Aspire

McNemar’s Test is used to determine if there is a statistically significant difference in proportions between paired data.

This tutorial explains how to perform McNemar’s Test in R.

Example: McNemar’s Test in R

Suppose researchers want to know if a certain marketing video can change people’s opinion of a particular law. They survey 100 people to find out if they do or do not support the law. Then, they show all 100 people the marketing video and survey them again once the video is over.

The following table shows the total number of people who supported the law both before and after viewing the video:

Before Marketing Video
After Marketing Video Support Do not support
Support 30 40
Do not Support 12 18

To determine if there was a statistically significant difference in the proportion of people who supported the law before and after viewing the video, we can perform McNemar’s Test.

Step 1: Create the data.

First, create the dataset in a matrix form.

#create data
data #view data

                Before Video
After Video      Support Do Not Support
  Support             30             40
  Do Not Support      12             18

Step 2: Perform McNemar’s Test.

Next, perform McNemar’s Test using the following syntax:

mcnemar.test(x, y = NULL, correct = TRUE)


  • x: either a two-dimensional contingency table in matrix form, or a factor object.
  • y: a factor object; ignored if x is a matrix.
  • correct: TRUE = apply continuity correction when computing test statistic; FALSE = do not apply continuity correction.

In general, a continuity correction should be applied when some counts in the table are small. As a rule of thumb, this correction is typically applied when any of the cell counts are less than 5.

We will perform McNemar’s Test both with and without a continuity correction, just to illustrate the differences:

#Perform McNemar's Test with continuity correction

	McNemar's Chi-squared test with continuity correction

data:  data
McNemar's chi-squared = 14.019, df = 1, p-value = 0.000181

#Perform McNemar's Test without continuity correction 
mcnemar.test(data, correct=FALSE) 

	McNemar's Chi-squared test

data:  data
McNemar's chi-squared = 15.077, df = 1, p-value = 0.0001032

In both cases the p-value of the test is less than 0.05, so we would reject the null hypothesis and conclude that the proportion of people who supported the law before and after watching the marketing video was statistically significant different.

You may also like