Repository navigation
BUG: Reject rank-deficient ANOVA of types I, II and III - #10622
Merged
Merged
Conversation
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]>
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 of 2 tasks
bashtage
marked this pull request as ready for review
October 27, 2026 20:13
1 task done
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.
NumPy's guide.
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_lmwith 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: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.y ~ C(a) * C(b)without any observation for one combination) gives a negative sum of squares and an F of -0.0 forC(b)in Type II, and the interaction 2 degrees of freedom in Type I although only one is identified.y ~ A + A:B, the example of BUG: Nested ANOVA incorrect results -- wrong matrix definition #8506) giveAaNaNsum of squares in Type II and an interaction with 33 degrees of freedom.What this changes
anova_lmraises aValueErrorfor 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.model.model.rank, from the weighted design matrix), the one that was used fordf_resid, instead of a secondnp.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.anova_lmdocstring documents the error and explains when it occurs. The nested-factor designA/Bis supported if the labels ofBare reused in every level ofA; this is tested.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.pyfail onmainand pass with the change; the other 26 are the existing tests and the full-rank controls.Issues
#5398 is about
anova_lmwith singularexogand 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.pypasses (80 tests) withSM_FORMULA_ENGINEset topatsyand toformulaic, andstatsmodels/regression/tests/test_regression.pypasses (WSL, Python 3.13, NumPy 2.5.4, SciPy 1.18.1, pandas 3.1.0, onmainwith the Python files of this branch).ruff checkpasses for both changed Python files.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.
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