Table of Contents
McNemar’s Test is a statistical method used to compare paired categorical data. It is commonly used to determine if there is a significant difference between two paired proportions. In order to perform McNemar’s Test in R, follow these steps:
1. Begin by importing your data into R. Make sure your data is in a table format with two columns representing the two paired variables.
2. Next, install and load the “McNemar” package in R.
3. Use the “mcnemar.test()” function to perform the test. This function requires two arguments: the first column of data and the second column of data.
4. The output of the function will provide the test statistic, degrees of freedom, and p-value. The p-value will determine the significance of the difference between the two proportions.
5. Finally, you can interpret the results and make conclusions about the significance of the difference between the two paired proportions.
In summary, McNemar’s Test is a useful tool for comparing paired categorical data in R. By following these steps, you can easily perform the test and analyze the results to make informed decisions.
Perform McNemar’s Test in R
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 <- matrix(c(30, 12, 40, 18), nrow = 2, dimnames = list("After Video" = c("Support", "Do Not Support"), "Before Video" = c("Support", "Do Not Support"))) #view data 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)
where:
- 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.test(data) 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.