Skip to content

WIP linear mixed models - #1398

Closed
kshedden wants to merge 73 commits into
statsmodels:masterfrom
kshedden:lme
Closed

kshedden wants to merge 73 commits into
statsmodels:masterfrom
kshedden:lme

Conversation

@kshedden

Copy link
Copy Markdown
Contributor

This is an implementation of linear mixed models, in the style of Laird & Ware (as found in R lme4, SAS proc mixed, etc.). I don't know of other work on this in statsmodels, so this starts from scratch.

Lots of work is still needed, but it converges, and the results agree with R/lme4 on a number of examples.

Major issues:

  • Only implements MLE, still needs REML.
  • Uses numeric second derivative to get standard errors. This saves a lot of work and the results agree with lme4 at least 1-2 sig figs.
  • EM and conjugate gradient are implemented. A few EM iterations (hopefully) get a good starting value, then CG takes over. There are many other algorithms for fitting LME's that we can explore. I don't know the details of what is done in other packages.
  • Speed can probably be improved, both from streamlining the linear algebra, and from improving the handoff between EM and CG iterations.
  • Very bare results class.
  • Behavior when the MLE of a variance component is on the boundary needs improvement (will always be difficult).
  • Almost no post-estimation results (AIC, LRT, prediction, etc.) are implemented. Some of these are tricky for mixed models. BLUP's for the random effects aren't too hard.
  • Doesn't work with formulas. We need to devise a way for two formulas to be passed (one for the fixed effects structure, one for the random effects structure).
  • Testing coverage is minimal.

@josef-pkt

Copy link
Copy Markdown
Member

Good to see this. I never really understood mixed models in R.

background question:
We have a mixed model in the sandbox that we mostly inherited from Jonathan Taylor, pre-statsmodels time.
https://github.com/statsmodels/statsmodels/blob/master/statsmodels/sandbox/panel/mixed.py
It's based on Laird, Lange and Stram
https://github.com/statsmodels/statsmodels/blob/master/statsmodels/sandbox/panel/mixed.py#L33

I fixed it so it seems to work, but I never figured out what the equivalent in R or other packages is, so I could never write unit tests to check what the results are really supposed to be.
I didn't do any big refactoring, and it still uses a separate class for each Unit.
As far as I could figure out, it assumes unstructured correlation between random effects.

Do you know what the main difference between Laird/Ware and Laird/Lange/Stram is?
Does it make sense to reuse some parts of sandbox Mixed in this?
Does it make sense to keep sandbox Mixed and refactor it in the same pattern as your mixed model?

If you have a rough idea how useful the sandbox Mixed is, then we can either refactor or delete it.

Mixed models are one of the main missing models, but so far none of us understood the statistics well enough to get an implementation for it.

@kshedden

Copy link
Copy Markdown
Contributor Author

Thanks, somehow I missed that file when searching.

I think we are fitting the same model (that is, LairdWare and Laird/Lange/Stram are using the same model). The latter gets into connections between EM and IRLS, and talks about ways to accelerate EM.

I'm not that knowledgeable about this area, but I think the other packages (like LMER) have not made much use of these modified EM strategies, but instead use gradient methods.

The sandbox implementation is all EM, whereas mine uses a few EM steps followed by gradient methods. The latter should be much faster, and having an analytic score function is useful for other purposes. EM is very useful for getting a rough starting value.

The sandbox code uses some sort of IRLS idea to get standard errors that I'm not very familiar with. I am using the numerical Hessian.

If you want to test the sandbox code, it should be possible to use the same examples that are in my test set.

Both implementations use an unstructured random effects covariance. In the gradient setting, certain structural constraints (like independent variance components) are easy to incorporate.

@josef-pkt josef-pkt added the PR label Feb 19, 2014
Comment thread statsmodels/regression/lme.py Outdated

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.

cov_re is undefined according to pylint or eclipse.pydev

@kshedden

Copy link
Copy Markdown
Contributor Author

I fixed the issue with the notebook and did some renaming and other cleanup per suggestions.

Comment thread statsmodels/regression/lme.py Outdated

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.

suggested rename num -> niter niter_sd, niter_em

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.

just a note: I would find fixed better than free, but we don't have a convention yet.
We will eventually need a consistent pattern for fixing parameters in the API for user.

@dengemann

Copy link
Copy Markdown
Contributor

@josef-pkt @kshedden Amazing. Will pull and take a look soonish.

@josef-pkt

Copy link
Copy Markdown
Member

parking a reference
http://psycnet.apa.org/journals/met/17/1/15/
Selecting a linear mixed model for longitudinal data: Repeated measures analysis of variance, covariance pattern model, and growth curve approaches.
Liu, Siwei; Rovine, Michael J.; Molenaar, Peter C. M.
Psychological Methods, Vol 17(1), Mar 2012, 15-30. doi: 10.1037/a0026971

looks like a good overview of different assumptions on error correlation structure (and random coefficients) for mixed, repeated measures anova and gee type correlation patterns.
I mixed up mixed style random coefficients with gee style correlation patterns when I worked on sandbox Mixed.

mixed in this PR assumes epsilon is iid (scale_identity covariance matrix), which would make it a pure random coefficient model I guess. (_)

(In sandbox Mixed I try to fake a patterned epsilon covariance matrix by choosing "funny" Z dummies.) Compound symmetry is equivalent to random intercept IIRC.

(*) random coefficient (and multilevel) models could be more general than mixed because RC doesn't necessarily assume independence across panel units.

(I'm still trying to translate "statistics" into something that I know or have some intuition about.)

@kshedden

Copy link
Copy Markdown
Contributor Author

We will need to extend MixedLM to accommodate heteroscedastic epsilon. I
intentionally left this out for now. I think the best approach is to have
the user provide a third formula or design matrix specifying the log
variances of epsilon in terms of explanatory covariates.

Non-diagonal structure for cov(epsilon) is getting more into what I
consider SEM rather than mixed models. I have some vague plans to write
code for SEM at some point.

Dependence between groups (I think your "panels" are my "groups") is
complicated from an implementation point of view, because the code relies
so heavily on splitting everything across the groups. The main use case
that I know of is "crossed" random effects. LMER supposedly has an
efficient way to do this. At some point I'll look into how they do it.

@josef-pkt

Copy link
Copy Markdown
Member

Yes, I don't expect we will have everything in one model or implemented very fast.
It's more to get an overview what the underlying assumptions of the different models are.

Panel PR also implements the simple two random factor model with one random constant per group (panel unit) and one random factor that is a time effect that is random over time but common to all groups (panel units). I don't think I have ever seen more than two "crossed" random factors.

Related: similar to random coefficient models
For arbitrary stochastic restriction on parameters, I have a generalized Ridge Regression, that allows to specify priors on all parameters or linear combinations of parameters. It just solves the expanded normal equations and doesn't need any (nobs by nobs) matrices. (IIRC)
It doesn't try to estimate or choose the prior weights, ridge penalization factors, which I hadn't figured out.

I had used it to impose closeness of parameters. For example in the longitudinal setting, we might have parameters/coefficients that change over time, but we want to impose a prior that the parameters don't change too much from period to period.
(When I read one of Gelman's blog posts, I thought this is just an example of what they consider multi-level models, which I had never heard of before.)
(assumes we work with normal prior, likelihood and posterior as approximation to the true distributions, or it's just penalized least squares with stochastic linear restrictions.)

@josef-pkt

Copy link
Copy Markdown
Member

@kshedden In the last commit you renamed the statsmodels/base/penalties.py module but you forgot to commit the removal of the old one, now both are in the repo.

Can you amend the last commit, removing penalties.py, and force push?

@kshedden

Copy link
Copy Markdown
Contributor Author

Done.

On Tue, May 20, 2014 at 11:10 PM, Josef Perktold
[email protected]:

@kshedden https://github.com/kshedden In the last commit you renamed
the statsmodels/base/penalties.py module but you forgot to commit the
removal of the old one, now both are in the repo.

Can you amend the last commit, removing penalties.py, and force push?

—
Reply to this email directly or view it on GitHubhttps://github.com//pull/1398#issuecomment-43638917
.

@josef-pkt josef-pkt added this to the 0.6 milestone May 22, 2014
@josef-pkt

Copy link
Copy Markdown
Member

@dengemann Any comments? Did you have a chance to look at it?

I'm planning to rebase, and merge after a quick look at it, within a few days or a week.
Overall looks good, but I never went through all the details, and expect some followups once it sees wider use.

@josef-pkt

Copy link
Copy Markdown
Member

for rebase:

  • includes dot_plot branch, first commit for this PR is "initial commit" e8921af
  • need to squash the renaming of penalties (2 commits)
  • overlap with solution_path: e430423 to bc8bcfc
    I think we can ignore the temporary solution_path in here, it's in a different module from the one in the new PR

@dengemann

Copy link
Copy Markdown
Contributor

@josef-pkt please go ahead, I don't have time to review in depth at this point. I'm happy to comment on master later.

@josef-pkt

Copy link
Copy Markdown
Member

@kshedden I looked at your notebook examples again: Is the constant/intercept always included in the random coefficients or can it be excluded?

@josef-pkt

Copy link
Copy Markdown
Member

rebased version is in #1688

@josef-pkt josef-pkt closed this in 6ff0f30 May 24, 2014
@kshedden

Copy link
Copy Markdown
Contributor Author

If exog_re is supplied explicitly then no intercept is added. But under
the default, when no exog_re is specified, exog_re becomes an intercept.

On Sat, May 24, 2014 at 4:15 AM, Josef Perktold [email protected]:

@kshedden https://github.com/kshedden I looked at your notebook
examples again: Is the constant/intercept always included in the random
coefficients or can it be excluded?

—
Reply to this email directly or view it on GitHubhttps://github.com//pull/1398#issuecomment-44055440
.

PierreBdR pushed a commit to PierreBdR/statsmodels that referenced this pull request Sep 2, 2014
@kshedden
kshedden deleted the lme branch September 27, 2014 14:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants