Skip to content

BUG: Use the rank of exog for GEE degrees of freedom - #10278

Merged
bashtage merged 3 commits into
statsmodels:mainfrom
mkzung:bug/gee-df-resid-rank
Oct 3, 2026
Merged

bashtage merged 3 commits into
statsmodels:mainfrom
mkzung:bug/gee-df-resid-rank

Conversation

@mkzung

@mkzung mkzung commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

GEE sets df_model and df_resid from the column count of exog, over the rank-based values GLM.initialize has already put there. On a singular design that overstates df_model and understates df_resid. With an intercept, a regressor and twice that regressor on 40 rows, GEE reports df_model 2 and df_resid 37 where GLM reports 1 and 38, and its Gaussian-independence scale is 0.8778 against GLM's 0.8547 on the same fit.

The default ddof_scale was the column count too, so with more columns than rows the scale goes negative: 43 columns of rank 22 on 40 rows give df_resid -3 and a scale of -3.51.

Both degrees of freedom now come from the rank, as in the linear, GLM, RLM and discrete models, and the default ddof_scale is nobs - df_resid, so the scale divides by df_resid. The two examples above now give 1, 38 and 0.8547, and 18 with a positive scale.

test_df_resid_rank_deficient checks both, with GLM as the reference in the first, and fails on main. statsmodels/genmod/tests/test_gee.py is 82 passed and 3 skipped, and statsmodels/genmod is 1023 passed.

AI Disclosure

Contributions must comply with the statsmodels AI Policy.

Please complete one of the following:

  • No AI tools were used to develop this pull request.
  • AI tools were used.
    Tool(s): Claude Code.
    Used for: drafting the implementation, the test 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.

@mkzung

mkzung commented Sep 26, 2026

Copy link
Copy Markdown
Contributor Author

The Pyodide failure was in my test, not the fix. It compared GEE's scale with GLM's on a singular design, and on Pyodide GLM lands on a different fit: GEE gives 0.854665 as on desktop, GLM 0.861311. The degrees of freedom still come from GLM, and the scale is checked against GEE's own residuals, sum(resid**2) / df_resid.

mkzung added 2 commits October 1, 2026 16:01
GEE overwrote the rank based df_model and df_resid that GLM.initialize sets
with counts of the columns of exog. On a singular design that inflates
df_model and deflates df_resid, and when exog has more columns than there are
observations df_resid comes out negative. The default ddof_scale, the amount
subtracted from nobs in the denominator of the scale estimate, was the column
count for the same reason, so the estimated scale came out negative too.

Set both degrees of freedom from the rank of exog, as the linear, GLM, RLM and
discrete models do, and default ddof_scale to nobs - df_resid so the scale
estimate keeps dividing by df_resid.

See statsmodels#1928
The test compared GEE's scale with GLM's on a singular design, and on Pyodide
GLM lands elsewhere: GEE gives 0.854665, the same as on desktop, and GLM
0.861311. The degrees of freedom still come from GLM, which is what GH1928 is
about; the scale is held to sum(resid**2) / df_resid, which does not depend on
which solver found the fit.
@bashtage
bashtage force-pushed the bug/gee-df-resid-rank branch from ec7580c to 0ced243 Compare October 1, 2026 15:01
The first block of test_df_resid_rank_deficient already checks the scale against
sum(resid**2) / df_resid. The wide design below it asserted only scale > 0,
which fails where the fit of a singular design is not finite.
@bashtage
bashtage merged commit 44db225 into statsmodels:main Oct 3, 2026
12 of 13 checks passed
@bashtage

bashtage commented Oct 3, 2026

Copy link
Copy Markdown
Member

Thanks.

bashtage added a commit to bashtage/statsmodels that referenced this pull request Oct 5, 2026
statsmodelsGH-10278 sets df_model and df_resid from the rank of exog when the model is
created. Its test checks them after fit, for designs where the fit warns
about a singular matrix, and compares with GLM, whose fit of a singular
design differs on Pyodide. Add a test of the attributes of the unfitted
model for a full rank design, a design with a duplicated column, and a
design with more columns than observations, which does not depend on a
solver. It fails, like the existing test, if the degrees of freedom use
the number of columns instead of the rank.

Follow-up to statsmodelsGH-10278

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
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.

GEE: question behavior with singular exog, k_exog versus rank

2 participants