Skip to content

BUG: Fix missing="drop" in MixedLM - #10467

Merged
bashtage merged 2 commits into
statsmodels:mainfrom
shan3520:bug-7978-mixedlm-missing
Oct 23, 2026
Merged

bashtage merged 2 commits into
statsmodels:mainfrom
shan3520:bug-7978-mixedlm-missing

Conversation

@shan3520

@shan3520 shan3520 commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

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.

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.

@shan3520

shan3520 commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

ready for review

@shan3520

Copy link
Copy Markdown
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 bashtage left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

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
shan3520 force-pushed the bug-7978-mixedlm-missing branch from 59d17ab to dbec904 Compare October 23, 2026 12:50
@shan3520
shan3520 requested a review from bashtage October 23, 2026 13:31
@bashtage
bashtage merged commit aa61cc1 into statsmodels:main Oct 23, 2026
12 of 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.

MixedLM cannot handle missing

2 participants