lmer_predict: This function predicts outcome variable from a (g)lmer object...

View source: R/lmer_predict.R

lmer_predictR Documentation

This function predicts outcome variable from a (g)lmer object across values of the predictors It plays well with ggplot2 and is intended to give a visual sense of the model-predicted estimates.

Description

By default, lmer_predict will compute estimates of the outcome variable for every combination of predictors that are in the model. For continuous predictors, it finds the range of observed values and computes at 20 equally spaced points along the range. This number is controlled by n.cont. For categorical predictors, it computes estimates at each level.

Usage

lmer_predict(
  lmerObj,
  divide = NULL,
  n.divide = 3,
  divide.prefix = TRUE,
  n.cont = 20,
  cont.pts = NULL,
  fixat0 = NULL,
  fixatmean = NULL,
  yoked = NULL
)

Arguments

lmerObj

the fitted (g)lmer object used to make predictions

divide

a character vector specifying continuous predictors that should be cut into categorical levels

n.divide

how many categories to use when cutting variables in divide. For 3, you get -1 SD, Mean, and +1SD. For 5, you get -2 SD, -1 SD, Mean, +1 SD, +2 SD. Useful for visualizing interactions.

divide.prefix

Whether to include the variable name in naming the cut points

n.cont

The number of points along the continuous predictor range at which to compute an estimate

cont.pts

A list named by predictor where each element is a vector of values at which to compute an estimate

fixat0

Predictors that should be fixed at 0 in the estimates

fixatmean

Predictors that should be fixed at sample mean in the estimates

yoked

Useful when predictors are transformations of each other (need to document this better).

Details

This function was adapted from code initially developed by Doug Bates. This is currently part of the predict method of lmer: https://github.com/bbolker/mixedmodels-misc/blob/master/glmmFAQ.rmd

Value

A data.frame containing model-predicted values for each predictor, along with standard errors (se), 95% CIs (ci_lo and ci_hi), and 95% prediction intervals (pred_lo and pred_hi).

Author(s)

Michael Hallquist

Examples

  data(rat.brain)
  library(lme4); library(ggplot2)
  m <- lmer(activate ~ region * treatment + (1 | animal), data=rat.brain)
  pred_vals <- lmer_predict(m)
  ggplot(pred_vals, aes(x=region, y=activate,
      ymin=activate-se, ymax=activate+se, color=treatment)) +
    geom_pointrange()


PennStateDEPENdLab/dependlab documentation built on May 21, 2024, 6:31 a.m.