Dear all,



I try to use the code provided here http://glmm.wikidot.com/faq in section
“Predictions and/or confidence (or prediction) intervals on predictions”,
to achieve predictions and confidence intervals form my lmer model.



I have the following model:

lmerfit1<-(Speed~BodyMass+(1|ID)+(1|Species))



In the code below provided by http://glmm.wikidot.com/faq it is stated that
complex models must be adapted (“tvar1 <- pvar1+VarCorr(fm1)$ID[1]## must
be adapted for more complex models”). I have two random effects, and I
wonder if I have modified the code correctly? See below, in orange text.







library(lme4)

library(ggplot2) # Plotting

fm1 <- lmer(

    formula = Speed ~ BodyMass + (1|ID)

+(1|Species), data = Flight)

newdat <- expand.grid(

BodyMass = seq(from = 0, to = 20, by = 1), distance = 0

)

mm <- model.matrix(terms(fm1),newdat)

newdat$Speed <- mm %*% fixef(fm1)

pvar1 <- diag(mm %*% tcrossprod(vcov(fm1),mm))

tvar1 <- pvar1+VarCorr(fm1)$ID[1] +  VarCorr(fm1)$Species[1]  ## must be
adapted for more complex models

newdat <- data.frame(

    newdat

    , plo = newdat$Speed-2*sqrt(pvar1)

    , phi = newdat$dSpeed+2*sqrt(pvar1)

    , tlo = newdat$Speed-2*sqrt(tvar1)

    , thi = newdat$Speed+2*sqrt(tvar1)

)

#plot confidence

g0 <- ggplot(newdat, aes(x=aBodyMass, y=Speed))+geom_point()

g0 + geom_errorbar(aes(ymin = plo, ymax = phi))+

    opts(title="CI based on fixed-effects uncertainty ONLY")

#plot prediction

g0 + geom_errorbar(aes(ymin = tlo, ymax = thi))+

    opts(title="CI based on FE uncertainty + RE variance")

	[[alternative HTML version deleted]]

