Skip to content

BUG: raise on non-2x2 tables in stats.mcnemar (#9485) - #9974

Merged
bashtage merged 2 commits into
statsmodels:mainfrom
lntutor:fix/mcnemar-reject-non-2x2-9485
Jul 26, 2026
Merged

bashtage merged 2 commits into
statsmodels:mainfrom
lntutor:fix/mcnemar-reject-non-2x2-9485

Conversation

@lntutor

@lntutor lntutor commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Closes #9485

Problem

statsmodels.stats.contingency_tables.mcnemar reads only the [0, 1] and
[1, 0] cells of the table:

table = _make_df_square(table)
table = np.asarray(table, dtype=np.float64)
n1, n2 = table[0, 1], table[1, 0]

There is no shape check, so passing a k x k table with k > 2 silently
returns a statistic computed from just the top-left 2x2 corner and ignores
every other cell. For example:

>>> from statsmodels.stats.contingency_tables import mcnemar
>>> import numpy as np
>>> mcnemar(np.array([[10, 5, 1], [3, 12, 2], [4, 6, 20]]), exact=False).statistic
0.125   # computed only from cells [0,1]=5 and [1,0]=3

Root cause

McNemar's test is only defined for a 2x2 table, but the implementation never
validates the shape after squaring the input, so oversized tables produce a
misleading result instead of an error.

Fix

After squaring the table, raise a clear ValueError when the shape is not
(2, 2), pointing users to SquareTable.symmetry (Bowker's test of
symmetry), which generalizes McNemar's test to k x k tables. Behavior for
valid 2x2 inputs is unchanged.

Test

Added test_mcnemar_non_2x2 in
statsmodels/stats/tests/test_contingency_tables.py, which asserts that a
3x3 input raises ValueError (both exact=True and exact=False) and that
a genuine 2x2 table still returns a finite statistic. The full
test_contingency_tables.py module passes (53 passed).

mcnemar only reads the [0, 1] and [1, 0] cells of the table, so passing a
k x k table with k > 2 silently returned a statistic computed from just the
top-left 2x2 corner, ignoring all other cells.

After squaring the table, raise a clear ValueError when the shape is not
(2, 2), pointing users to SquareTable.symmetry (Bowker's test) for larger
tables. Behavior for valid 2x2 tables is unchanged.

Closes statsmodels#9485

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01G8b9dEaJtpvydzz4ttM7JH

@bashtage bashtage left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small change needed, otehrwise LGTM.

Comment thread statsmodels/stats/contingency_tables.py Resolved
@bashtage

Copy link
Copy Markdown
Member

Thanks.

@bashtage
bashtage merged commit a99265f into statsmodels:main Jul 26, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Returning an error message when a table of dimensions larger than 2x2 is passed to the function stats.contingency_tables.mcnemar

2 participants