Last active
February 11, 2020 18:46
-
-
Save kylebgorman/c8b3fb31c1552ecbaafb to your computer and use it in GitHub Desktop.
Compute McNemar's test (two two-sided variants) in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scipy.stats | |
def mcnemar_p(n1: int, n2: int) -> float: | |
"""Computes McNemar's test. | |
Args: | |
n1: the number of "wins" for the first condition. | |
n2: the number of "wins" for the second condition. | |
Returns: | |
A p-value for McNemar's test. | |
""" | |
n = n1 + n2 | |
x = min(n1, n2) | |
dist = scipy.stats.binom(n, .5) | |
return 2.0 * dist.cdf(x) | |
def mcnemar_midp(n1: int, n2: int) -> float: | |
"""Computes McNemar's test using the "mid-p" variant. | |
This is based closely on: | |
M. W. Fagerland, S. Lydersen, P. Laake. 2013. The McNemar test for | |
binary matched-pairs data: Mid-p and asymptotic are better than exact | |
conditional. BMC Medical Research Methodology 13: 91. | |
Args: | |
n1: the number of "wins" for the first condition. | |
n2: the number of "wins" for the second condition. | |
Returns: | |
A p-value for the mid-p variant of McNemar's test. | |
""" | |
x = min(n1, n2) | |
return mcnemar_p(n1, n2) - dist.pmf(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment