Repository navigation
BUG: Follow up #10792 on GEE small-sample covariances, docs and tests - #10802
Merged
Merged
Conversation
bashtage
force-pushed
the
wip-gee-margeff-followup
branch
from
November 19, 2026 11:03
b2f0375 to
a144ece
Compare
bashtage
marked this pull request as ready for review
November 19, 2026 16:20
The leverage check of statsmodels#10792 calls np.linalg.eigvals, which raises a bare LinAlgError when the covariance of the parameters contains NaN or inf, for example for a Poisson model with an exchangeable correlation and an observation with y = 0 and its own dummy variable. Before statsmodels#10792 the same fit returned NaN standard errors for cov_type="bias_reduced" and failed in sqrtm for "kauermann_carroll". Check that the covariance is finite first and raise a ValueError that says why the leverages cannot be computed. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Add a Raises entry with a versionchanged directive to GEE.fit, and say that a leverage larger than 1 - 1e-8 counts as 1 (the tolerance of the check added in statsmodels#10792). Reword the release note of statsmodels#10792: main returned meaningless covariances for some families, not only Gaussian (Binomial bse about 9e15, Poisson NaN), and a non-finite covariance now raises the ValueError. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The leverage test of statsmodels#10792 still passed if the p x p matrix was replaced by dmat.T @ dmat, if the message showed the position instead of the label of the group, or if the cov_type name in the message was wrong. Use string group labels and match the cov_type, compare the check with the eigenvalues of the full leverage matrix built in numpy for an exchangeable correlation (with dummies whose leverage is within and outside 1e-8 of 1), and add a case with a non-finite covariance. Import scipy.stats at module level. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
bashtage
force-pushed
the
wip-gee-margeff-followup
branch
from
November 19, 2026 17:56
a144ece to
9d2f408
Compare
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.
Follow up to #10792 (
get_margeffwithuse_t, the margins summary, and the leverage check of the small-sample covariances ofGEE). Its review left one case where a valid call now fails differently from before, docstring points and a leverage test that several breakages of the code survive.NumPy's guide.
Decision for the maintainer. The
BUG:commit (the first one) changes whatGEE.fit(cov_type="bias_reduced")and"kauermann_carroll"do when the covariance of the parameters is not finite: it raises aValueErrorwhere #10792 raised a bareLinAlgError(and the code before #10792 returnedNaNstandard errors forbias_reduced). If you prefer to keep returningNaNthere, drop that commit and its test; the docstring and release-note sentences about it are in the later commits and would go with it.Changes
BUG:the new eigenvalue call of BUG: Follow up #10790 on GEE marginal effects and small-sample covariances #10792 inGEE._bc_covmatraisedLinAlgError: Array must not contain infs or NaNswhencov_naiveis not finite. Reproduced with Poisson and an exchangeable working correlation, one observation withy = 0and its own dummy in a group of 4: on the code before BUG: Follow up #10790 on GEE marginal effects and small-sample covariances #10792 (2e47023ed)bias_reducedgives standard errors[nan nan nan]andkauermann_carrollaLinAlgErrorfromsqrtm; on main both raise the bareLinAlgError; with this change both raise aValueErrorthat says that the covariance of the parameters is not finite, so that the leverages needed bycov_type="..."cannot be computed. (cov_type="robust"is alsoNaNin that fit, so the message does not suggest it.) Onenp.isfinite(cov_naive).all()check at the top of_bc_covmat.DOC:thefitdocstring has aRaisesentry with.. versionchanged:: 0.15.1for theValueError(a group with leverage 1, and the non-finite case) and says that a leverage larger than1 - 1e-8counts as 1. Checked with a dummy jittered by hand and a leverage matrix built in NumPy:1 - leverageof 5.2e-9 raises, 4.7e-8 and 5.2e-7 do not. The release note says 'for some families' instead of 'for the Gaussian family': on the code before BUG: Follow up #10790 on GEE marginal effects and small-sample covariances #10792 Binomial with independence gave standard errors of 9.0e15 (bias_reduced) and 9.5e7 (kauermann_carroll), Poisson with an exchangeable correlation and groups of 4 gave[nan nan 1.3e23], and Gaussian groups of 4 gave 6e17 and 3.6e16; it also says that the non-finite covariance raises the sameValueError. The docstring parses with numpydoc (checked in the Windows environment).TST:the leverage test passed when the p x p matrix was replaced bydmat.T @ dmat, when the message named the group position instead of its label (the labels were 0 to 10, the positions), and when thecov_typein the message was wrong. The groups now have string labels (site_k) and the message is matched with the label and thecov_type; a newtest_small_sample_cov_leverage_exchangeable(Gaussian,Exchangeable) compares the check with the largest eigenvalue of the full leverage matrix built in NumPy, for an exact dummy, a jitter of 1e-5 (raises) and 1e-4 (does not);test_small_sample_cov_naive_not_finitecovers the non-finite case without relying onLinAlgError(Pyodide does not raise it);from scipy import statsis a module-level import instead of three imports inside tests.Mutation check (the tree mutated, restored afterwards; each caught by the test named):
dmat.T @ dmatinstead of the p x p matrix failstest_small_sample_cov_leverage_exchangeable[exact-kauermann_carroll]; the group position instead of the label failstest_small_sample_cov_leverage_one[Poisson-bias_reduced]; a wrongcov_typename fails[Gaussian-kauermann_carroll]; the threshold1 - 1e-3fails the 'outside 1e-8' case and1 - 1e-12the 'within 1e-8' case; removing the finite check failstest_small_sample_cov_naive_not_finite[kauermann_carroll].Not changed (noted in the review)
bias_reducedandkauermann_carroll, which raiseValueError: operands could not be broadcast together with shapes (4,) (3,)with unit weights and groups of 4 with 3 parameters, on the code before BUG: Follow up #10790 on GEE marginal effects and small-sample covariances #10792 and on main;NominalGEE.get_margeff()with 3 or more categories; the whitespace-only line 326 ofgeneralized_estimating_equations.py(W293, already on main; left alone to avoid a conflict with the other GEE follow-ups).Tests
statsmodels/genmod/tests/test_gee.pyandtest_gee_glm.py: 201 passed on Python 3.13; 198 passed and 3 skipped (no matplotlib) on Python 3.10 (NumPy 1.23.5, SciPy 1.8.1).ruff checkof the changed test file andgit diff --checkpass.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).
Used for: reviewing BUG: Follow up #10790 on GEE marginal effects and small-sample covariances #10792, and writing and checking the follow-up changes.
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