Skip to content

ENH: mixed effects / repeated measures #646

Open

Description

@dengemann

How is this evolving in the moment? Looking at the sandbox there does not seem to be too much change.
I've got a couple of use cases for which I ideally need to report 'repeated measures' analysis
Also for quite some time I've been thinking about implementing a general solution applicable to to cognitive neuroscience / physiological data.
So typically it should handle scenarios like: Y ~ within_a x within_b x between_j. Y can be button press latencies, error rates, etc., or brain responses. In that case Y has to be estimated at location l, time t (and frequency f).
I might give this a shot if no one is deeply devoted to this during the next weeks or so.

Activity

  1. josef-pkt commented on Feb 5, 2013

    @josef-pkt
    Member

    Nothing going on at the moment, I haven't worked on this since #145 .

    When I worked on this, I didn't have a specific use case in mind, so some of the code is all over, preparing for different models in repeated measures, mixed effects, panel data and GLS with estimated error structure.

    Much of the basics/infrastructure for repeated measures in the ANOVA style is missing, however, there is quite a lot of code to estimate with non-identity error structure, going into the direction towards GEE.

    It wasn't on my schedule to work on this, and nobody else is, so give it a shot.
    I can help, especially if you need to find your way around the existing code.

    Questions that will affect how to efficiently implement estimation for specific use cases relate to the structure of your data.
    Do you have many observations per individual and few individuals (long panel), or few observations per individual and many individuals (wide panel)? balanced or unbalanced (same number of observations per individual or not)?

    For the balanced case, there are also multivariate models in the sysreg pull request (GSOC).

    This area is a bit large, but we don't have to worry about all different versions.
    I think it would be good to get one use case out of the sandbox. Also, some of it may not be relevant for your initial repeated measures case, but might come in handy for later extensions.

    Have fun and code :)

  2. dengemann commented on Feb 5, 2013

    @dengemann
    ContributorAuthor

    Yay,

    On Tue, Feb 5, 2013 at 3:34 PM, Josef Perktold [email protected]:

    Nothing going on at the moment, I haven't worked on this since #145#145.

    When I worked on this, I didn't have a specific use case in mind, so some
    of the code is all over, preparing for different models in repeated
    measures, mixed effects, panel data and GLS with estimated error structure.

    Much of the basics/infrastructure for repeated measures in the ANOVA style
    is missing, however, there is quite a lot of code to estimate with
    non-identity error structure, going into the direction towards GEE.

    It wasn't on my schedule to work on this, and nobody else is, so give it a
    shot.
    I can help, especially if you need to find your way around the existing
    code.

    Great, I'll get back to you in case I start getting lost in the sandbox
    jungle ;-)

    Questions that will affect how to efficiently implement estimation for
    specific use cases relate to the structure of your data.

    Exactly, this is someting I worry about.

    Do you have many observations per individual and few individuals (long
    panel), or few observations per individual and many individuals (wide
    panel)? balanced or unbalanced (same number of observations per individual
    or not)?

    Typically my use case is of sort a) 20-30 subjects with 200-400 total
    observations. But then it depends at which level I analyze data.
    For behavioral studies, to avoid balancing issues, to keep things simple
    and to skip the tedious sum of square types i-iii debate I would aggregate
    responses. If the experiment allows for it I would e.g., take the median of
    one block (which normally gets replicate a few times) so I get 3-4
    observations per within subject factors cell (typically 2 x 2, so about 16
    observations per individual).
    It is actually quite appealing to me to model single trials. The above
    pictured strategy is just quite adaptive given the publication culture in
    certain fields of interest. Personally I'd like to move on though. For this
    I would be happy to have procedure at hand (and in Python) that allows me
    to do robust-simple-minded repeated measures and more sophisticated
    hierarchical regression models.

    The second use case is the more critical one and it is even clearer related
    to the long panel style (although data aren't represented as panel here).
    Here we would have e.g. an ndarray of subjects X conditions X time slices
    X cortical vertices (x frequency). The data matrix implied ca easily eat up
    some GB.
    For this case I'm not exactly sure how much 'repeatedness' and ANOVA I
    really need. Most often first level estimates would be generated using
    variants of a mass-univariate approach to generate a spatio-temporal
    statistical map which then is passed to a non-parametric test, often a
    clustering permutation test.
    It would be nice however to be able to specify a model allowing me to do
    inferences on the group level, not only on the individual level and also to
    test for interaction contrasts + main effects in one model. Maybe the GLM
    framework available already provides me with most of what I need. I've got
    to think about it for some more.

    Anyways, having model class that is able to reproduce 2x2 repeated models
    from R aov
    in Python and is implemented efficiently would be very neat and useful. I
    think this also might serve as another access point to statsmodels.

    For the balanced case, there are also multivariate models in the sysreg

    pull request (GSOC).

    sounds interesting.

    This area is a bit large, but we don't have to worry about all different
    versions.
    I think it would be good to get one use case out of the sandbox. Also,
    some of it may not be relevant for your initial repeated measures case, but
    might come in handy for later extensions.

    Yes, definitely, as I indicated I would like something that given the right
    parameters does repeated measures but also can do more. Let's see.

    Have fun and code :)

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/646#issuecomment-13131701..

  3. josef-pkt commented on Feb 5, 2013

    @josef-pkt
    Member

    sounds interesting

    just two comments:

    large parts of the estimation in #145 was oriented towards short and wide panels (including unbalanced for some parts). We will see how much can also be applied to long panels.

    a general idea for very large datasets:
    currently models and statistical tests want to have the full data. In many cases we could also base it on summary statistics, which could be more easily precalculated with large out-of-memory data. It's an old idea, but we haven't done anything in this direction since I haven't heard of anyone using datasets that large. possibly useful later on.

  4. dengemann commented on Feb 5, 2013

    @dengemann
    ContributorAuthor

    On Tue, Feb 5, 2013 at 4:44 PM, Josef Perktold [email protected]:

    sounds interesting

    ... not sure which expansion / unpacking algorithm to apply ;-)

    just two comments:

    large parts of the estimation in
    #145https://github.com/statsmodels/statsmodels/issues/145was
    oriented towards short and wide panels (including unbalanced for some
    parts). We will see how much can also be applied to long panels.

    Yay, I think it will boil down to learning about statsmodels style and
    already implemented estimation functions and then writing / factoring it
    from scratch. Another idea that came into my mind was to extend the GLM /
    somehow fit it in the GLM framework, maybe via subclassing / arguments /
    formula such to make it possible to specify an error term in the R aov
    style.

    a general idea for very large datasets:
    currently models and statistical tests want to have the full data. In many
    cases we could also base it on summary statistics, which could be more
    easily precalculated with large out-of-memory data. It's an old idea, but
    we haven't done anything in this direction since I haven't heard of anyone
    using datasets that large. possibly useful later on.

    I think i've got to slow down here. In most cases this stuff easily runs on
    my 16B ram / 12 core machine. But more content-related, actually I would
    need this stuff to generate first-order / level / etc. estimates. Summary
    stats would kick in when it comes to estimating population effects /
    modeling subject specific error terms.

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/646#issuecomment-13135299.

  5. josef-pkt commented on Feb 5, 2013

    @josef-pkt
    Member

    I think it would be helpful if you start a SMEP on the Wiki. If you have specific R functions and options (like aov) and examples, then it would be useful so we can get a more specific picture of where you want to go. I still have only a vague idea what's required, and then more on the estimation parts.

    Given that we have the formulas with patsy now, and Skipper's ANOVA example, it might be good to start from there and then see which parts or models are missing.

    It could be useful to have an overview, before getting lost in the "jungle".

    Specifically, do you need one of the GLM distribution families, or is it mainly linear models?

  6. dengemann commented on Feb 5, 2013

    @dengemann
    ContributorAuthor

    On Tue, Feb 5, 2013 at 5:29 PM, Josef Perktold [email protected]:

    I think it would be helpful if you start a SMEP on the Wiki. If you have
    specific R functions and options (like aov) and examples, then it would be
    useful so we can get a more specific picture of where you want to go. I
    still have only a vague idea what's required, and then more on the
    estimation parts.

    Yes, makes a lot of sense. Me too, btw., I feel I'm stepping on 'terrain
    vague'.

    Given that we have the formulas with patsy now, and Skipper's ANOVA
    example, it might be good to start from there and then see which parts or
    models are missing.

    Yes, exactly.

    It could be useful to have an overview, before getting lost in the
    "jungle".

    Yes, first starting with something more constrained might be a good idea
    (use case 1, replace R repeated measures anova for behavioral data, star
    from R / aov + patsy example as starting point). I might browse through the
    R data to find an experimental psychology test-data set exemplifying this
    common use case.

    Specifically, do you need one of the GLM distribution families, or is it

    mainly linear models?

    For the beginning I would concentrate on gaussian / linear / identity-link.
    Sometimes I've got to deal with log models, but that's very special / maybe
    for later.

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/646#issuecomment-13137809.

  7. josef-pkt commented on Feb 5, 2013

    @josef-pkt
    Member

    just remembered: UCLA and some other university websites often have good example
    I quick search found this http://www.ats.ucla.edu/stat/r/seminars/Repeated_Measures/repeated_measures.htm
    Is this along the lines of your use case?

  8. dengemann commented on Feb 5, 2013

    @dengemann
    ContributorAuthor

    that's one of my favorites, I would often give to students as example how
    to do it in R.

    On Tue, Feb 5, 2013 at 5:50 PM, Josef Perktold [email protected]:

    just remembered: UCLA and some other university websites often have good
    example
    I quick search found this
    http://www.ats.ucla.edu/stat/r/seminars/Repeated_Measures/repeated_measures.htm
    Is this along the lines of your use case?

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/646#issuecomment-13139092.

  9. dengemann commented on Feb 5, 2013

    @dengemann
    ContributorAuthor

    this is another one which is quite instructive for use case 1 (behavioral
    data):

    http://personality-project.org/r/r.anova.html

    On Tue, Feb 5, 2013 at 5:53 PM, Denis-Alexander Engemann <
    [email protected]> wrote:

    that's one of my favorites, I would often give to students as example how
    to do it in R.

    On Tue, Feb 5, 2013 at 5:50 PM, Josef Perktold [email protected]:

    just remembered: UCLA and some other university websites often have good
    example
    I quick search found this
    http://www.ats.ucla.edu/stat/r/seminars/Repeated_Measures/repeated_measures.htm
    Is this along the lines of your use case?

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/646#issuecomment-13139092.

  10. dengemann commented on Feb 5, 2013

    @dengemann
    ContributorAuthor

    ... and finally:
    http://www.psych.upenn.edu/~baron/rpsych/rpsych.html#htoc52

    sorry for the incremental updates.

    On Tue, Feb 5, 2013 at 5:57 PM, Denis-Alexander Engemann <
    [email protected]> wrote:

    this is another one which is quite instructive for use case 1 (behavioral
    data):

    http://personality-project.org/r/r.anova.html

    On Tue, Feb 5, 2013 at 5:53 PM, Denis-Alexander Engemann <
    [email protected]> wrote:

    that's one of my favorites, I would often give to students as example how
    to do it in R.

    On Tue, Feb 5, 2013 at 5:50 PM, Josef Perktold [email protected]:

    just remembered: UCLA and some other university websites often have good
    example
    I quick search found this
    http://www.ats.ucla.edu/stat/r/seminars/Repeated_Measures/repeated_measures.htm
    Is this along the lines of your use case?

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/646#issuecomment-13139092.

  11. josef-pkt commented on Feb 5, 2013

    @josef-pkt
    Member

    examples or descriptions like these were also some of the references that I used for short panel, for example I got a start on correlation structures to use with short panels here https://github.com/statsmodels/statsmodels/blob/master/statsmodels/sandbox/panel/correlation_structures.py

  12. dengemann commented on Feb 5, 2013

    @dengemann
    ContributorAuthor

    I think I found one.
    lme4::sleepstudy should be a feasible example.

    On Tue, Feb 5, 2013 at 5:59 PM, Josef Perktold [email protected]:

    examples or descriptions like these were also some of the references that
    I used for short panel, for example I got a start on correlation structures
    to use with short panels here
    https://github.com/statsmodels/statsmodels/blob/master/statsmodels/sandbox/panel/correlation_structures.py

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/646#issuecomment-13139559.

  13. dengemann commented on Feb 5, 2013

    @dengemann
    ContributorAuthor

    unfortunately it misses real experimental conditions, let's see what else
    can be found.

    On Tue, Feb 5, 2013 at 6:03 PM, Denis-Alexander Engemann <
    [email protected]> wrote:

    I think I found one.
    lme4::sleepstudy should be a feasible example.

    On Tue, Feb 5, 2013 at 5:59 PM, Josef Perktold [email protected]:

    examples or descriptions like these were also some of the references that
    I used for short panel, for example I got a start on correlation structures
    to use with short panels here
    https://github.com/statsmodels/statsmodels/blob/master/statsmodels/sandbox/panel/correlation_structures.py

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/646#issuecomment-13139559.

  14. josef-pkt commented on Feb 5, 2013

    @josef-pkt
    Member

    a partial map to the Jungle:

    The two main current classes for this are OneWayMixed and ShortPanelGLS

    some example scripts are there
    "statsmodels\sandbox\examples\ex_random_panel.py"
    "statsmodels\sandbox\examples\ex_mixed_lls_0.py"
    "statsmodels\sandbox\examples\ex_mixed_lls_re.py"
    "statsmodels\sandbox\examples\ex_mixed_lls_timecorr.py"

    Both models assume we have a block correlation structure by individual (within) but not across individuals

    OneWayMixed has currently a refactoring bug, uses EM algorithm, and is mainly a cleaned up version of the original model when stats.models was in scipy
    ShortPanelGLS is my version and uses iterative generalized least squares

    In both cases, we need enough individuals to estimate the correlation parameters across individuals, or we need to assume as simple correlation structure so we don't need to estimate many parameters for it.

    OneWayMixed also allows for random effects but I haven't found a direct R analog to write tests for it.

  15. dengemann commented on Feb 5, 2013

    @dengemann
    ContributorAuthor

    Thanks heaps!

    On Tue, Feb 5, 2013 at 6:20 PM, Josef Perktold [email protected]:

    a partial map to the Jungle:

    The two main current classes for this are OneWayMixed and ShortPanelGLS

    some example scripts are there
    "statsmodels\sandbox\examples\ex_random_panel.py"
    "statsmodels\sandbox\examples\ex_mixed_lls_0.py"
    "statsmodels\sandbox\examples\ex_mixed_lls_re.py"
    "statsmodels\sandbox\examples\ex_mixed_lls_timecorr.py"

    Both models assume we have a block correlation structure by individual
    (within) but not across individuals

    which is something one could feed into a summary stats approach...

    OneWayMixed has currently a refactoring bug, uses EM algorithm, and is
    mainly a cleaned up version of the original model when stats.models was in
    scipy
    ShortPanelGLS is my version and uses iterative generalized least squares

    In both cases, we need enough individuals to estimate the correlation
    parameters across individuals,

    what does enough mean here? n >= 20?

    or we need to assume as simple correlation structure so we don't need to
    estimate many parameters for it.

    which is not a big deal for simple cases with 2 factor levels...

    OneWayMixed also allows for random effects but I haven't found a direct R
    analog to write tests for it.

    there must be one ...

    So what's missing here basically is general is supoprt for k-way
    interaction terms?

    —

    Reply to this email directly or view it on GitHubhttps://github.com//issues/646#issuecomment-13140570.

  16. 41 remaining items

  17. dengemann commented on Feb 11, 2013

    @dengemann
    ContributorAuthor

    it's BSD but has GPL ingredients. A weak dependency could work. What about scipy.linalg.cholesky?

  18. josef-pkt commented on Feb 11, 2013

    @josef-pkt
    Member

    I think GPL infection is pretty strict, we still need to have an alternative implementation. It's not LGPL where it's allowed to link against.
    IIRC, scikits-sparse is not supported on Windows.

    scipy.linalg is all (or almost all) dense, sparse support has improved in scipy, especially since scikit-learn, and I haven't kept up with it, but I there was nothing in scipy.sparse that could be used directly.

  19. dengemann commented on Feb 11, 2013

    @dengemann
    ContributorAuthor

    I think in scikit-learn there a Cholesky factorization is implemented in
    several places.

    !git grep Cholesky

    On Mon, Feb 11, 2013 at 7:31 PM, Josef Perktold [email protected]:

    I think GPL infection is pretty strict, we still need to have an
    alternative implementation. It's not LGPL where it's allowed to link
    against.
    IIRC, scikits-sparse is not supported on Windows.

    scipy.linalg is all (or almost all) dense, sparse support has improved in
    scipy, especially since scikit-learn, and I haven't kept up with it, but I
    there was nothing in scipy.sparse that could be used directly.

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/646#issuecomment-13394773.

  20. josef-pkt commented on Feb 11, 2013

    @josef-pkt
    Member

    It might not be so difficult to implement it directly since it's for a specific use case. But I never looked very closely at it.

    Once, I tried for a different kind of mixed model, with several random factors and not the repeated measures block structure. In that case the inverse covariance matrix was not sparse and I gave up with the generic approach.
    For cluster robust standard errors, I have a version that uses separate sparse covariance matrices for each factor/group variable and it worked pretty well, but that's a much simpler problem.

  21. jseabold commented on Feb 11, 2013

    @jseabold
    Member

    I thought scikit-learn might have sparse cholesky, but I didn't see it.

  22. dengemann commented on Feb 11, 2013

    @dengemann
    ContributorAuthor

    it's more context-related, no API or so. Implemented on demand, if you wish.

    On 11.02.2013, at 20:13, Skipper Seabold [email protected] wrote:

    I thought scikit-learn might have sparse cholesky, but I didn't see it.

    —
    Reply to this email directly or view it on GitHub.

  23. josef-pkt commented on Feb 11, 2013

    @josef-pkt
    Member

    before you go hunting for everything Cholesky ;)
    I think what we need is cholsigmainv, the cholesky decomposition of the inverse covariance matrix, or of a dot operator defined with it.

  24. dengemann commented on Feb 11, 2013

    @dengemann
    ContributorAuthor

    Short update on lmr sources studies:

    • after some initial struggle this seems more straight forward than initially assumed. You have to buy some R peculiarities like mc <- match.call() which is just a way of packaging and dispatching arguments from the called function to another e.g. private function, roughly similar to kwargs.
    • The interesting functions in lmer.R are then lmerFrame, lmerFactorList and lmer_finalize that takes the results from the two former to construct a 'mer' object calling new. This is also where interesting interfacing with the C code takes place.
    • To interactively explore the matrices, etc. of this 'mer' instance returned from lmer (via lmer_finalize) use @ as membership operator, did not know that...
  25. dengemann commented on Feb 11, 2013

    @dengemann
    ContributorAuthor

    before you go hunting for everything Cholesky ;)

    and

    just one more (while shutting down open windows and tabs)

    This seems to be a nice blog:
    http://quantstart.com/articles/Cholesky-Decomposition-in-Python-and-NumPy

  26. josef-pkt commented on Feb 14, 2013

    @josef-pkt
    Member

    to add one more tree, since I just saw a reference:

    what I called factor random effects are usually in the literature as variance components mixed or random effects models.
    The solution for known variances can be written so it has normal equations in the same way (mostly) as stochastic restrictions in a linear model. That was my "classical" way to impose prior assumptions/restrictions similar to hierarchical models for Bayesian analysis. (I haven't worked yet on estimating the (co)variances for this, as two step or iterative estimator.)

  27. dengemann commented on Feb 14, 2013

    @dengemann
    ContributorAuthor

    On 14.02.2013, at 19:24, Josef Perktold [email protected] wrote:

    to add one more tree, since I just saw a reference:

    what I called factor random effects are usually in the literature as variance components mixed or random effects models.

    ok, good to have that link, this binds a few trees ;-)

    The solution for known variances can be written so it has normal equations in the same way (mostly) as stochastic restrictions in a linear model. That was my "classical" way to impose prior assumptions/restrictions similar to hierarchical models for Bayesian analysis. (I haven't worked yet on estimating the (co)variances for this, as two step or iterative estimator.)

    —
    Reply to this email directly or view it on GitHub.

  28. yl565 commented on Sep 26, 2016

    @yl565
    Contributor

    Is there anyone working on this currently? I happens to work a lot with mixed model with correlated errors and would like to give it a shot.

  29. kshedden commented on Sep 26, 2016

    @kshedden
    Contributor

    Yes, this was merged a few years ago and is available as the MixedLM class. Please try it out and open an issue if needed or ask a question at https://groups.google.com/forum/#!forum/pystatsmodels.

  30. yl565 commented on Sep 26, 2016

    @yl565
    Contributor

    Hmm...Maybe I missed something but I thought the current MixedLM supports random effect but do not allow specifying error covariance structure, like the correlation in R lme

    lme(Y ~ X, random=~1 | id, correlation=corCompSymm(form=~1|id), method="ML", data=data)
  31. kshedden commented on Sep 26, 2016

    @kshedden
    Contributor

    I don't know the lme syntax very well, but I believe the model you have specified using corCompSymm should be equivalent to a random intercept model, which we can certainly handle. But we cannot handle time-series like dependence structures such as AR. I would like to do that at some point but no time now (it is not in lme4 either AFAIK).

    Our implementation is more similar to lme4 than to lme/nlme. Again, AFAIK we can handle the full range of model specifications supported by lme4 (and even a few that I do not know how to specify in lme4). Our vc syntax is quite general albeit somewhat harder to use. Also, our code will run moderately/dramatically slower depending on the specifics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions