| Title: | Interaction Testing and Plotting for lavaan Fitted Objects |
|---|---|
| Description: | Tools to test and visualize moderation in structural equation models fitted with 'lavaan'. Computes conditional (simple) slopes across moderator values and produces predicted lines with Wald confidence ribbons using model estimates and their covariance without refitting. Accepts continuous or categorical moderators. See Rosseel (2012) <doi:10.18637/jss.v048.i02>; Preacher, Curran, and Bauer (2006) <doi:10.1207/s15327906mbr4101_1>. |
| Authors: | Giuseppe Corbelli [aut, cre] (ORCID: <https://orcid.org/0000-0002-2864-3548>) |
| Maintainer: | Giuseppe Corbelli <[email protected]> |
| License: | GPL-3 |
| Version: | 0.1.0 |
| Built: | 2026-06-03 09:51:30 UTC |
| Source: | https://github.com/g-corbelli/lavinteract |
lavaan modelsComputes 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 )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 )
fit |
A fitted |
outcome |
Character. Name of the dependent variable in |
pred |
Character. Name of the focal predictor whose simple slopes are probed (required). |
modx |
Character. Name of the moderator (required). |
interaction |
Character. Name of the product term in |
data |
|
modx.values |
Numeric or character vector. Values or levels of the moderator
at which to compute slopes; derived automatically when |
modx.labels |
Character vector. Legend/table labels for |
pred.range |
Numeric length-2. Range |
conf.level |
Numeric in (0,1). Confidence level for CIs and ribbons (default: 0.95). |
x.label |
Character. X-axis label (default: |
y.label |
Character. Y-axis label (default: |
legend.title |
Character. Legend title; if |
colors |
Character vector. Colors for lines and ribbons; named vector recommended with names matching |
line.size |
Numeric > 0. Line width (default: 0.80). |
alpha |
Numeric in |
table |
Logical. Print APA-style interaction test and simple-slopes table (default: |
digits |
Integer |
modx_n_unique_cutoff |
Integer |
return_data |
Logical. If |
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.
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.
Estimates are unstandardized; a standardized beta for the interaction is also reported for reference. Wald tests assume large-sample normality of estimates.
## Not run: library(lavaan) # Fit a model with X, Z, and their product X_Z predicting Y # model <- ' Y ~ X + Z + X_Z ' # fit <- sem(model, data = your_data) # lav_slopes(fit, outcome = "Y", pred = "X", modx = "Z", # interaction = "X_Z", legend.title = "Moderator") ## End(Not run)## Not run: library(lavaan) # Fit a model with X, Z, and their product X_Z predicting Y # model <- ' Y ~ X + Z + X_Z ' # fit <- sem(model, data = your_data) # lav_slopes(fit, outcome = "Y", pred = "X", modx = "Z", # interaction = "X_Z", legend.title = "Moderator") ## End(Not run)
lavaan Fitted ObjectsTools to test and visualize moderation in structural equation models fitted
with lavaan using model estimates and their covariance without refitting.
The framework accepts continuous or categorical moderators.
The function is:
lav_slopes: simple slopes and interaction plots from a fitted lavaan model.
The development of this package was possible only thanks to the constant interaction (sic) with Dr. Cataldo Giuliano Gemmano (ORCID: 0000-0003-4519-8594), and particularly because of his sustained feedback and support.
Giuseppe Corbelli (<[email protected]>, or <[email protected]>)
Useful links:
Report bugs at https://github.com/g-corbelli/lavinteract/issues