GenerateModelP function dynamically generates a
Structural Equation Model (SEM) formula to analysis parallel mediation
for ‘lavaan’ based on the prepared dataset. This document explains the
mathematical principles and the structure of the generated model.
Taking the difference between the two conditions: \[ Y_{\text{diff}} = Y_2 - Y_1 = (b_{20} - b_{10}) + \sum_{i=1}^N b_{i2} M_{i2} - \sum_{i=1}^N b_{i1} M_{i1} + (e_2 - e_1) \]
Define: - \(\Delta b_0 = b_{20} - b_{10}\): Difference in intercepts. - \(e = e_2 - e_1\): Difference in residuals.
Substitute mediator difference and average: 1. Mediator difference: \[ M_{\text{diff},i} = M_{i2} - M_{i1} \]
Substitute \(M_{i2} = M_{\text{avg},i} + \frac{M_{\text{diff},i}}{2}\) and \(M_{i1} = M_{\text{avg},i} - \frac{M_{\text{diff},i}}{2}\) into the equation: \[ Y_{\text{diff}} = \Delta b_0 + \sum_{i=1}^N \left( \frac{b_{i1} + b_{i2}}{2} \cdot M_{\text{diff},i} + (b_{i2} - b_{i1}) \cdot M_{\text{avg},i} \right) + e \]
Define: - \(b_i = \frac{b_{i1} + b_{i2}}{2}\): Average effect of the \(i\)-th mediator. - \(d_i = b_{i2} - b_{i1}\): Difference in the effect of the \(i\)-th mediator.
The final equation becomes: \[ Y_{\text{diff}} = \Delta b_0 + \sum_{i=1}^N \left( b_i M_{\text{diff},i} + d_i M_{\text{avg},i} \right) + e \]
Each mediator difference \(M_{\text{diff},i}\) is modeled as: \[ M_{\text{diff},i} = a_i + \epsilon_i \]
Where: - \(a_i\): Intercept term for the \(i\)-th mediator difference. - \(\epsilon_i\): Residual for \(M_{\text{diff},i}\).
For each mediator \(M_i\), the indirect effect is defined as: \[ \text{indirect}_i = a_i \cdot b_i \]
Where: - \(a_i\): Effect of the independent variable on mediator \(M_i\). - \(b_i\): Average effect of mediator \(M_i\) on the dependent variable.
The total indirect effect is: \[ \text{total_indirect} = \sum_{i=1}^N \text{indirect}_i \]
The contrast between indirect effects of two mediators \(M_i\) and \(M_j\) is: \[ CI_{i,j} = \text{indirect}_i - \text{indirect}_j \]
The total effect combines the direct effect and the total indirect effect: \[ \text{total_effect} = c_p + \text{total_indirect} \]
Where \(c_p\) is the direct effect of the independent variable on the dependent variable.
When there are multiple mediators (\(M_1, M_2, \dots, M_N\)), comparing their indirect effects provides insights into the relative influence of each mediator. This section details the formulas and interpretations for such comparisons.
For a mediator \(M_i\), the indirect effect is defined as: \[ \text{indirect}_i = a_i \cdot b_i \]
Where: - \(a_i\): Effect of the independent variable on mediator \(M_i\). - \(b_i\): Average effect of mediator \(M_i\) on the dependent variable.
To compare the indirect effects of two mediators \(M_i\) and \(M_j\), we calculate the contrast: \[ CI_{i,j} = \text{indirect}_i - \text{indirect}_j \]
To compute C1- and C2-measurement coefficients \(X1_{b,i}\) and \(X0_{b,i}\), consider two mediators \(M_1\) and \(M_2\):
From the difference model: \[ Y_{\text{diff}} = \Delta b_0 + \left(\frac{b_{11} + b_{21}}{2}\right) M_{\text{diff}} + \left(b_{21} - b_{11}\right) M_{\text{avg}} + e \]
Define: - \(b = \frac{b_{11} + b_{21}}{2}\): Average effect. - \(d = b_{21} - b_{11}\): Difference in effect.
The C2-measurement coefficient \(X1_{b,i}\) is defined as: \[ X1_{b,i} = b + d/2 \]
Substitute \(b\) and \(d\): \[ X1_{b,i} = \frac{b_{11} + b_{21}}{2} + \frac{b_{21} - b_{11}}{2} = b_{21} \]
Thus, \(X1_{b,i}\) is the effect of \(M_i\) under Condition 2.
The C1-measurement coefficient \(X0_{b,i}\) is defined as: \[ X0_{b,i} = X1_{b,i} - d \]
Substitute \(X1_{b,i} = b_{21}\) and \(d = b_{21} - b_{11}\): \[ X0_{b,i} = b_{21} - (b_{21} - b_{11}) = b_{11} \]
Thus, \(X0_{b,i}\) is the effect of \(M_i\) under Condition 1.
Additional Interpretation: The coefficient \(d_i = b_{2i} - b_{1i}\) reflects the moderating effect of the within-subject variable X, capturing how the mediator’s influence differs across conditions.
This section summarizes all the regression equations used in the analysis, including the difference model, indirect effects, mediator comparisons, and C1- and C2-measurement coefficients.
\[ Y_{\text{diff}} = cp + \sum_{i=1}^N \left( b_i M_{\text{diff},i} + d_i M_{\text{avg},i} \right) + e \]
\[ M_{\text{diff},i} = a_i + \epsilon_i \]
\[ \text{indirect}_i = a_i \cdot b_i \]
\[ \text{total_indirect} = \sum_{i=1}^N \text{indirect}_i \]
\[ CI_{i,j} = \text{indirect}_i - \text{indirect}_j \]
\[ X1_{b,i} = b_i + d_i/2 \]
\[ X0_{b,i} = X1_{b,i} - d_i \]
By combining these equations: 1. The difference model \(Y_{\text{diff}}\) decomposes into contributions from mediator differences (\(M_{\text{diff}}\)) and averages (\(M_{\text{avg}}\)). 2. Indirect effects and their contrasts provide insights into the mediators’ relative importance. 3. C1- and C2-measurement coefficients quantify the effects in specific conditions.
GenerateModel*() constructs the unstandardized model
syntax. Request standardized estimates when fitting with
wsMed(..., standardized = TRUE). PrepareData()
uses condition-2 minus condition-1 differences and centered mediator
averages. Differences are scaled without recentering; centering the
average component changes the intercept, not the slopes above.
Let \(s_{D M_i}\), \(s_{A M_i}\) and \(s_{D Y}\) denote the marginal model-implied SDs of mediator differences, centered mediator averages and the outcome difference. Then
\[ a_i^*=a_i/s_{D M_i},\quad b_i^*=b_i s_{D M_i}/s_{D Y},\quad d_i^*=d_i s_{A M_i}/s_{D Y},\quad c'^*=c'/s_{D Y}. \]
A mediator-to-mediator coefficient uses its own source and destination SDs. Intermediate SDs cancel in a serial indirect-effect product, so every complete indirect effect satisfies \(IE^*(w)=IE(w)/s_{D Y}\). The same common marginal outcome SD applies across moderator values and categorical groups. Sum all model-implied indirect paths when computing the total indirect effect.
With moderators, use result$moderation_std for
standardized conditional effects and retain
result$moderation for raw effects. MP selects
requested interactions and focal paths; already-fitted moderator main
effects also enter conditional intercepts even when the corresponding
a or cp is absent from MP. See Standardized moderated
mediation for continuous and categorical examples, joint-draw
intervals and plots.
The condition-specific slopes above are reconstructed on the raw scale: \(X1_b=b+d/2\) and \(X0_b=b-d/2\). Do not apply this reconstruction directly to standardized \(b^*\) and \(d^*\), whose predictor scale factors differ. A difference between two indirect effects is a contrast estimate; its confidence interval must be computed from the joint sampling distribution.