By Data Tricks, 28 July 2020
A McNemar’s test is used to compare the frequencies of paired samples of dichotomous data. It is similar to the paired samples t-test but for dichotomous nominal instead of interval variables.
If your data are not dichotomous and you have more than two categories in your nominal variable an extension of the McNemar’s test called the McNemar-Bowker test might be appropriate. Fortunately, the code in R used in the example below would be identical as the mcnemar.test function can handle multiple categories in the nominal variable.
A McNemar’s test has a null hypothesis that the marginal probabilities for each outcome is the same. In other words, if the data is tabulated in a contingency table as follows:
Test 2 | |||
---|---|---|---|
Positive | Negative | ||
Test 1 | Positive | a | b |
Negative | c | d |
then the probability of b is the same as the probability of c.
First let’s create a set of values to use in this example:
set.seed(150) data <- data.frame(before = sample(c("Positive", "Positive", "Positive", "Positive", "Negative"), 300, replace = TRUE), after = sample(c("Positive", "Positive", "Positive", "Positive", "Negative"), 300, replace = TRUE))
A contingency table can be created using the table function:
> table(data$before, data$after) Negative Positive Negative 13 49 Positive 55 183
Our null hypothesis is that the marginal probabilities are the same, whilst the alternative hypothesis is that they are different.
test <- mcnemar.test(table(data$before, data$after))
Now analyse the result of the test:
> test McNemar's Chi-squared test with continuity correction data: table(data$before, data$after) McNemar's chi-squared = 0.24038, df = 1, p-value = 0.6239
The p-value is 0.62, above the 5% significance level and therefore the null hypothesis cannot be rejected.
Tags: mcnemars, statistics
Please note that your first comment on this site will be moderated, after which you will be able to comment freely.