User-defined mediation models with GenerateModelCustom
Source:vignettes/GenerateModelCustom.Rmd
GenerateModelCustom.RmdIntroduction
GenerateModelCustom() generates lavaan
syntax for a user-defined two-condition within-subject mediation model.
It is useful when the intended mediator structure cannot be represented
by one of the predefined parallel, chained, chainedβparallel, or
parallelβchained models in wsMed.
Users specify directed paths among mediators and from mediators to the outcome. For example:
custom_paths <- c(
"M1 -> M3",
"M3 -> Y",
"M2 -> Y"
)Here, M1, M2, and M3 denote
the mediators, and Y denotes the outcome. The user does not
specify paths beginning with X. The function automatically
adds an
path from the within-subject condition to every mediator and the direct
effect
from the condition to the outcome.
Mediator numbering
Mediator labels are assigned according to the order of the variables
supplied to M_C1 and M_C2. For example:
| Construct | Condition 1 | Condition 2 | Model label |
|---|---|---|---|
| A | A1 |
A2 |
M1 |
| B | B1 |
B2 |
M2 |
| C | C1 |
C2 |
M3 |
| D | D1 |
D2 |
Y |
Therefore, "M1 -> M3" represents a path from
construct A to construct C. Mediator numbering identifies variables; it
does not determine path direction.
Preparing example data
The following reproducible example creates three mediator pairs and one outcome pair. It does not depend on an external data file.
set.seed(123)
n <- 200
example_custom <- data.frame(
A1 = rnorm(n),
A2 = rnorm(n, mean = 0.30),
B1 = rnorm(n),
B2 = rnorm(n, mean = 0.20),
C1 = rnorm(n),
C2 = rnorm(n, mean = 0.25),
D1 = rnorm(n),
D2 = rnorm(n, mean = 0.35)
)PrepareData() converts the raw condition-specific
variables into mediator difference and centered level components and an
outcome difference score.
prepared_data <- PrepareData(
data = example_custom,
M_C1 = c("A1", "B1", "C1"),
M_C2 = c("A2", "B2", "C2"),
Y_C1 = "D1",
Y_C2 = "D2"
)
names(prepared_data)
#> [1] "Ydiff" "M1diff" "M2diff" "M3diff" "M1avg" "M2avg" "M3avg"For mediator , the two components are
and
where the average component is centered by
PrepareData().
Specifying a custom model
Each element of paths must describe either a
mediator-to-mediator path or a mediator-to-outcome path:
custom_paths <- c(
"M1 -> M3",
"M3 -> Y",
"M2 -> Y"
)Spaces around -> are optional. For example,
"M1 -> M3" and "M1->M3" are
equivalent.
The corresponding model syntax is generated as follows:
custom_model <- GenerateModelCustom(
prepared_data = prepared_data,
paths = custom_paths
)
cat(custom_model)
#> Ydiff ~ cp*1 + b2*M2diff + d2*M2avg + b3*M3diff + d3*M3avg
#> M1diff ~ a1*1
#> M2diff ~ a2*1
#> M3diff ~ a3*1 + b_1_3*M1diff + d_1_3*M1avg
#> indirect_1_3 := a1 * b_1_3 * b3
#> indirect_2 := a2 * b2
#> indirect_3 := a3 * b3
#> total_indirect := indirect_1_3 + indirect_2 + indirect_3
#> total_effect := cp + total_indirectFor every specified structural edge, the generated regression
includes both the difference and level components of the predictor
mediator. Thus, "M1 -> M3" contributes
b_1_3*M1diff + d_1_3*M1avgto the regression equation for M3diff.
Only specified structural paths are added. Because
"M1 -> Y" is absent from custom_paths,
M1diff and M1avg do not appear directly in the
regression equation for Ydiff.
Indirect effects
GenerateModelCustom() identifies every directed path
from a mediator to Y. The model above contains three
indirect effects:
The last effect exists because the path from the condition to every
mediator, including
,
is added automatically. All identified indirect effects are summed to
form total_indirect, and the total effect is defined as
cp + total_indirect.
Parameter labels
The generated model uses the following labels:
| Path | Difference component | Level component |
|---|---|---|
ai |
β | |
bi |
di |
|
b_j_i |
d_j_i |
|
cp |
β |
For example, the path
uses b_1_3 and d_1_3, whereas
uses b3 and d3.
Fitting the complete model with wsMed
For an applied analysis, users can pass the same path specification
directly to wsMed() by setting
form = "UD".
The following chunk is not evaluated while the vignette is knitted
because Monte Carlo confidence intervals can take some time. Remove
eval=FALSE to run it.
Input requirements
A valid custom path specification must meet the following requirements:
- Each path must use a form such as
"M1 -> M2"or"M2 -> Y". - Every referenced mediator must be present in
prepared_data. - Duplicate paths and self-loops such as
"M1 -> M1"are not allowed. - The mediator structure must be acyclic.
- At least one mediator must have a path ending in
Y.
Paths beginning with X, such as
"X -> M1" or "X -> Y", should not be
included because these paths are added automatically. Paths beginning
with Y are not permitted because Y is the
final outcome.
Summary
To fit a user-defined model:
- Supply matched mediator variables through
M_C1andM_C2. - Define the structural edges using mediator labels and
Y. - Use
GenerateModelCustom()to inspect the generated syntax or callwsMed(form = "UD", paths = ...)for the complete analysis.
This workflow retains the existing difference-and-level
parameterization in wsMed while allowing users to specify
mediation structures beyond the four predefined model forms.