Skip to content

BUG: Reject rank-deficient ANOVA of types I, II and III - #10622

Merged
bashtage merged 6 commits into
statsmodels:mainfrom
bashtage:wip-pr10340-anova-rank
Oct 28, 2026
Merged

bashtage merged 6 commits into
statsmodels:mainfrom
bashtage:wip-pr10340-anova-rank

Conversation

@bashtage

Copy link
Copy Markdown
Member

This takes over #10340 by Donncha O'Toole (dnncha), who found the problem and wrote the Type I guard and its tests. Their two commits are the first two commits of this branch, unchanged. The rest of the branch addresses the review of #10340, which asked for the same protection for Type II and III and for tests.

The problem

anova_lm with a single model assumes that every column of the design matrix is identified. If it is not, the table is wrong and does not say so:

  • A duplicated column (y ~ x + duplicate) gives each copy the full degree of freedom and the same p-value in Type II and III, and an arbitrary sum of squares in Type I.
  • A two-way design with an empty cell (y ~ C(a) * C(b) without any observation for one combination) gives a negative sum of squares and an F of -0.0 for C(b) in Type II, and the interaction 2 degrees of freedom in Type I although only one is identified.
  • Nested factors with unique labels (y ~ A + A:B, the example of BUG: Nested ANOVA incorrect results -- wrong matrix definition #8506) give A a NaN sum of squares in Type II and an interaction with 33 degrees of freedom.
  • A WLS model where a factor level only has observations with zero weight has a design matrix of full rank but a weighted design matrix that is rank deficient, and the tables count the empty level.

What this changes

  • anova_lm raises a ValueError for a single model of Type I, II or III if the design matrix is rank deficient. The message gives the type, the number of columns and the rank. Multiple models are not affected.
  • The rank is the rank of the fitted model (model.model.rank, from the weighted design matrix), the one that was used for df_resid, instead of a second np.linalg.matrix_rank(exog) of the unweighted matrix. The two agree except when weights are zero, where the unweighted check would not catch the problem.
  • The anova_lm docstring documents the error and explains when it occurs. The nested-factor design A/B is supported if the labels of B are reused in every level of A; this is tested.
  • The release note is in the behavior changes of 0.15.1.
  • The tests of BUG: reject rank-deficient Type I ANOVA #10340 are moved below the data and extended. They now cover Type I, II and III (1/2/3 and "I"/"II"/"III"), robust="hc3", the duplicate and the zero column, more columns than observations, an empty cell, the nested design of BUG: Nested ANOVA incorrect results -- wrong matrix definition #8506 with unique labels (raises) and with reused labels (works, sums of squares checked against the change in the residual sum of squares of the fitted sub-models), zero weights, and a full-rank control.

54 of the 80 tests in test_anova.py fail on main and pass with the change; the other 26 are the existing tests and the full-rank controls.

Issues

#5398 is about anova_lm with singular exog and asks for a better exception message or a justification for ignoring the case. The error and the documentation provide both.

The nested ANOVA of #8506 and #8336 is not implemented here, so those issues stay open: a rank-deficient nested design now raises instead of returning wrong numbers, and automatic dropping of empty columns is a separate design question (see #8512 and #8721).

Verification

  • pytest statsmodels/stats/tests/test_anova.py passes (80 tests) with SM_FORMULA_ENGINE set to patsy and to formulaic, and statsmodels/regression/tests/test_regression.py passes (WSL, Python 3.13, NumPy 2.5.4, SciPy 1.18.1, pandas 3.1.0, on main with the Python files of this branch).
  • ruff check passes for both changed Python files.
  • The CI of this draft has not run yet and the documentation was not built.

AI Disclosure

Contributions must comply with the statsmodels AI Policy.

Tick exactly one of the following. If AI tools were used, replace the <...>
placeholders in the second option with the tool name(s) and how the tool's output was
used.

  • No AI tools were used to develop this pull request.
  • AI tools were used.
    Tool(s): Claude Code (Claude Sonnet 5.5). The commits of BUG: reject rank-deficient Type I ANOVA #10340 were made with OpenAI Codex, according to the disclosure of that pull request.
    Used for: extending the check to Type II and III, using the rank of the fitted model, the docstring, the tests, the release note and this description.
    I have personally read, understood, and can explain every line of this diff, and
    have verified the statistical/numerical correctness of the change.

🤖 Generated with Claude Code

dnncha and others added 6 commits October 27, 2026 19:36
Type I sums of squares assume a full-rank design. Reject unsupported singular designs before QR decomposition so redundant terms cannot receive arbitrary sums of squares. Related to statsmodelsGH-8506.
Check the rank of the fitted model instead of the unweighted design matrix
and apply the check to all three types. Type II and III tables of a model
with a duplicated column, an empty cell or a level that only has zero
weights assigned arbitrary degrees of freedom, negative sums of squares and
meaningless p-values. The error message reports the number of columns and
the rank, and the docstring of anova_lm explains the restriction.

Closes statsmodelsGH-5398. Related to statsmodelsGH-8506.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Move the tests of the rank check below the data and extend them to Type I,
II and III, robust covariance, empty cells, the nested factor of statsmodelsGH-8506,
zero weights and more columns than observations. Add controls for the full
rank designs that must keep working.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@bashtage bashtage mentioned this pull request Oct 27, 2026
1 of 2 tasks
@bashtage
bashtage marked this pull request as ready for review October 27, 2026 20:13
@bashtage
bashtage merged commit 5c4ad85 into statsmodels:main Oct 28, 2026
13 checks passed
@bashtage
bashtage deleted the wip-pr10340-anova-rank branch October 28, 2026 07:45
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.

BUG/ENH: singular exog and anova_lm

2 participants