I would like to construct predictions for a mixed model (logistic via glmer) on a new data set using only the fixed effects, holding the random effects to 0. But I am having trouble setting up the model matrix to be able to calculate them.
Since the mer class doesn't have a predict method, and since I want to omit the random effects for predictions on the new data set, I think I need to construct a model matrix for the fixed effects of the same structure used in the original model, but using the new data. Then multiply by the fixed effect coefficients in the model.
The fixed effect portion of my model formula contains factors and interaction terms between numeric fixed effects, so it's a little more complicated than just extracting the fixed variables from the matrix. e.g. I need to ensure the factor contrast expansion is the same as the original, interaction terms are properly listed, etc.
So my question is: what is the more straightforward general approach for constructing a new model matrix that mimics the structure of the original model matrix used in creating the model?
I've tried model.matrix(my.model, data=newdata) but that seems to return the original model matrix, not one based on newdata.
library(lme4) cake2 <- head(cake) # cake2 is "new" data frame for future predictions # recipe is a fixed effect factor, temp is fixed effect numeric, replicate is random effect m <- lmer(angle ~ temp + recipe + (1 | replicate), data=cake) summary(m) nrow(cake2) # but new data frame has 6 rows nrow(cake) # original data frame has 270 rows # attempt to make new model matrix using different data frame mod.mat.cake2 <- model.matrix(m, data=cake2) nrow(mod.mat.cake2) # 270 rows, same as orig data frame
I tried other methods like extracting the terms from the formula and building a new formula from that, but it seemed overly convoluted, and brittle in handling factors and interaction terms.
How can I get mod.mat.cake2 to be a fixed effect model matrix based on the formula in m, but using values from cake2? Or is there an easier way to go about getting fixed-effect only predictions from an lmer model?
All help is appreciated. Thank you.