Sam
Sam

Reputation: 4497

issues with groupedData()

I'm following a tutorial on mixed effects models. The tutorial uses egsingle dataset from mlmRev package. As part of the tutorial, the author uses groupedData() as:

egsingle <- groupedData(math ~ year | schoolid/childid, data = egsingle)

Could someone help me understand what "schoolid/childid" refers to?

Please note that schoolid and childid are both factors!

Also, later in the tutorial, the author takes a sample of size 50 and uses lmList() to fit OLS regression for each subject by using:

egsingle <- groupedData(math ~ year | schoolid/childid, data = egsingle)
samp <- sample(levels(egsingle$childid), 50)
level2.subgroup <- subset(egsingle, childid %in% samp)

# fitting a separate OLS regression line to each student
level2 <- lmList(math ~ year | childid, data = level2.subgroup)
plot(augPred(level2))

when I run lmList command above, I get these errors:

Error in eval(expr, envir, enclos) : object 'childid' not found
In addition: Warning messages:
1: In lmList(math ~ year | childid, data = level2.subgroup) :
lmList does not (yet) work correctly on groupedData objects
2: In Ops.factor(schoolid, childid) : ‘/’ not meaningful for factors

Could anyone help me figure out why I get these errors?

Upvotes: 1

Views: 633

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226087

  1. As Roman Luštrik comments, schoolid/chilidid means "school ID" and "child ID nested within school ID" are both grouping variables. The nesting format formally constructs an interaction between the higher and lower levels; heuristically, it lets the computer know that "child 1 in school 1" and "child 1 in school 2" are different individuals.

  2. You're having a problem with conflicts between the versions of lmList in the nlme and lme4 packages. If you run exactly these lines from a clean R session:

## load data without loading package & dependencies
data(egsingle, package="mlmRev")
library("nlme")

egsingle <- groupedData(math ~ year | schoolid/childid, data = egsingle)
samp <- sample(levels(egsingle$childid), 50)
level2.subgroup <- subset(egsingle, childid %in% samp)

# fitting a separate OLS regression line to each student
level2 <- lmList(math ~ year | childid, data = level2.subgroup)
plot(augPred(level2))

it should work fine. It should also work if you library("mlmRev") before you load nlme (so nlme is before lme4 in the search path), or if you explicitly specify nlme::lmList so you don't accidentally pick up lme4::lmList.

Upvotes: 2

Related Questions