mlm_variance_decomp: Decompose uncertainty in the simple slope of a multilevel...

View source: R/mlm_variance_decomp.R

mlm_variance_decompR Documentation

Decompose uncertainty in the simple slope of a multilevel interaction

Description

In a random-slope model, the uncertainty around a simple slope has two distinct sources that standard mlm_probe() collapses into one SE:

Usage

mlm_variance_decomp(
  model,
  pred,
  modx,
  modx.values = c("mean-sd", "quartiles", "tertiles", "custom"),
  at = NULL,
  conf.level = 0.95
)

Arguments

model

An lmerMod object with a random slope for pred and a two-way interaction between pred and modx.

pred

Character scalar. Focal predictor name.

modx

Character scalar. Moderator name.

modx.values

Strategy for moderator values. See mlm_probe().

at

Optional numeric vector of custom moderator values.

conf.level

Confidence level for fixed-effect CIs. Default 0.95.

Details

  1. Fixed-effect uncertainty – imprecision in the estimated average slope (\beta_1 + \beta_3 \cdot w), captured by the fixed-effect variance-covariance matrix.

  2. Random-slope variance – genuine between-cluster heterogeneity in the slope of pred (\tau_{11}), which is not estimation error but real variation in effects across clusters.

These answer different questions:

  • Fixed-effect uncertainty: "How precisely do we know the average slope at this moderator value?"

  • Random-slope variance: "How much does the slope actually vary across clusters, regardless of what the moderator does?"

The function also reports a prediction interval for the slope in a new (unobserved) cluster, which combines both sources and is the appropriate uncertainty interval for making cluster-level predictions.

Random-slope variance (\tau_{11}) interpretation: If tau11 is large relative to the fixed slope, the effect of pred varies substantially across clusters even after accounting for the moderator. This is important: a significant interaction does not mean the moderator fully explains between-cluster slope heterogeneity.

Prediction interval interpretation: The prediction interval answers: "For a randomly sampled new cluster at this moderator value, what range of slopes should we expect?" It will always be wider than the confidence interval because it incorporates \tau_{11}.

Percentage of variance from random effects: \% \text{random} = \frac{\tau_{11}}{\tau_{11} + \text{Var}(\hat{\beta}_{\text{simple slope}})} \times 100

Value

An object of class mlm_variance_decomp (a list) with:

  • decomp: data frame with columns modx_value, slope, se_fixed (SE from fixed-effect vcov only), tau11 (random-slope SD, if estimable), se_total (combined SE for prediction in new cluster), ci_lower, ci_upper (fixed-effect CI), pi_lower, pi_upper (prediction interval for new cluster), pct_random (% of total slope variance from random effects).

  • tau11: the random-slope variance (\tau_{11}).

  • has_random_slope: logical – does the model include a random slope for pred?

  • Metadata: pred, modx, conf.level.

Examples

set.seed(1)
dat <- data.frame(
  y   = rnorm(200), x = rnorm(200),
  m   = rep(rnorm(20), each = 10),
  grp = factor(rep(1:20, each = 10))
)
dat$y <- dat$y + dat$x * dat$m
mod <- lme4::lmer(y ~ x * m + (1 + x | grp), data = dat,
                  control = lme4::lmerControl(optimizer = "bobyqa"))
vd <- mlm_variance_decomp(mod, pred = "x", modx = "m")
print(vd)


mlmoderator documentation built on April 4, 2026, 1:07 a.m.