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=T)

REQUIRED ARGUMENTS:
x:
either a factor, a category object, or a two-dimensional contingency table in matrix form. If x is a matrix, it must be square, have at least two rows and columns, all elements of x must be non-negative, and NAs, and Infs are not allowed. The elements of matrix x should be whole numbers, as the test is based on counts; however, since all computations are carried out to double precision accuracy where possible, the storage mode of matrix x will be coerced to "double". For restrictions on x when it is a factor or a category object, see argument y.

OPTIONAL ARGUMENTS:
y:
factor or category object. If x is a matrix, y is ignored. If x is a factor or a category object, y is required and must have the same length as x. Both objects must have the same number of levels, and this number must be at least two. NAs in the category index vectors are allowed, but pairs (x[i],y[i]) containing these will be 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; an NA in an index vector means that the observation is not in one of the groups listed for that category object. Infs have no meaning as indices, and should not be present.

Conversely, if x or y is not a factor/category object (and x is not a matrix), it will be coerced to one implicitly. In this case pairs (x[i],y[i]) containing NAs will be removed, but not pairs with Infs. Coercion of x and y in this manner is intended for datasets of mode numeric, whose elements are typically small integers; data in the form of character vectors should first be made into factor or category objects.

correct:
logical flag: if TRUE, a continuity correction will be applied, but only under certain conditions. See section DETAILS.

VALUE:
A list of class "htest", containing the following components:

statistic:
McNemar's statistic, with names attribute "McNemars chi-square". See section DETAILS for a definition.
parameters:
the degrees of freedom of the asymptotic chi-square distribution associated with statistic. Component parameters has names attribute "df".
p.value:
the asymptotic p-value for the test.
method:
character string giving the name of the method used, including whether the continuity correction was applied.
data.name:
a character string (vector of length 1) containing the actual name of the input argument x, and of y if both are factor or category objects.


NULL:
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]. Fleiss (1981) p. 113 gives an application of McNemar's test to matched case-control pairs.


TEST:
The returned p.value should be interpreted carefully. 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. Some attention should also be given to the underlying sampling scheme; see the literature references for examples.

DETAILS:
See the hardcopy help-file for an algebraic definition of the statistic.

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.


REFERENCES:
Fleiss, J. L. (1981). Statistical Methods for Rates and Proportions, 2nd ed. New York: Wiley.

Zar, J. H. (1984). Biostatistical Analysis, 2nd ed. Englewood Cliffs: Prentice-Hall.


SEE ALSO:
fisher.test , chisq.test , mantelhaen.test , category , cut , table .

EXAMPLES:
x        # x and y are category objects
  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1
 [33] 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 [65] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 [97] 2 2 2 2
attr(, "levels"):
[1] "Present" "Absent"
table(x,y)        # table from Fleiss, p. 114
        Present Absent
Present      15     20
 Absent       5     60
mcnemar.test(x,y)
mcnemar.test(table(x,y))        # same thing