Repository navigation
BUG: Fix missing="drop" in MixedLM - #10467
Merged
Merged
Conversation
Contributor
Author
|
ready for review |
Contributor
Author
|
Hi @bashtage, update regarding this very small change of only +106 lines of code in total in two files needed for failing issue #7978, MixedLM fails on missing='drop' with NumPy and pandas data but smf.mixedlm works perfectly well, seems to be OK, since all the tests are passing and is cleanly mergable. Also this will help in fixing the skipped test introduced by me in #10524. |
bashtage
requested changes
Oct 23, 2026
bashtage
left a comment
Member
There was a problem hiding this comment.
I think reord the the method to be static woudl be cleaner.
| _wrap_methods = base.wrap.union_dicts(_methods, _upstream_methods) | ||
|
|
||
|
|
||
| def _drop_missing_rows(*arrays): |
Member
There was a problem hiding this comment.
Can you make this a static method of the main class? I don't think this has a lot of generality, since missing is normally handled somewhere else. From a housekeeping POV, it is simpler to keep it inside the class.
MixedLM passed groups and exog_re to the upstream missing data handling but kept using the original groups, and the upstream handling drops rows and columns of 2d arrays, which is not correct for exog_re. Drop the rows with missing values in MixedLM before calling super, as from_formula already does, and raise when rows are dropped and exog_vc is used. closes statsmodels#7978
shan3520
force-pushed
the
bug-7978-mixedlm-missing
branch
from
October 23, 2026 12:50
59d17ab to
dbec904
Compare
bashtage
approved these changes
Oct 23, 2026
1 of 2 tasks
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.
This PR fixes #7978 where initializing a MixedLM model with missing="drop" using numpy or pandas inputs causes an IndexError on main (eg "index 58 is out of bounds for axis 0 with size 58"). The formula interface (smf.mixedlm) was not affected since it pre-processes the missing rows before initializing the model (#3591).
Two bugs were at play here. First, while the base Model drops the rows with missing observations in groups and exog_re, MixedLM still relied on the initial groups array which caused out of bounds indexing of the truncated arrays. Second, the missing observation logic of the base class assumes that any extra 2D array is a square nobs x nobs covariance matrix like in GLS and therefore removes rows and columns. This is incorrect for exog_re.
The fix consists of adding a helper function that drops rows with missing observations in endog, exog, groups and exog_re before calling super().init(). After that, the model calls the base class with missing="none". This mimics what happens in from_formula and preserves the pandas indices.
Since the exog_vc is provided as a list of group-specific arrays, it is impossible to remove single rows from it. Therefore, if missing="drop" is chosen and there are missing observations in combination with exog_vc, the model now raises a ValueError. The behavior of missing="none" and missing="raise" is completely unaffected.
Four tests are added that check how the number of observations, groups, parameter estimates, standard errors and log likelihood of numpy/pandas models with and without exog_re compare to those in a model fitted using the prepared complete-case dataset. In addition, a test checking the ValueError with exog_vc is added. Five tests fail on main and pass on this branch; all 187 MixedLM tests pass. There is no external R reference used in the tests since they only compare the missing="drop" case to the complete-case fit which is compared to R in test_lme.
missing#7978NumPy'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.