Repository navigation
BUG: Use the rank of exog for GEE degrees of freedom - #10278
Merged
Merged
Conversation
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. |
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
force-pushed
the
bug/gee-df-resid-rank
branch
from
October 1, 2026 15:01
ec7580c to
0ced243
Compare
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.
Member
|
Thanks. |
4 of 5 tasks
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]>
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.
GEE sets
df_modelanddf_residfrom the column count of exog, over the rank-based valuesGLM.initializehas already put there. On a singular design that overstatesdf_modeland understatesdf_resid. With an intercept, a regressor and twice that regressor on 40 rows, GEE reportsdf_model2 anddf_resid37 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_scalewas the column count too, so with more columns than rows the scale goes negative: 43 columns of rank 22 on 40 rows givedf_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_scaleisnobs - df_resid, so the scale divides bydf_resid. The two examples above now give 1, 38 and 0.8547, and 18 with a positive scale.test_df_resid_rank_deficientchecks both, with GLM as the reference in the first, and fails on main.statsmodels/genmod/tests/test_gee.pyis 82 passed and 3 skipped, andstatsmodels/genmodis 1023 passed.AI Disclosure
Contributions must comply with the statsmodels AI Policy.
Please complete one of the following:
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.