Skip to content

BUG: Follow up #10792 on GEE small-sample covariances, docs and tests - #10802

Merged
bashtage merged 3 commits into
statsmodels:mainfrom
bashtage:wip-gee-margeff-followup
Nov 19, 2026
Merged

bashtage merged 3 commits into
statsmodels:mainfrom
bashtage:wip-gee-margeff-followup

Conversation

@bashtage

Copy link
Copy Markdown
Member

Follow up to #10792 (get_margeff with use_t, the margins summary, and the leverage check of the small-sample covariances of GEE). 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.

  • tests added / passed.
  • code/documentation is well formatted.
  • properly formatted commit message. See
    NumPy's guide.

Decision for the maintainer. The BUG: commit (the first one) changes what GEE.fit(cov_type="bias_reduced") and "kauermann_carroll" do when the covariance of the parameters is not finite: it raises a ValueError where #10792 raised a bare LinAlgError (and the code before #10792 returned NaN standard errors for bias_reduced). If you prefer to keep returning NaN there, 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 in GEE._bc_covmat raised LinAlgError: Array must not contain infs or NaNs when cov_naive is not finite. Reproduced with Poisson and an exchangeable working correlation, one observation with y = 0 and 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_reduced gives standard errors [nan nan nan] and kauermann_carroll a LinAlgError from sqrtm; on main both raise the bare LinAlgError; with this change both raise a ValueError that says that the covariance of the parameters is not finite, so that the leverages needed by cov_type="..." cannot be computed. (cov_type="robust" is also NaN in that fit, so the message does not suggest it.) One np.isfinite(cov_naive).all() check at the top of _bc_covmat.
  • DOC: the fit docstring has a Raises entry with .. versionchanged:: 0.15.1 for the ValueError (a group with leverage 1, and the non-finite case) and says that a leverage larger than 1 - 1e-8 counts as 1. Checked with a dummy jittered by hand and a leverage matrix built in NumPy: 1 - leverage of 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 same ValueError. The docstring parses with numpydoc (checked in the Windows environment).
  • TST: the leverage test passed when the p x p matrix was replaced by dmat.T @ dmat, when the message named the group position instead of its label (the labels were 0 to 10, the positions), and when the cov_type in the message was wrong. The groups now have string labels (site_k) and the message is matched with the label and the cov_type; a new test_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_finite covers the non-finite case without relying on LinAlgError (Pyodide does not raise it); from scipy import stats is 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 @ dmat instead of the p x p matrix fails test_small_sample_cov_leverage_exchangeable[exact-kauermann_carroll]; the group position instead of the label fails test_small_sample_cov_leverage_one[Poisson-bias_reduced]; a wrong cov_type name fails [Gaussian-kauermann_carroll]; the threshold 1 - 1e-3 fails the 'outside 1e-8' case and 1 - 1e-12 the 'within 1e-8' case; removing the finite check fails test_small_sample_cov_naive_not_finite[kauermann_carroll].

Not changed (noted in the review)

  • The absolute tolerance of 1e-8 (badly scaled designs can raise falsely; a relative tolerance is a design change); weighted bias_reduced and kauermann_carroll, which raise ValueError: 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 of generalized_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.py and test_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 check of the changed test file and git diff --check pass.

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.

  • No AI tools were used to develop this pull request.
  • AI tools were 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

@bashtage
bashtage force-pushed the wip-gee-margeff-followup branch from b2f0375 to a144ece Compare November 19, 2026 11:03
@bashtage
bashtage marked this pull request as ready for review November 19, 2026 16:20
bashtage and others added 3 commits November 19, 2026 17:55
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
bashtage force-pushed the wip-gee-margeff-followup branch from a144ece to 9d2f408 Compare November 19, 2026 17:56
@bashtage
bashtage merged commit ba594f9 into statsmodels:main Nov 19, 2026
13 of 14 checks passed
@bashtage
bashtage deleted the wip-gee-margeff-followup branch November 19, 2026 23:14
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.

1 participant