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.
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,
...
)
A data.frame
(invisibly inheriting class "simple_slopes"
)
with columns:
vals_x
, vals_z
: The requested grid values of x
and z
.
predicted
: The predicted value of y
at that combination of
x
and z
.
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 that predicted == 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.
The name of the variable on the x-axis (focal predictor).
The name of the moderator variable.
The name of the outcome variable.
The name of the interaction term (x:z
). If NULL
, it will
be created by combining x
and z
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.
An object of class modsem_pi
, modsem_da
,
modsem_mplus
, or a lavaan
object. This should be a fitted SEM
model that includes paths for y ~ x + z + x:z
.
Numeric vector of values of x
at which to compute predicted
slopes. Defaults to -3:3
. If rescale = TRUE
, these values are taken
relative to the mean and standard deviation of x
. A higher density of points
(e.g., seq(-3, 3, 0.1)
) will produce smoother curves and confidence bands.
Numeric vector of values of the moderator z
at which to compute
predicted slopes. Defaults to -1:1
. If rescale = TRUE
, these values
are taken relative to the mean and standard deviation of z
. Each unique value
of z
generates a separate regression line y ~ x | z
.
Logical. If TRUE
(default), x
and z
are standardized
according to their means and standard deviations in the model. The values in
vals_x
and vals_z
are interpreted in those standardized units. The
raw (unscaled) values corresponding to these standardized points will be displayed
in the returned table.
A numeric value between 0 and 1 indicating the confidence (or prediction) interval width. The default is 0.95 (i.e., 95% interval).
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.
Logical. If TRUE
(default), hypothesis tests for the
predicted values (predicted - h0
) assume h0
is the model-estimated
mean of y
. If FALSE
, the null hypothesis is h0 = 0
.
Additional arguments passed to lower-level functions or other internal helpers.
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.
if (FALSE) {
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)
}
Run the code above in your browser using DataLab