View source: R/plot_interaction.R
| plot_interaction | R Documentation | 
This function creates an interaction plot of the outcome variable (y) as a function
of a focal predictor (x) at multiple values of a moderator (z). It is
designed for use with structural equation modeling (SEM) objects (e.g., those from
modsem). Predicted means (or predicted individual values) are calculated
via simple_slopes, and then plotted with ggplot2 to display
multiple regression lines and confidence/prediction bands.
plot_interaction(
  x,
  z,
  y,
  xz = NULL,
  vals_x = seq(-3, 3, 0.001),
  vals_z,
  model,
  alpha_se = 0.15,
  digits = 2,
  ci_width = 0.95,
  ci_type = "confidence",
  rescale = TRUE,
  standardized = FALSE,
  ...
)
| x | A character string specifying the focal predictor (x-axis variable). | 
| z | A character string specifying the moderator variable. | 
| y | A character string specifying the outcome (dependent) variable. | 
| xz | A character string specifying the interaction term ( | 
| vals_x | A numeric vector of values at which to compute and plot the focal
predictor  | 
| vals_z | A numeric vector of values of the moderator  | 
| model | An object of class  | 
| alpha_se | A numeric value in  | 
| digits | An integer specifying the number of decimal places to which the
moderator values ( | 
| ci_width | A numeric value in  | 
| ci_type | A character string specifying whether to compute
 | 
| rescale | Logical. If  | 
| standardized | Should coefficients be standardized beforehand? | 
| ... | Additional arguments passed on to  | 
Computation Steps:
 Calls simple_slopes to compute the predicted values of y
at the specified grid of x and z values.
 Groups the resulting predictions by unique z-values (rounded to
digits) to create colored lines.
 Plots these lines using ggplot2, adding ribbons for confidence
(or prediction) intervals, with transparency controlled by alpha_se.
Interpretation:
Each line in the plot corresponds to the regression of y on x at
a given level of z. The shaded region around each line (ribbon) shows
either the confidence interval for the predicted mean (if ci_type =
"confidence") or the prediction interval for individual observations (if
ci_type = "prediction"). Where the ribbons do not overlap, there is
evidence that the lines differ significantly over that range of x.
A ggplot object that can be further customized (e.g., with
additional + theme(...) layers). By default, it shows lines for each
moderator value and a shaded region corresponding to the interval type
(confidence or prediction).
## Not run: 
library(modsem)
# Example 1: Interaction of X and Z in a simple SEM
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)
# Plot interaction using a moderate range of X and two values of Z
plot_interaction(x = "X", z = "Z", y = "Y", xz = "X:Z",
                 vals_x = -3:3, vals_z = c(-0.2, 0), model = est1)
# Example 2: Interaction in a theory-of-planned-behavior-style model
tpb <- "
# Outer Model
  ATT =~ att1 + att2 + att3 + att4 + att5
  SN  =~ sn1 + sn2
  PBC =~ pbc1 + pbc2 + pbc3
  INT =~ int1 + int2 + int3
  BEH =~ b1 + b2
# Inner Model
  INT ~ ATT + SN + PBC
  BEH ~ INT + PBC
  BEH ~ PBC:INT
"
est2 <- modsem(tpb, data = TPB, method = "lms", nodes = 32)
# Plot with custom Z values and a denser X grid
plot_interaction(x = "INT", z = "PBC", y = "BEH",
                 xz = "PBC:INT",
                 vals_x = seq(-3, 3, 0.01),
                 vals_z = c(-0.5, 0.5),
                 model = est2)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.