Repository navigation
BUG: Use unwrapped results in MixedLM.fit_regularized - #10465
Merged
Merged
Conversation
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
Member
|
Thanks. |
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.
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 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.
NumPy's guide.
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 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.