Skip to content

Instantly share code, notes, and snippets.

@mbjoseph
Created May 7, 2020 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbjoseph/fe8661282bc47b2c8fd212a7b3b16af0 to your computer and use it in GitHub Desktop.
Save mbjoseph/fe8661282bc47b2c8fd212a7b3b16af0 to your computer and use it in GitHub Desktop.
Predicting for new factor levels in a mixed effects model - lme4
library(lme4)
# adapted from ?predict.merMod
gm1 <- glmer(cbind(incidence, size - incidence) ~ period + (1 |herd),
cbpp,
binomial)
# add some new levels to herd, which is a random effect
newdata <- with(cbpp,
expand.grid(period=unique(period),
herd=c(unique(herd), 16:20)))
# this raises an error
predict(gm1,newdata)
# this allows you to make predictions for new ranef levels
predict(gm1, newdata, allow.new.levels = TRUE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment