Paired test for dichotomous data-McNemar’s test in R

[This article was first published on Methods – finnstats, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Paired test for dichotomous data, McNemar’s test to see the effect of some treatment, training, or advertisement which brings about the changes in the attitude of individuals.

This test is mainly useful when the measurements are on the nominal or ordinal scale.

A McNemar’s test is used to compare the frequencies of paired samples when it has binary responses.

If your scale contains more than 2 levels then make use of Wilcoxon signed rank test

Hypothesis

Ho: There is no significant change in individuals after the treatment

H1: There is a significant change in individuals after the treatment

Let’s take a practical example in R

Getting Data

set.seed(345)
data <- data.frame(before = sample(c("Positive",
                                     "Positive",
                                     "Positive",
                                     "Positive",
                                     "Negative",
                                     "Positive",
                                     "Positive",
                                     "Positive",
                                     "Positive",
                                     "Negative",
                                     "Negative"),
                                   30, replace = TRUE),
                   after = sample(c("Positive",
                                    "Positive",
                                    "Positive",
                                    "Positive",
                                    "Negative",
                                    "Negative",
                                    "Negative",
                                    "Negative",
                                    "Negative",
                                    "Negative"),
                                  30, replace = TRUE))

A contingency table can be created based on table function:

Sample Size Calculation Formula » Sampling Methods »

Paired test Comparison

table(data$before, data$after) 
            Negative Positive
 Negative        4        6
   Positive      10       10
test <- mcnemar.test(table(data$before, data$after))

Paired test Output:-

McNemar's Chi-squared test with continuity correction
 data:  table(data$before, data$after)
 McNemar's chi-squared = 0.5625, df = 1, p-value = 0.4533

Conclusion

Based on McNemar’s test, the p-value is 0.45, above the 5% significance level and therefore the null hypothesis cannot be rejected.

What are the Nonparametric tests? » Why, When and Methods »

The post Paired test for dichotomous data-McNemar’s test in R appeared first on finnstats.

To leave a comment for the author, please follow the link and comment on their blog: Methods – finnstats.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)