I am trying to fit a mixed effects model, and then use this model to generate estimates of a new dataset, which can have different levels. I expected that the estimates of the new dataset will use the average value of the estimated parameters, but this does not seem to be the case. Here's a minimal working example:
library(lme4) d = data.frame(x = rep(1:10, times = 3), y = NA, grp = rep(1:3, each = 10)) d$y[d$grp == 1] = 1:10 + rnorm(10) d$y[d$grp == 2] = 1:10 * 1.5 + rnorm(10) d$y[d$grp == 3] = 1:10 * 0.5 + rnorm(10) fit = lmer(y ~ (1+x)|grp, data = d) newdata = data.frame(x = 1:10, grp = 4) predict(fit, newdata = newdata, allow.new.levels = TRUE)
In this example, I essentially define three groups with different regression equations (slopes 1, 1.5, and 0.5). However, when I try to predict a new dataset with an invisible level, I get a constant estimate. I expected the expected slope and intercept value to be used to generate forecasts for this new data. I expect something wrong? Or what am I doing wrong with my code?
source share