knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "75%"
)

mlmtools

Multilevel and mixed effects models often require specialized data pre-processing and further post-estimation derivations and graphics to gain insight into model results. mlmtools is a suite of pre- and post-estimation tools for multilevel models in R. The package's post-estimation tools are designed to work with models estimated using lme4's lmer function, which fits linear mixed effects regression models. Although nearly all the functions provided in the mlmtools package exist as singleton functions within other R packages, they are often improved in mlmtools and more accessible by being located within a multilevel modeling specific package.

The package was developed by Laura Jamison, Jessican Mazen, Erik Ruzek, and Gus Sjobek.

Included functions

Installation

To install the latest release version (1.0.0) from GitHub with:

# install.packages("devtools")
devtools::install_github("lj5yn/mlmtools")

Sample workflow

Working with the included data, we briefly show how some of the included functions can be used.

# data
library(mlmtools)
library(lme4)
data("instruction")

# fit variance components model
fit1 <- lmer(mathgain ~ 1 + (1|classid), instruction)

# intraclass correlation coefficient
ICCm(fit1)

# group-mean center mathkind 
center(instruction, x="mathkind", grouping = "classid")

# add group-mean centered and group mean as predictors
fit2 <- lmer(mathgain ~ classid_mathkind.devcmn + classid_mathkind.cmn + (1|classid), instruction)

# variance explained by adding predictor
varCompare(fit2, fit1)

# test model assumptions
mlm_assumptions(fit2)

# variance explained by the model
rsqmlm(fit2)

Visualizations

Rich visualizations of associations can be had along with caterpillar plots, which graph the 95% prediction intervals for the random effects.

# visualize between-group association
betweenPlot(x = "mathkind", y = "mathgain", grouping = "classid", dataset = instruction, xlab = "Kindergarten Math Score", ylab = "Gain in Math Score")

# visualze within-group association
withinPlot(x = "mathkind", y = "mathgain", grouping = "classid", dataset = instruction)

# caterpillar plot
caterpillarPlot(fit2, grouping = "classid")


lj5yn/mlmtools documentation built on March 10, 2023, 8:37 p.m.