Skip to content

BUG: Use unwrapped results in MixedLM.fit_regularized - #10465

Merged
bashtage merged 1 commit into
statsmodels:mainfrom
shan3520:bug-8902-mixedlm-regularized
Oct 6, 2026
Merged

bashtage merged 1 commit into
statsmodels:mainfrom
shan3520:bug-8902-mixedlm-regularized

Conversation

@shan3520

@shan3520 shan3520 commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

The Problem

This PR fixes #8902, where calling MixedLM.from_formula(...).fit_regularized(...).summary() resulted in a KeyError (0, 0). On current main with pandas 3, the problem occurs even earlier, as fit_regularized fails immediately with a KeyError: 0.

Cause

The issue is caused by fit_regularized which internally invokes self.fit(), retrieving some properties such as fe_params, cov_re, and params. In case of pandas and formula models, fit() returns wrapped results (pandas Series and DataFrames with string index). Since fit_regularized uses positional integer indexing (e.g., fe_params[j]), it raises a KeyError in pandas 3 (while pandas 2 only issued a warning). Passing the wrapped pandas objects further into regularized results is the cause of the original cov_re[i, j] KeyError within summary().

The Solution

The solution is to access the underlying numpy arrays using self.fit(**fit_kwargs)._results instead. It works the same way as fit() itself internally. The return values will be properly wrapped into named Series/DataFrames again.

Models based on numpy arrays did not suffer from the problem at all. Also since fit_regularized tests currently use .values, pandas path was not tested.

Tests and Context

Additional tests (test_fit_regularized_pandas) comparing formula models against numpy models with alpha penalties 0, 1, and 40 check the following conditions:

  • The result of the formula path and numpy path is the same in terms of fe_params, params, bse, cov_re and scale.
  • fe_params is returned as a Series with variable names. cov_re is returned as a DataFrame.
  • Regularization works correctly (alpha 40 regularizes out x1 completely, thus the standard error of x1 is NaN).
  • summary() does not raise an error.

The tests fail on main, but pass with the patch applied. All 182 MixedLM tests pass. No reference to external R was used since the test compares the pandas path against already tested numpy path.

Note: As kshedden mentioned in 2023, the output of summary() is meaningless in general for regularized fits. This PR does not alter the output of summary(), just makes it runnable and produces the same output as in the case of numpy models, where summary() after fit_regularized was working fine.

Closes #8902.

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 Opus 5.5).
    Used for: drafting the fix and tests.
    I have personally read, understood, and can explain every line of this diff, and
    have verified the statistical/numerical correctness of the change.

fit_regularized took fe_params, cov_re and params from the wrapped
results of the unpenalized fit. These are pandas objects when the model
has pandas data, so positional indexing raised KeyError in the
coordinate descent loop, and the pandas objects stored in the results
made summary fail. Use the unwrapped results so the estimates are
arrays, as in fit.

closes statsmodels#8902
@bashtage

bashtage commented Oct 6, 2026

Copy link
Copy Markdown
Member

Thanks.

@bashtage
bashtage merged commit 2de08d8 into statsmodels:main Oct 6, 2026
13 checks passed
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.

KeyError: (0, 0) for summary() method of MixedLMResultsWrapper

2 participants