Repository navigation
BUG: raise on non-2x2 tables in stats.mcnemar (#9485) - #9974
Merged
bashtage merged 2 commits intoJul 26, 2026
Merged
Conversation
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
requested changes
Jul 26, 2026
bashtage
left a comment
Member
There was a problem hiding this comment.
Small change needed, otehrwise LGTM.
bashtage
approved these changes
Jul 26, 2026
Member
|
Thanks. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #9485
Problem
statsmodels.stats.contingency_tables.mcnemarreads only the[0, 1]and[1, 0]cells of the table:There is no shape check, so passing a
k x ktable withk > 2silentlyreturns a statistic computed from just the top-left 2x2 corner and ignores
every other cell. For example:
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
ValueErrorwhen the shape is not(2, 2), pointing users toSquareTable.symmetry(Bowker's test ofsymmetry), which generalizes McNemar's test to
k x ktables. Behavior forvalid 2x2 inputs is unchanged.
Test
Added
test_mcnemar_non_2x2instatsmodels/stats/tests/test_contingency_tables.py, which asserts that a3x3 input raises
ValueError(bothexact=Trueandexact=False) and thata genuine 2x2 table still returns a finite statistic. The full
test_contingency_tables.pymodule passes (53 passed).