Skip to content

Instantly share code, notes, and snippets.

@kylebgorman
Last active Feb 11, 2020
Embed
What would you like to do?
Compute McNemar's test (two two-sided variants) in Python
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