[R-sig-ME] MCMCglmm predict failing with more than one fixed effect
Tom Houslay
hou@|@y @end|ng |rom gm@||@com
Fri Sep 27 17:11:25 CEST 2019
Hi all,
Just closing the open question I had in case anyone else has had the same
issue using predict.MCMCglmm. Turns out that it didn't seem to like
predicting with a single value in any of the fixed effect terms - so even
if I just want to predict for a single value of something, I just have to
add in another value and then ignore those rows afterwards. I've appended a
little bit to the reproducible code from my last email to show how that's
working (see below).
Cheers
Tom
---
sleepstudy <- lme4::sleepstudy
# Basic model of sleepstudy with 1 fixed effect and simple random intercept
mcmc_ss_1 <- MCMCglmm(Reaction ~ Days,
random =~ Subject,
data = sleepstudy)
summary(mcmc_ss_1)
# Predicts fine on original data
predict(mcmc_ss_1)
# Create data on which to predict
df_ss_pred <- data.frame(Days = 0:9,
Subject = 350,
Reaction = 0)
# This prediction works
predict(mcmc_ss_1,
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)
summary(mcmc_ss_2)
# Predict works fine from original data
predict(mcmc_ss_2)
# 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
predict(mcmc_ss_2,
newdata = df_ss_pred_2)
# Check model formula
mcmc_ss_2$Fixed$formula
# Check variable names in data frame
str(sleepstudy_2)
# Check variable names in new data frame
str(df_ss_pred_2)
# Try adding another value for 'extra'
df_ss_pred_3 <- as.data.frame(expand.grid(Reaction = 0,
Days = 0:9,
Subject = 350,
extra =
c(mean(sleepstudy_2$extra),
mean(sleepstudy_2$extra) + sd(sleepstudy_2$extra))))
# Prediction now works!
predict(mcmc_ss_2,
newdata = df_ss_pred_3)
[[alternative HTML version deleted]]
More information about the R-sig-mixed-models
mailing list