add_ci.lmerMod: Confidence Intervals for Linear Mixed Model Predictions

View source: R/add_ci_lmer.R

add_ci.lmerModR Documentation

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 bootstrapped via lme4::bootMer.

Usage

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

Arguments

df

A 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" or "boot", If type = "boot", then add_ci calls lme4::bootMer to calculate the confidence intervals. This method may be time consuming, but is applicable with random slope and random intercept models. The parametric method is fast, but currently only works well for random intercept models.

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".

...

Additional arguments.

Details

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

Value

A dataframe, df, with predicted values, upper and lower confidence bounds attached.

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

## Not run: 
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)
# Try the parametric bootstrap method, and make prediction at the population level
add_ci(dat, fit, alpha = 0.5, type = "boot", includeRanef = FALSE, nSims = 100)

## End(Not run)

jthaman/ciTools documentation built on Nov. 11, 2023, 2:04 p.m.