mcnemar.test
McNemar's Chi-Square Test for Count Data

Description

Performs a McNemar's chi-square test on a two-dimensional contingency table.

Usage

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

Arguments

x a factor or a two-dimensional contingency table in matrix form.

If x is a matrix, it must be square and contain at least two rows and two columns. All elements of the matrix must be non-negative, and not contain any missing values (NAs) or infinite values (Infs). Since the test uses counts, the elements should be whole numbers, however, the function coerces the storage mode of the matrix to double and, where possible, the function carries out computations to double precision accuracy.
For restrictions on x when it is a factor object, see argument y.

y a factor object. If x is a matrix, y is ignored.

If x is a factor object, y is required and length(x) must equal length(y). Both objects must contain more than two levels and have the same number of levels as the other object.

The index vectors can contain missing values (NAs), but if both elements in a pair ((x[i],y[i])) are missing values (NAs), they are removed. Each element of the index vectors of x and y should give the membership of that observation in one of the groups present in the levels attributes, a missing value (NA) in an index vector means that the observation is not in one of the groups listed for that factor object. Infinite values Infs have no meaning as indices, and should not be present.

If x or y is not a factor object (and x is not a matrix), it is implicitly coerced to a factor object. In this case, pairs ((x[i],y[i])) that contain missing values (NAs) are removed, but not pairs with infinite values (Infs). Coercion of x and y in this manner is intended for datasets of mode numeric, whose elements are typically small integers; you should convert data in the form of character vectors into factor objects before using them in this function.

correct a logical value. If TRUE, a continuity correction will be applied, but only under certain conditions.
Value
returns a list of class htest that contain the following components:
statistic McNemar's statistic along with the names attribute "McNemar's chi-squared".
parameters degrees of freedom of the asymptotic chi-square distribution associated with statistic along with the names attribute df.
p.value asymptotic p-value for the test.
method a character string that returns the name of the method used, including whether the continuity correction was applied.
data.name a character string (vector of length 1) that contains the actual name of the input argument x, and of y if both are factor objects.
Null hypothesis
Under the null hypothesis, McNemar's statistic has an asymptotic chi-square distribution with degrees of freedom given by
R * (R - 1) / 2
where R is the number of rows of the contingency table.
If the contingency table consists of N observations cross-classified on the row and column variables, which would typically have the same levels, then McNemar's statistic could be used to test the null hypothesis of symmetry, namely that the probability of an observation being classified into cell [i,j] is the same as the probability of being classified into cell [j,i]. For an application of McNemar's test to matched case-control pairs see Fleiss (1981) p. 113.
Test assumptions
You should interpret the returned p.value carefully because its validity depends on the assumption that the cell counts are at least moderately large. Even when cell counts are adequate, the chi-square is only a large-sample approximation to the true distribution of McNemar's statistic under the null hypothesis. You should also consider the underlying sampling scheme; see the literature references for examples.
References
Agresti, A. 1990. Categorical data analysis. New York, NY: Wiley. Pages 350-354.
Fleiss, J. L. 1981. Statistical Methods for Rates and Proportions. Second Edition. New York, NY: Wiley.
Zar, J. H. 1984. Biostatistical Analysis. Second Edition. Englewood Cliffs, NJ: Prentice-Hall.
See Also
fisher.test, chisq.test, mantelhaen.test, cut, table.
Examples
# Data from table Fleiss, p. 114
cases <- factor(rep(c(1,2,1,2), c(15,5,20,60)),
    labels=c("Present", "Absent"))
controls <- factor(rep(c(1,2), c(20,80)),
    labels=c("Present", "Absent"))
table(cases, controls)
mcnemar.test(cases, controls)
mcnemar.test(table(cases, controls))  # same thing 

mcnemar.test(cases, controls, correct = FALSE) mcnemar.test(matrix(c(6,3,5,8,2,9,1,7,4), nrow = 3)) # matrix input

Package stats version 6.1.1-7
Package Index