Computes conditional (simple) slopes of a focal predictor across values of a moderator from a fitted 'lavaan' model that includes their explicit product term. Plots predicted lines with Wald confidence ribbons, and print an APA-style test of the interaction for easy reporting and interpretation, plus a simple-slopes table.
lav_slopes(
fit,
outcome,
pred,
modx,
interaction,
data = NULL,
modx.values = NULL,
modx.labels = NULL,
pred.range = NULL,
conf.level = 0.95,
x.label = NULL,
y.label = NULL,
legend.title = NULL,
colors = NULL,
line.size = 0.80,
alpha = 0.20,
table = TRUE,
digits = 2,
modx_n_unique_cutoff = 4L,
return_data = FALSE
)# S3 method for lav_slopes
print(x, ...)
# S3 method for lav_slopes
summary(object, ...)
A list with elements:
plotggplot object with lines and confidence ribbons.
slope_tableData frame with moderator levels, simple slopes, SE, z, and CI.
plot_dataOnly when return_data = TRUE: data used to build the plot.
A fitted 'lavaan' object that includes the product term (required).
Character. Name of the dependent variable in fit (required).
Character. Name of the focal predictor whose simple slopes are probed (required).
Character. Name of the moderator (required).
Character. Name of the product term in fit (e.g., "X_Z") (required).
data.frame. Raw data. If NULL, the function tries to pull
data from fit via lavInspect.
Numeric or character vector. Values or levels of the moderator
at which to compute slopes; derived automatically when NULL.
Character vector. Legend/table labels for modx.values
(default: the character form of modx.values).
Numeric length-2. Range c(min, max) for the x-axis;
uses observed range in data when available, else c(-2, 2).
Numeric in (0,1). Confidence level for CIs and ribbons (default: 0.95).
Character. X-axis label (default: pred).
Character. Y-axis label (default: outcome).
Character. Legend title; if NULL, the legend shows only levels (default: NULL).
Character vector. Colors for lines and ribbons; named vector recommended with names matching modx.labels (default: Okabe-Ito palette).
Numeric > 0. Line width (default: 0.80).
Numeric in (0,1). Ribbon opacity (default 0.20).
Logical. Print APA-style interaction test and simple-slopes table (default: TRUE).
Integer >= 0. Decimal digits in printed output (default: 2).
Integer >= 1. Threshold for treating a numeric moderator
as continuous and using mean ± SD (default: 4).
Logical. If TRUE, include the plotting data.frame in the returned list (default: FALSE).
A 'lav_slopes' object.
Additional arguments; unused.
A 'lav_slopes' object.
Estimates are unstandardized; a standardized beta for the interaction is also reported for reference. Wald tests assume large-sample normality of estimates.
The model should include a main effect for the predictor, a main effect for the moderator, and their product term. The simple slope of the predictor at a given moderator value combines the predictor main effect with the interaction term. The moderator can be continuous or categorical. Standard errors use the delta method with the model covariance matrix of the estimates.
set.seed(42)
X <- rnorm(100); Z <- rnorm(100); X_Z <- X*Z
Y <- 0.6*X + 0.6*Z + 0.3*X_Z + rnorm(100, sd = 0.7)
dataset <- data.frame(Y, X, Z, X_Z)
fit <- lavaan::sem("Y ~ X + Z + X_Z", data = dataset)
lav_slopes(
fit = fit,
data = dataset,
outcome = "Y",
pred = "X",
modx = "Z",
interaction = "X_Z")
Run the code above in your browser using DataLab