You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Returning an error message when a table of dimensions larger than 2x2 is passed to the function stats.contingency_tables.mcnemar #9485
Is your feature request related to a problem? Please describe
Currently, when one passes a table of dimensions larger than 2x2 to the function statsmodels.stats.contingency_tables.mcnemar, no error message is thrown. However, under the hood, the function considers only the upper left sub-table, and ignore the rest of the table. The end user should be informed of that, I think under the form of an error message.
The current absence of information about that is misleading, because an existing extension of the McNemar test (the McNemar-Bowker test) allows for square tables larger than 2x2. So, when passing a table larger than 2x2, one could think that the resulting p-value and statistic comes from the McNemar-Bowker test, when in reality they come from the "regular" McNemar test, conducted on only 4 cells of the table provided as input.
Illustrative example of the problem:
import statsmodels.stats.contingency_tables as ct
import numpy as np
table1 = np.array([[45,13,5],
[8,18,30],
[9,25,40]])
result1 = ct.mcnemar(table, exact=True, correction=True) #Ideally, this should return an error message
table2 = np.array([[45,13],
[8,18]])
result2 = ct.mcnemar(table2, exact=True, correction=True)
assert(result1.pvalue == result2.pvalue) #The p-values are exactly the same, but they shouldn't.
Describe the solution you'd like
When the user provides a table larger than 2x2 as input, the function should return an error, similar to the error returned when a table larger than 2x2 is provided to the class statsmodels.stats.contingency_tables.Table2x2 (i.e. ValueError: Table2x2 takes a 2x2 table as input.)
Describe alternatives you have considered
An alternative would be to offer the McNemar-Bowker test when the table provided as input is a square table with dimensions > 2x2. This is the current behavior of the mcnemar.test function in base R, but this may be more complicated to implement than an error message, hence my primary suggestion of just displaying an error message.
Additional context
I initially thought that the McNemar-Bowker test was implemented in statsmodels, but when comparing the results to R, I noticed that something wrong was happening. I had to dig in the source code to understand the behavior of the function for tables larger than 2x2.
mcnemar needs See Also to symmetry_bowker which is now in SquareTable.symmetry method
doc error: docstrings refer to TableSymmetry which is likely an old name for SquareTable.
related statsmodels/stats/contingency_tables has the rewritten standalone functions for mcnemar and cochransq, but does not have symmetry_bowker from the sandbox runs module as standalone function
quick internet search for literature:
mcnemar-bowker could be used from mcnemar but we have only the asymptotic chi2 pvalues.
In my quick search, I did not find a "exact" version for larger than 2x2
Aside: Since I was looking at multiple comparisons recently
This would benefit from a review of docstrings and how code is linked
possible rename method symmetry to test_symmetry
symmetry method in 2x2 Table class is inherited from SquareTable and so has only "bowker" i.e. asymptotic chi2 pvalues.
This should use mcnemar instead, so we get the "exact" option.
Note
Input checking for 2x2 versus kxk (and against cxk) is only relevant in standalone functions. The methods are attached to classes that check the size of the table in the __init__.
Activity
josef-pkt commentedon Jan 20, 2025
Thanks for reporting this.
Sounds bad, and I will try to confirm (and fix it) it before the next release
jeanbaptisteb commentedon Jan 20, 2025
As an additional information, I'm using the version 0.14.4 of statsmodels.
josef-pkt commentedon Mar 21, 2025
mcnemar needs See Also to
symmetry_bowkerwhich is now in SquareTable.symmetry methoddoc error: docstrings refer to
TableSymmetrywhich is likely an old name for SquareTable.related
statsmodels/stats/contingency_tableshas the rewritten standalone functions for mcnemar and cochransq, but does not have symmetry_bowker from the sandbox runs module as standalone functionquick internet search for literature:
mcnemar-bowker could be used from mcnemar but we have only the asymptotic chi2 pvalues.
In my quick search, I did not find a "exact" version for larger than 2x2
Aside: Since I was looking at multiple comparisons recently
We could add multiple test with pvalue corrections for each symmetry pair (i, j) versus (j, i).
AFAICS, this would be mcnemar on every 2x2 submatrix, i and j rows with i and j columns.
update that's available in R see post-hoc in https://rcompanion.org/handbook/H_05.html
This would benefit from a review of docstrings and how code is linked
possible rename method
symmetrytotest_symmetrysymmetry method in 2x2 Table class is inherited from SquareTable and so has only "bowker" i.e. asymptotic chi2 pvalues.
This should use mcnemar instead, so we get the "exact" option.
Note
Input checking for 2x2 versus kxk (and against cxk) is only relevant in standalone functions. The methods are attached to classes that check the size of the table in the
__init__.Aside:
those tests need checking for how Zero cells are handled
e.g.
https://stats.stackexchange.com/questions/529659/mcnemar-bowker-exact-test-in-r-difficulty-interpreting-the-results
references are mostly missing in contingency_tables
Tableclass docstring refers to a "dead" link (redirects to top page) https://onlinecourses.science.psu.edu/stat504/node/86I guess new link would be
https://online.stat.psu.edu/stat504/lesson/3
or a subpage
BUG: raise on non-2x2 tables in stats.mcnemar
Merge pull request #9974 from lntutor/fix/mcnemar-reject-non-2x2-9485