Question

I am reading this book on data analysis and graphics in R and I think there are some problems between the current version of lme4 and the one they used (I have the 2007 and 2010 editions). My current problem concerns p340 of the book where the authors show how to extract the slopes per individual using lmList. The code reads as follows:

library(lme4) data(Orthodont, package = "nlme") ab <- coef(lmList(distance ~ age|Subject, data=Orthodont))

This should be pretty straighforward, and it seems like that in the book. However, I get the following:

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

This is confusing at three levels:

  1. Why is Subject not found despite it being in Orthodont ?
  2. How does R know that an unfound variable is an ordered factor ?
  3. Why would this regression not function for ordered factors anyway? What's the difference?

I tried a workaround and checked via str(Orthodont) whether the first command indeed created a new variable sub2 that is of mode and class character:

Orthodont$sub2<-as.character(Orthodont$Subject)
ab <- with(Orthodont,coef(lmList(distance ~ age|sub2, data=Orthodont)))

I got an identical error message: sub2 not found and a warning that sub2 is an ordered factor.

Any ideas?

Was it helpful?

Solution

This problem is documented, admittedly not as clearly as it should be: from ?lmList:

‘data’ should be a data frame (not, e.g. a ‘groupedData’ object from the ‘nlme’ package); use ‘as.data.frame’ first to convert the data.

The reason you're encountering trouble and the authors didn't is that they pulled the Orthodont data from the MEMSS package (where it is stored as a regular data frame, not a groupedData object) rather than from nlme.

For reasons that I don't remember right now, doing the conversion from groupedData to data.frame automatically (which would be the sensible thing to do) is harder than it seems because of the way the code is designed.

This is also discussed at https://stat.ethz.ch/pipermail/r-sig-mixed-models/2013q4/021283.html and https://github.com/lme4/lme4/issues/26

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top