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)
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?
source share