Learn R Programming

ciTools (version 0.1.0)

add_ci.lmerMod: Confidence Intervals for Linear Mixed Model Predictions

Description

This function is one of the methods for add_ci, and is called automatically when add_ci is used on a fit of class lmerMod. It is recommended that one use parametric confidence intervals when modeling with a random intercept linear mixed model (i.e. a fit with a formula such as lmer(y ~ x + (1|group))). Otherwise, confidence intervals may be simulated (type = "sim") via merTools::predictInterval or bootstrapped via lme4::bootMer.

Usage

# S3 method for lmerMod
add_ci(tb, fit, alpha = 0.05, names = NULL,
  yhatName = "pred", type = "boot", includeRanef = TRUE, nSims = 200,
  ...)

Arguments

tb

A tibble or data frame of new data.

fit

An object of class lmerMod.

alpha

A real number between 0 and 1. Controls the confidence level of the interval estimates.

names

NULL or character vector of length two. If NULL, confidence bounds automatically will be named by add_ci, otherwise, the lower confidence bound will be named names[1] and the upper confidence bound will be named names[2].

yhatName

A string. Name of the predictions vector.

type

A string. Must be "parametric", "boot", or "sim". If type = "sim", then add_ci calls merTools::predictInterval. If type = "boot", then add_ci calls lme4::bootMer.

includeRanef

A logical. Default is TRUE. Set whether the predictions and intervals should be made conditional on the random effects. If FALSE, random effects will not be included.

nSims

A positive integer. Controls the number of bootstrap replicates if type = "boot", or the number of simulated draws if type = "sim".

...

Additional arguments.

Value

A tibble, tb, with predicted values, upper and lower confidence bounds attached.

Details

Bootstrapped intervals are the slowest to compute, but they are the recommended method when working with any linear mixed models more complicated than the random intercept model.

See Also

add_pi.lmerMod for prediction intervals of lmerMod objects, add_probs.lmerMod for conditional probabilities of lmerMod objects, and add_quantile.lmerMod for response quantiles of lmerMod objects.

Examples

Run this code
# NOT RUN {
# Define the data set
dat <- lme4::sleepstudy
# Fit a linear mixed model (random intercept model)
fit <- lme4::lmer(Reaction ~ Days + (1|Subject), data = lme4::sleepstudy)
# Get the fitted values for each observation in dat, and
# append CIs for those fitted values to dat
add_ci(dat, fit, alpha = 0.5, nSims = 100)
# Try another method, and make prediction at the population level
add_ci(dat, fit, alpha = 0.5, type = "parametric", includeRanef = FALSE, nSims = 100)
# Try another method, and add custom names to the confidence bounds
add_ci(dat, fit, alpha = 0.5, type = "sim", names = c("lwr", "upr"), nSims = 1000)

# }

Run the code above in your browser using DataLab