In the first example, a researcher attempts to determine if a drug has an effect on a particular disease. There are 314 patients, and they are diagnosed (disease:
present or
absent) before and after using the drug, which means that each sample can be described using 1 out of 4 combinations. Counts of individuals are given in the table, with the diagnosis (disease:
present or
absent) before treatment given in the rows, and the diagnosis after treatment in the columns. The test requires the same subjects to be included in the before-and-after measurements (matched pairs). In this example, the null hypothesis of "marginal homogeneity" would mean there was no effect of the treatment. From the above data, the McNemar test statistic: :\chi^2 = {(121 - 59)^2 \over {121 + 59}} has the value 21.35, which is extremely unlikely to form the distribution implied by the null hypothesis (
p \chi^2 = 3.68 and
p = 0.055. The asymptotic McNemar's test gives \chi^2 = 4.55 and
p = 0.033 and the mid-P McNemar's test gives
p = 0.035. Both the McNemar's test and mid-P version provide stronger evidence for a statistically significant treatment effect in this second example.
Example implementation in Python The following is an example implementation using the probability distributions provided by the
SciPy package. The implementation uses the mid-P version if
b +
c from scipy.stats import binom, chi2 def mcnemar(b: int, c: int, continuity_correction: bool = False) -> float: check_valid = lambda n: isinstance(n, int) or (isinstance(n, float) and n.is_integer()) if not all(map(check_valid, [b, c])): raise ValueError("b and c must be integers!") n_min, n_max = sorted([b, c]) corr = int(continuity_correction) if (n_min + n_max) It is also worth noting that the above implementation ensures that the smaller of
b and
c is passed as the first argument to the binomial probability distributions. See "Additional file 1" under the supplementary materials for further reading. ==Discussion==