predict.merMod: A predict method for merMod models with the bootstrap

Description Usage Arguments Examples

Description

When this package is loaded after loading lme4, it replaces the predict method for linear mixed-effects models (merMod objects) with this function. The method provided here adds the argument se.fit (just like in predict.lm). It uses the bootstrap, as implemented in bootMer to estimate standard errors and confidence intervals for the predictions. Also useful in conjunction with the visreg package, which by default does not plot confidence intervals for merMod models.

Usage

1
2
3
## S3 method for class 'merMod'
predict(object, nsim = getOption("bootnsim"),
  se.fit = FALSE, alpha = 0.05, ...)

Arguments

object

An object as returned by lmer

nsim

The number of bootstrap replicates. The default is a small number to allow quick testing. A warning is printed when nsim < 100, and it can be set via options (see Examples)

se.fit

If TRUE, returns standard error (se.fit) and confidence interval (ci.fit) for the predictions, as components of the returned list.

alpha

Controls the coverage of the confidence interval.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# Fit a linear mixed-effects model 
fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)

# Predictions without standard error (fixed effects only)
predict(fm1, newdata=data.frame(Days=5), re.form=NA)

# Predictions with standard error and confidence interval
# Set number of bootstrap replicates first (you should use a larger number)
options(nbootsim=20)
predict(fm1, newdata=data.frame(Days=5), re.form=NA, se.fit=TRUE)

# Add confidence intervals to visreg
library(visreg)
visreg(fm1, "Days")

# Also works with an overlay plot, using visreg
# First add an artificial group to the sleepstudy data
x <- sort(with(sleepstudy, tapply(Reaction, Subject, mean)))
sleepstudy$Group <- as.factor(ifelse(sleepstudy$Subject %in% names(x[1:9]), "A", "B"))
fm2 <- lmer(Reaction ~ Days*Group + (Days | Subject), sleepstudy)
visreg(fm2, "Days", by="Group", overlay=TRUE)

RemkoDuursma/bootpredictlme4 documentation built on May 9, 2019, 9:39 a.m.