simple_slopes {modsem} | R Documentation |
Get the simple slopes of a SEM model
Description
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,
standardized = FALSE,
...
)
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 ( |
model |
An object of class |
vals_x |
Numeric vector of values of |
vals_z |
Numeric vector of values of the moderator |
rescale |
Logical. If |
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 |
relative_h0 |
Logical. If |
standardized |
Should coefficients be standardized beforehand? |
... |
Additional arguments passed to lower-level functions or other internal helpers. |
Details
Computation Steps
The function extracts parameter estimates (and, if necessary, their covariance matrix) from the fitted SEM model (
model
).It identifies the coefficients for
x
,z
, andx:z
in the model's parameter table, as well as the variance ofx
,z
and the residual variance ofy
.If
xz
is not provided, it will be constructed by combiningx
andz
with a colon (":"
). Depending on the approach used to estimate the model, the colon may be removed or replaced internally; the function attempts to reconcile that.A grid of
x
andz
values is created fromvals_x
andvals_z
. Ifrescale = TRUE
, these values are transformed back into raw metric units for display in the output.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.The standard error (
std.error
) is derived from the covariance matrix of the relevant parameters, and ifci_type = "prediction"
, adds the residual variance.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.
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.
Examples
## Not run:
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)
## End(Not run)