[R-sig-ME] MCMCglmm predict failing with more than one fixed effect

Tom Houslay hou@|@y @end|ng |rom gm@||@com
Wed Sep 4 13:02:27 CEST 2019

Hi all,

Hoping someone can help me with this issue, even (especially?) if I'm
just being an idiot. I had been using predict.MCMCglmm with new data
successfully until recently (I think coinciding with updating R and
various packages), and now if I have more than one fixed effect in a
model then it fails when supplying any newdata. The error message is:

Error in predict.MCMCglmm(mcmc_ss_2, newdata = as.data.frame(df_ss_pred_2)) :
  model for newdata has fixed effects absent from the original model

I've tried a load of different ways of building the data to predict
from, but nothing seems to work.

Code below for a reproducible example (requires MCMCglmm and the
'sleepstudy' data from lme4). I'm running R 3.6.1 and MCMCglmm 2.29.



# Basic model of sleepstudy with 1 fixed effect and simple random intercept
mcmc_ss_1 <- MCMCglmm(Reaction ~ Days,
                      random =~ Subject,
                      data = sleepstudy)


# Predicts fine on original data

# Create data on which to predict
df_ss_pred <- data.frame(Days = 0:9,
                         Subject = 350,
                         Reaction = 0)

# This prediction works
        newdata = df_ss_pred)

# Create new data frame with fake additional column
sleepstudy_2 <- cbind(sleepstudy,
                      extra = rnorm(nrow(sleepstudy)))

# Run model with additional fixed effect
mcmc_ss_2 <- MCMCglmm(Reaction ~ Days + extra,
                      random =~ Subject,
                      data = sleepstudy_2)


# Predict works fine from original data

# Create data frame on which to predict,
#  as before but now with new column added
df_ss_pred_2 <- as.data.frame(expand.grid(Reaction = 0,
                                          Days = 0:9,
                                          Subject = 350,
                                          extra = mean(sleepstudy_2$extra)))

# Prediction fails
        newdata = df_ss_pred_2)

# Check model formula

# Check variable names in data frame

# Check variable names in new data frame

More information about the R-sig-mixed-models mailing list