Skip to content

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

Closed

Description

@jeanbaptisteb

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.

Activity

  1. added
    type-bug-wrong
    on Jan 20, 2025
  2. added this to the 0.15 milestone on Jan 20, 2025
  3. josef-pkt commented on Jan 20, 2025

    @josef-pkt
    Member

    Thanks for reporting this.

    Sounds bad, and I will try to confirm (and fix it) it before the next release

  4. jeanbaptisteb commented on Jan 20, 2025

    @jeanbaptisteb
    Author

    As an additional information, I'm using the version 0.14.4 of statsmodels.

  5. josef-pkt commented on Mar 21, 2025

    @josef-pkt
    Member

    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

    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 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__.

    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
    Table class docstring refers to a "dead" link (redirects to top page) https://onlinecourses.science.psu.edu/stat504/node/86
    I guess new link would be
    https://online.stat.psu.edu/stat504/lesson/3
    or a subpage

  6. added a commit that references this issue on Jul 26, 2026
    d4aa2ac
  7. added a commit that references this issue on Jul 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    comp-statsprio-hightype-bug-wrong

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions