[R-sig-ME] lmList from lme4 on grouped data objects

Ben Bolker bbolker at gmail.com
Wed Nov 13 16:19:03 CET 2013


Viechtbauer Wolfgang (STAT <wolfgang.viechtbauer at ...> writes:

> 
> Dear lme4-Maintainers,
> 
> As far as I can tell, lmList() from lme4 does not 
> play nicely with grouped data objects from the nlme package. Example:
> 
> library(lme4)
> data(Orthodont, package="nlme")
> 
> class(Orthodont)
> 
> ### [1] "nfnGroupedData" "nfGroupedData"  "groupedData"    "data.frame"
> 
> res <- lmList(distance ~ age | Subject, data=Orthodont)
> 
> ### Error in eval(expr, envir, enclos) : object 'Subject' not found
> ### In addition: Warning message:
> ### In Ops.ordered(age, Subject) : '|' is not 
> meaningful for ordered factors
> 
> Orthodont <- as.data.frame(Orthodont)
> 
> res <- lmList(distance ~ age | Subject, data=Orthodont)
> 
> ### works
> 
> This had me stumped for a while, so I figured I would note this here (in
case anybody else runs into this
> issue). After googling a bit, I found:
> 
> https://github.com/lme4/lme4/blob/master/tests/lmList.R
> 
> so nothing really new here. But maybe add a check
>  to lmList() whether somebody specifies a grouped data
> object and turn it into a regular data frame?


  This is a good point. The code of lmList actually has a
line converting the 'data' argument via 'as.data.frame', but
it doesn't work! The reason is a bit subtle -- lmList constructs
its model frame by modifying the call and evaluating it in the
parent frame, so that modifications to 'data' within the function
itself get lost ...  I'm not currently sure of the best way to
fix this, so in the meantime I'm adding a comment to the documentation.

There are still some open issues with lmList: in particular, it
doesn't play nicely with the nlme accessor methods:

https://github.com/lme4/lme4/issues/26



More information about the R-sig-mixed-models mailing list