Skip to content

BUG: reject rank-deficient Type I ANOVA - #10340

Closed
dnncha wants to merge 2 commits into
statsmodels:mainfrom
dnncha:fix/type1-anova-rank
Closed

dnncha wants to merge 2 commits into
statsmodels:mainfrom
dnncha:fix/type1-anova-rank

Conversation

@dnncha

@dnncha dnncha commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

Type I ANOVA maps QR effects back to formula terms under a full-rank design assumption. With an exact duplicate predictor, the model span and residual sum of squares are unchanged, but the duplicate can receive an arbitrary sum of squares and a significant p-value.

For a fixed 12-row example, y ~ x + duplicate currently assigns the duplicate sum_sq=138.6197 and p=0.02714, while y ~ x gives p=0.80229; the fitted values differ by less than 9e-16. This change rejects rank-deficient single-model Type I ANOVA before the QR calculation. It does not implement nested ANOVA or change Type II/III behavior.

Related to #8506.

Validation:

  • Regression tests: 5 failures before the guard; 5 passes after it.
  • python -m pytest --cov=statsmodels.stats.anova --cov-report=term-missing statsmodels/stats/tests/test_anova.py -q: 22 passed.
  • ruff check statsmodels/stats/anova.py statsmodels/stats/tests/test_anova.py: passed.
  • Public anova_lm replay: the rank-deficient model now raises ValueError; the full-rank control is unchanged.

AI Disclosure

Contributions must comply with the statsmodels AI Policy.

  • No AI tools were used to develop this pull request.
  • AI tools were used.
    Tool(s): OpenAI Codex (GPT-6).
    Used for: reproducing the reported behavior, porting the guard and regression tests, running validation, and drafting this description.
    The numerical behavior was checked against the unchanged model span and residual sum of squares. This submission has not undergone independent human scientific review.

@bashtage

Copy link
Copy Markdown
Member

Is this unique to Type I? If it affects other types, it could be good to cover those as well.

dnncha added 2 commits October 1, 2026 16:02
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.
@bashtage
bashtage force-pushed the fix/type1-anova-rank branch from 0f7df18 to 3232738 Compare October 1, 2026 15:02

@bashtage bashtage left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please extend the PR to Type II and III which also have the same issue.

@bashtage bashtage left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, should have been request changes to add proection for type II and III and tests.

@bashtage

Copy link
Copy Markdown
Member

Thanks @dnncha for finding this and for the Type I guard and its tests. I have taken this over in #10622 to add the same protection for Type II and III, as requested in the review. Your two commits are kept unchanged at the start of that branch, so you remain their author. I will close this pull request when #10622 is merged.

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.

2 participants