Created
May 7, 2020 17:38
-
-
Save mbjoseph/fe8661282bc47b2c8fd212a7b3b16af0 to your computer and use it in GitHub Desktop.
Predicting for new factor levels in a mixed effects model - lme4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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