# Getting fixed-effect only predictions from mixed model on new data in R

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.

Sample code:

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.

• very crude but simple approach: if computing time is not a major factor (i.e. you don't have to do this a ton of times) you could fit the new data in lm() and extract the model matrix from the fit, and apply it to the model coefficients from the previous fit. – Macro Jun 2 '12 at 15:16

Maybe this is cheating, but

fixedformula <- as.formula(lme4.0:::nobars(formula(m))[-2])
model.matrix(fixedformula,newdata=cake2)


note:

• I am using lme4.0 here, which is the r-forge version of "old" (CRAN) lme4: you can substitute lme4 for lme4.0 in the code above
• the new (r-forge/development) version of lme4 has a predict method: in that case

predict(m,re.form=NA,newdata=cake2)


works fine (re.form=NA sets all random effects to zero, equivalent to level=0 in the old predict.lme)

• Thank you Ben. I had trouble getting the first solution to work properly. But switching to the dev version of lme4 and using the predict/REform=NA function does the trick without having to mess with the details of the model matrix. There are a couple of features that don't appear to be implemented yet in the dev version that I was using in my code (in particular, I can't seem to extract the random effect postVars as easily), but I'd rather be able to use the predict function and wait until the new lme4 is complete for the other pieces. – colonel.triq Jun 6 '12 at 13:55
• It may be obvious, but if you could write up a (very) small, self-contained example of what you want to do that doesn't work in the dev version and send it to me, that would be useful – Ben Bolker Jun 6 '12 at 13:58