Lmer: population projections cause error

I want to use a linear mixed model and make predictions at the population level (i.e. use only fixed effects and use 0 instead of random effects).

Model Example:

require(lme4) fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy) summary(fm1) # values for prediction: newx <- seq(min(sleepstudy$Days), max(sleepstudy$Days)) 

I tried several methods of predicting by population level, but all of them failed:

 pred <- predict(fm1, newdata = data.frame(Days = newx), allow.new.levels = TRUE) # Error: couldn't evaluate grouping factor Subject within model frame: try adding grouping factor to data frame explicitly if possible pred <- predict(fm1, newdata = data.frame(Days = newx, Subject = NA), allow.new.levels = TRUE) # Error: Invalid grouping factor specification, Subject pred <- predict(fm1, newdata = data.frame(Days = newx, Subject = as.factor(NA)), allow.new.levels = TRUE) # Error: Invalid grouping factor specification, Subject 

I tried to find a guide for the correct prediction method, but I do not know how to do this? I tried looking at help(package = "lme4") , and the closest function I found was predict.merMod (although the class of the fm1 model is lmerMod not merMod ). ?predict.merMod reads:

allow.new.levels (boolean), if FALSE (default), then any new levels (or NA values) found in newdata will cause an error; if TRUE, then the forecast will use the unconditional (population level) for data with previously unobserved levels (or NA)

It specifically says "or NA", but it doesn't seem to work!

  • Am I looking at the proper method help page? If not, what is the correct method?
  • How to make a population forecast?
+6
source share
1 answer

You are looking for re.form :

re.form: formula for random effects for the on condition. If "NULL, include all random effects; if" NA "or" 0 ", specify random effects

 require(lme4) fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy) newx <- seq(min(sleepstudy$Days), max(sleepstudy$Days)) predict(fm1, newdata=data.frame(Days=newx), re.form=NA) ## 1 2 3 4 5 6 7 8 ## 251.4051 261.8724 272.3397 282.8070 293.2742 303.7415 314.2088 324.6761 ## 9 10 ## 335.1434 345.6107 

Regarding other issues:

  • merMod is a superclass that includes both linear ( lmerMod ) and generalized linear ( glmerMod ) models: see ?"merMod-class"
  • Your two attempts should probably have worked; however allow.new.levels was intended for cases with random NA values, not all NA values ​​... predict(fm1, newdata = data.frame(Days = newx, Subject = "a"), allow.new.levels = TRUE) work. It seems that the code detects the all- NA column and interprets it as something wrong at the top - this can be fixed in the code, but does not seem to be very high priority, since re.form exists.
+8
source

All Articles