[R-sig-ME] How to calculate predictions

Stuart Luppescu slu at ccsr.uchicago.edu
Mon Feb 27 22:02:31 CET 2012


Hello, I have modeled student math growth curves with lmer with a model
like this. It predicts math achievement by age (centered at age 11) with
a linear and quadratic term, with both these and the intercept varying
randomly across students:

math.lme3 <- lmer(data=allmathgains, math ~ I(age-11) + I((age-11)^2) +
old4gr + (I(age-11) + I((age-11)^2) | sid))

where sid is the student ID and old4gr take a value of 1 if the student
is old for grade, 0 otherwise. I want to get a prediction of each
student's achievement at age 15. I have done this kind of thing:

random.effects3 <- ranef(math.lme3)
fixed.effects3 <- fixef(math.lme3)

## Just try it for the first 100 students for now
test <- random.effects3$sid[1:100,]
test2 <- cbind(as.numeric(rownames(test)),
               test[,1]+fixed.effects3[1],
               test[,2]+fixed.effects3[2],
               test[,3]+fixed.effects3[3])
 
test3 <- cbind(test2, allmathgains[1:100, "old4gr"])
 
to.predict <- as.data.frame(cbind(rep(1, 100), rep(4, 100), rep(16,
100)))
to.predict2 <- cbind(to.predict, allmathgains[1:100, "old4gr"])

my.predictions <- numeric(100)
for (i in 1:100) {
  my.predictions[i] <- test3[i, 2:5] %*% t(as.matrix(to.predict2[i,]))
}

My questions: 
1) Is my idea to add each random effect to the fixed effects (to make
data.frame test2) correct? 
2) Is there a more efficient way of doing this? This is a big issue
because I am working with a data set of about 240,000 students. 

Thanks in advance.
-- 
Stuart Luppescu -=- slu .at. ccsr.uchicago.edu        
University of Chicago -=- CCSR 
才文と智奈美の父 -=-    Kernel 3.2.1-gentoo-r2                
For the S system, which has forever altered the way
 people analyze, visualize, and manipulate data
 .... S is an elegant, widely accepted, and
 enduring software system, with conceptual
 integrity, thanks to the insight, taste, and
 effort of John Chambers.    -- Association for
 Computing Machinery       ACM/Software System
 Award citation (1998)




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