Get the simple slopes of a SEM model
simple_slopes.Rd
This function calculates simple slopes (predicted values of the outcome variable)
at user-specified values of the focal predictor (x
) and moderator (z
)
in a structural equation modeling (SEM) framework. It supports interaction terms
(xz
), computes standard errors (SE), and optionally returns confidence or
prediction intervals for these predicted values. It also provides p-values for
hypothesis testing. This is useful for visualizing and interpreting moderation
effects or to see how the slope changes at different values of the moderator.
Usage
simple_slopes(
x,
z,
y,
xz = NULL,
model,
vals_x = -3:3,
vals_z = -1:1,
rescale = TRUE,
ci_width = 0.95,
ci_type = "confidence",
relative_h0 = TRUE,
...
)
Arguments
- x
The name of the variable on the x-axis (focal predictor).
- z
The name of the moderator variable.
- y
The name of the outcome variable.
- xz
The name of the interaction term (
x:z
). IfNULL
, it will be created by combiningx
andz
with a colon (e.g.,"x:z"
). Some backends may remove or alter the colon symbol, so the function tries to account for that internally.- model
An object of class
modsem_pi
,modsem_da
,modsem_mplus
, or alavaan
object. This should be a fitted SEM model that includes paths fory ~ x + z + x:z
.- vals_x
Numeric vector of values of
x
at which to compute predicted slopes. Defaults to-3:3
. Ifrescale = TRUE
, these values are taken relative to the mean and standard deviation ofx
. A higher density of points (e.g.,seq(-3, 3, 0.1)
) will produce smoother curves and confidence bands.- vals_z
Numeric vector of values of the moderator
z
at which to compute predicted slopes. Defaults to-1:1
. Ifrescale = TRUE
, these values are taken relative to the mean and standard deviation ofz
. Each unique value ofz
generates a separate regression liney ~ x | z
.- rescale
Logical. If
TRUE
(default),x
andz
are standardized according to their means and standard deviations in the model. The values invals_x
andvals_z
are interpreted in those standardized units. The raw (unscaled) values corresponding to these standardized points will be displayed in the returned table.- ci_width
A numeric value between 0 and 1 indicating the confidence (or prediction) interval width. The default is 0.95 (i.e., 95% interval).
- ci_type
A string indicating whether to compute a
"confidence"
interval for the predicted mean (i.e., uncertainty in the regression line) or a"prediction"
interval for individual outcomes. The default is"confidence"
. If"prediction"
, the residual variance is added to the variance of the fitted mean, resulting in a wider interval.- relative_h0
Logical. If
TRUE
(default), hypothesis tests for the predicted values (predicted - h0
) assumeh0
is the model-estimated mean ofy
. IfFALSE
, the null hypothesis ish0 = 0
.- ...
Additional arguments passed to lower-level functions or other internal helpers.
Value
A data.frame
(invisibly inheriting class "simple_slopes"
)
with columns:
vals_x
,vals_z
: The requested grid values ofx
andz
.predicted
: The predicted value ofy
at that combination ofx
andz
.std.error
: The standard error of the predicted value.z.value
,p.value
: The z-statistic and corresponding p-value for testing the null hypothesis thatpredicted == h0
.ci.lower
,ci.upper
: Lower and upper bounds of the confidence (or prediction) interval.
An attribute "variable_names"
(list of x
, z
, y
)
is attached for convenience. Typically, the returned object can be passed to
plot()
or plot.simple_slopes
to visualize the slopes and their
intervals.
Details
Computation Steps
1. The function extracts parameter estimates (and, if necessary, their covariance
matrix) from the fitted SEM model (model
).
2. It identifies the coefficients for x
, z
, and x:z
in the model's
parameter table, as well as the variance of x
, z
, and the residual.
3. If xz
is not provided, it will be constructed by combining x
and
z
with a colon (":"
). In certain SEM software, the colon may be
removed or replaced internally; the function attempts to reconcile that.
4. A grid of x
and z
values is created from vals_x
and
vals_z
. If rescale = TRUE
, these values are transformed back into raw
metric units for display in the output.
5. For each point in the grid, a predicted value of y
is computed via
(beta0 + beta_x * x + beta_z * z + beta_xz * x * z)
and, if included, a
mean offset.
6. The standard error (std.error
) is derived from the covariance matrix of
the relevant parameters, and if ci_type = "prediction"
, adds the residual
variance.
7. Confidence (or prediction) intervals are formed using ci_width
(defaulting
to 95%). The result is a table-like data frame with predicted values, CIs,
standard errors, z-values, and p-values.
Examples
if (FALSE) { # \dontrun{
library(modsem)
m1 <- "
# Outer Model
X =~ x1 + x2 + x3
Z =~ z1 + z2 + z3
Y =~ y1 + y2 + y3
# Inner model
Y ~ X + Z + X:Z
"
est1 <- modsem(m1, data = oneInt)
# Simple slopes at X in [-3, 3] and Z in [-1, 1], rescaled to the raw metric.
simple_slopes(x = "X", z = "Z", y = "Y", model = est1)
# If the data or user wants unscaled values, set rescale = FALSE, etc.
simple_slopes(x = "X", z = "Z", y = "Y", model = est1, rescale = FALSE)
} # }