Skip to content

Instantly share code, notes, and snippets.

@kylebgorman kylebgorman/mcnemar.py
Last active Jun 15, 2019

Embed
What would you like to do?
Compute McNemar's test (two two-sided variants) in Python
import scipy.stats
def mcnemar_p(b, c):
"""Computes McNemar's test.
Args:
b: the number of "wins" for the first condition.
c: the number of "wins" for the second condition.
Returns:
A p-value for McNemar's test.
"""
n = b + c
x = min(b, c)
dist = scipy.stats.binom(n, .5)
return 2. * dist.cdf(x)
def mcnemar_midp(b, c):
"""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:
b: the number of "wins" for the first condition.
c: the number of "wins" for the second condition.
Returns:
A p-value for the mid-p variant of McNemar's test.
"""
x = min(b, c)
return mcnemar_p(b, c) - dist.pmf(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.