View source: R/simple_slopes.R
simple_slopes | R Documentation |
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,
...
)
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 |
... |
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.
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.
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.