Skip to content

Generalized Linear Mixed Model - #2516

Open
gjhunt wants to merge 24 commits into
statsmodels:mainfrom
gjhunt:glmm
Open

gjhunt wants to merge 24 commits into
statsmodels:mainfrom
gjhunt:glmm

Conversation

@gjhunt

@gjhunt gjhunt commented Jul 10, 2015

Copy link
Copy Markdown

Poisson and Binomial with cannonical links working. Non cannonical links not working yet. Gaussian not working.

edit (Josef)

  • we need to squash the first 4 commits before merge, up to 0685f2f, to remove redundant copy of modules.

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.

Using centered=True option will give higher accuracy at the minimum or maximum

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks. Added.

@josef-pkt

Copy link
Copy Markdown
Member

Can you add an example to statsmodels/examples (inside the source tree), so it's faster to try out an example. Also if you use an R script to generate reference numbers than you can commit it inside the test directory.

@josef-pkt

Copy link
Copy Markdown
Member

Are there famous examples with public domain or non-restrictively licensed datasets that we can add for docs and unit tests, e.g. Binomial and Poisson?

@josef-pkt

Copy link
Copy Markdown
Member

@gjhunt Do you have a list of references on which the implementation is based upon?

We need references especially for the Laplace approximation both for review and maintenance internally, and also for users so they can check or learn what this actually does.

@gjhunt

gjhunt commented Jul 18, 2015

Copy link
Copy Markdown
Author

@josef-pkt @kshedden

I'll look at finding some examples. I don't know if we can use the data that is used in the LME4 examples or vignettes? Those might be ideal but I'm not sure what the restrictions on using them are.

Honestly the reference I've been using for the implementation is conversations with Kerby. I've been looking at some lecture notes form Michael Jordan for the Laplace approximation (http://www.cs.berkeley.edu/~jordan/courses/260-spring10/lectures/lecture16.pdf). I can look at getting some better references if we need them. I'll probably have to ask Kerby for what he views as good references.

@kshedden

Copy link
Copy Markdown
Contributor

A document in progress attempting to derive the exact Laplace approximation for both canonical and non-canonical links:

https://umich.box.com/shared/static/cc38q61ipjd6mow2o8rzm5zgxstf6od4.pdf

If this derivation is correct, then we need to add two things to the genmod base classes if we want to be able to use them with mixed models and non-canonical links:

  • The derivative of each variance function with respect to its argument (the mean).
  • The inverse of the derivative of gamma, as defined in the document linked above.

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.

you can disable tests by changing the name instead of commenting out, that's less interuptive for the change history.
I often just add an underline like t_est_laplace_loglike_gamma or drop the "t" in "test", then nose will not identify as a unit tests

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Cool. I'll do that for future commits.

@gjhunt

gjhunt commented Jul 25, 2015

Copy link
Copy Markdown
Author

Updated. I got a correctly implemented gradient and hessian for the Laplace approximation sub-problem working for non-canonical links. It is noticeably faster than using the (albeit very accurate) numerical approximations. I need to now add some more tests for the log-like and add some tests showing the .fit() function. Things that might need to be fiddled with are the two optimization problems (1) for Laplace and (2) for .fit(). They can be temperamental. Although I'm highly confident that the things we are passing to them are correctly now. The current tests seem to pass well in the canonical cases however they still aren't matching quite perfectly for the non-canon cases. Its possible (probable?) that glmer is using an approximation to the Laplace approximation so I'll see if I can find anything about that. Something like that might explain the discrepancy. I'm fairly confident in these results so I'm starting to think that glmer is doing something different than a straight-up Laplace approximation. Strangely the log-like seems to be accurate for non-canon cases when the number of groups is small but diverges when this gets large. I'll look into that more.

Comment thread statsmodels/genmod/families/family.py Outdated

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

So this is really a hack right now because MixedGLM assumes the log-likelihood to be in the normal form. We should probably support Gaussian case though? In which case this needs to be changed if it hasn't already in an updated version? I think I'm on .7 right now.

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.

Kerby has prepared PR #2353
I wanted to postpone it for 0.8 because I'm not sure about the API and impact.
I see if I can figure out #2353 in the next few days with examples and unit tests. #2353 will be merged before MixedLM, so this can always be adjusted later.

@kshedden

Copy link
Copy Markdown
Contributor

For the binomial variance function it should be easy to use the analytic derivative.

@kshedden

Copy link
Copy Markdown
Contributor

Do you have a derivation for your derivative and Hessian? I think what I derived earlier is the derivative and Hessian with respect to beta. We need the derivative and Hessian with respect to the random effects, right?

@kshedden

Copy link
Copy Markdown
Contributor

We should retain the earlier simple canonical calculation and use it when applicable in the Laplace calculations. You can create a private variable _canonical_link for each family (e.g. _canonical_link = statsmodels.genmod.families.links.logit for Binomial family), then set a variable to true/false in the GLM class:

self._canonical = isinstance(self.family.link, sm.family._canonical_link).

If self._canonical is True you would use the old code that is simple and fast.

It would also be good to come up with a test where you use the non-canonical code in a setting where the link is actually canonical, then compare the results using the old and new code. You could do that by creating a version of the link class that is not recognized as being the canonical link.

@gjhunt

gjhunt commented Aug 11, 2015

Copy link
Copy Markdown
Author

So I added a GLM fit which initializes the start_params. I'm not sure it fixes or problems. I just added some fit tests comparing some canonical cases to R. As you can see the negative binomial test fails to fit even with the new GLM initialization.

@josef-pkt

Copy link
Copy Markdown
Member

Viewed from the other side: Does this mean Gaussian, Binomial and Poisson work correctly?

Those three are the important ones that are reasonably easy to optimize. The others might have messy optimization problems that need specific attention (after the GSOC).

@gjhunt

gjhunt commented Aug 11, 2015

Copy link
Copy Markdown
Author

The short answer is yes. For canonical links those three fit correctly and quickly.

@josef-pkt

Copy link
Copy Markdown
Member

For canonical links those three fit correctly and quickly.

Very good.
How well do non-canonical links work for Gaussian and Binomial? Which if any work?

I just remembered: R GLM doesn't have cloglog, R has a misnamed loglog which we don't have yet (open issue IIRC).

@gjhunt

gjhunt commented Aug 11, 2015

Copy link
Copy Markdown
Author

So for small data sets it can definitely fit some non-canonical links for those. Its a little more complicated than "which links work?". Things start to get difficult if the data gets too large. So for some links they work for small data sets and not for larger ones. Its hard to tell the accuracy because I don't have much to compare it against since R uses some approximation for non-canonical links.

I added a Gaussian fit example which unfortunately isn't quite working yet. Not clear to me why the basic Gaussian case should be failing.

@josef-pkt

Copy link
Copy Markdown
Member

General question: Does R also have adaptive quadrature as a numerical integration option?
I was looking at an article that uses 7 point adaptive gaussian quadrature as base reference for the accuracy of Laplace approximation and penalization approximation.

@josef-pkt

Copy link
Copy Markdown
Member

What I suggest given that we are running out of time for GSOC right now:

Focus on the 3 cases that work: Gaussian, Binomial and Poisson with canonical link, and get the missing results (like bse/cov_params) and test coverage compared to R for those cases.
Then we have the basic functionality working and unit tested, and we can then slowly expand on the parts that don't work properly or where we don't have an exact equivalent in R for reference.

@gjhunt

gjhunt commented Aug 12, 2015

Copy link
Copy Markdown
Author

That sounds like a good plan. I'll try to finish these up.

@gjhunt

gjhunt commented Aug 12, 2015

Copy link
Copy Markdown
Author

What's the thoughts on leveraging some parallel computing for this stuff? the multiprocessing library is part of python's standard library. For larger data sets we might be able to justify the overhead. There are some embarrassingly parallel problems here e.g. computing the means for each point or even looping over groups. We could take advantage of multi-core processors this way. This wouldn't solve convergence problems but could give a speed-up.

@josef-pkt

Copy link
Copy Markdown
Member

@gjhunt Yes, that will be a possibility to add. We have an optional dependency on joblib for parallel processing. (I guess it would also be a good candidate for out of core processing because of the embarrassingly parallel structure.)

However, I wouldn't add it in the first round when we still need to fix some problems, because it would make that more difficult.

@gjhunt

gjhunt commented Aug 12, 2015

Copy link
Copy Markdown
Author

Yup. I think its best to try and get these convergence issues fixed.

@jona-sassenhagen

Copy link
Copy Markdown

Is this still being worked on? It would be amazing to have a fully-featured LMM in statsmodels.

@kshedden

Copy link
Copy Markdown
Contributor

Yes, I'm hoping to get this usable within a month or so. This is GLMM, our
LMM is already somewhat fully-featured (see MixedLM).

On Sun, Jan 10, 2016 at 9:06 PM, jona-sassenhagen [email protected]
wrote:

Is this still being worked on? It would be amazing to have a
fully-featured LMM in statsmodels.

—
Reply to this email directly or view it on GitHub
#2516 (comment)
.

@jona-sassenhagen

Copy link
Copy Markdown

Cool.

The features I'm most missing would indeed be generalized linear mixed models plus crossed random effects. Both are huge right now in linguistics and psychology.

@kshedden

Copy link
Copy Markdown
Contributor

We have decent support for crossed linear mixed models now. If you try it
out and have trouble file an issue or post to the statsmodels list and we
will try to help out.

Mixed GLM is not really a "feature", it is an entirely different code base
from the ground up. But we should be able to build up to something usable
over time.

On Sun, Jan 10, 2016 at 9:15 PM, jona-sassenhagen [email protected]
wrote:

Cool.

The features I'm most missing would indeed be generalized linear mixed
models plus crossed random effects. Both are huge right now in linguistics
and psychology.

—
Reply to this email directly or view it on GitHub
#2516 (comment)
.

@jona-sassenhagen

Copy link
Copy Markdown

We have decent support for crossed linear mixed models now

Nice!

@mmagnuski

Copy link
Copy Markdown

@kshedden

We have decent support for crossed linear mixed models now.

It should be corrected in the documentation then, I was not using statsmodels LMM before because I needed crossed random effects and the docs say:

Some limitations of the current implementation are that it does not support structure more complex on the residual errors (they are always homoscedastic), and it does not support crossed random effects.

@kshedden

Copy link
Copy Markdown
Contributor

@mmagnuski I will change the docs. It remains unclear how powerful our implementation is when it comes to crossed effects. We do not use sparse Cholesky like R and some other packages. We do have some other tricks to help with performance.

@pzivich

pzivich commented Feb 7, 2018

Copy link
Copy Markdown

Is GLMM implementation still in progress?

@kshedden

kshedden commented Feb 7, 2018 via email

Copy link
Copy Markdown
Contributor

@josef-pkt

Copy link
Copy Markdown
Member

This pull request introduces 14 alerts when merging 76b63d0 into b5cb3de - view on lgtm.com

new alerts:

  • 10 for Module-level cyclic import
  • 1 for Wrong name for an argument in a call
  • 1 for Missing call to __init__ during object initialization
  • 1 for Variable defined multiple times
  • 1 for Redundant assignment

Comment posted by lgtm.com

@emjun

emjun commented Mar 3, 2021 •

Copy link
Copy Markdown

It looks like the Bayesian implementation of GLMMs is a great start but incomplete.

What is the latest effort for supporting more families in GLMMs?

I am just digging into this part of the code base, but would a reasonable implementation approach be to port/reuse some of the family (link and variance) calculations in the fit method for MixedLM to "prep" the model for fitting in GLM ?

[Apologies if this seems like a rhetorical/naive question, really just trying to see how I can contribute] Conceptually, a GLMM can be thought of as a GLM with mixed effects, so it seems like we should be able to do some "matrix expansion" to include the random intercepts and slopes before using the GLM fitting procedure. I know that we might need a new fitting algo (which it seems the Bayesian implementation provides), but is there any reason we can't do this in GLM class? Am I missing something about the complexity of incorporating mixed effects in the the current GLM class implementation?

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.

7 participants