---
title: "GenerateModelP"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{GenerateModelP}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
## Introduction
The `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.
---
## 1. Difference Model Description
### 1.1 Regression for \( Y_1 \) and \( Y_2 \)
For \( N \) mediators \( M_1, M_2, \dots, M_N \), and \( k = 2 \) within-subject conditions (Condition 1 and Condition 2), the model is defined as:
1. For Condition 1:
\[
Y_1 = b_{10} + \sum_{i=1}^N b_{i1} M_i + e_1
\]
2. For Condition 2:
\[
Y_2 = b_{20} + \sum_{i=1}^N b_{i2} M_i + e_2
\]
Where:
- \( b_{10} \) and \( b_{20} \): Intercepts for the two conditions.
- \( b_{i1} \) and \( b_{i2} \): Regression coefficients for the mediators under Condition 1 and Condition 2.
- \( e_1 \) and \( e_2 \): Residuals for the two conditions.
---
### 1.2 Difference Model for \( Y_{\text{diff}} \)
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}
\]
2. **Mediator average**:
\[
M_{\text{avg},i} = \frac{M_{i1} + M_{i2}}{2}
\]
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
\]
---
### 1.3 Regression for \( M_{\text{diff}} \)
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} \).
---
## 2. Indirect Effects
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
\]
---
## 3. Total Effect
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.
---
## 4. Comparison of Indirect Effects
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.
---
### 4.1 Indirect Effect Definition
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.
---
### 4.2 Comparing Indirect Effects
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
\]
#### **Interpretation**
1. **\( CI_{i,j} > 0 \)**:
- Mediator \( M_i \) has a stronger indirect effect than \( M_j \).
2. **\( CI_{i,j} < 0 \)**:
- Mediator \( M_j \) has a stronger indirect effect than \( M_i \).
3. **\( CI_{i,j} = 0 \)**:
- Both mediators contribute equally to the indirect effect.
---
## 5. C1- and C2-Measurement Coefficients
To compute C1- and C2-measurement coefficients \( X1_{b,i} \) and \( X0_{b,i} \), consider two mediators \( M_1 \) and \( M_2 \):
---
### 5.1 Difference Model with Two Mediators
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.
---
### 5.2 C2-Measurement Coefficients
The C2-measurement coefficient \( X1_{b,i} \) is defined as:
\[
X1_{b,i} = b + d
\]
Substitute \( b \) and \( d \):
\[
X1_{b,i} = \frac{b_{11} + b_{21}}{2} + (b_{21} - b_{11}) = b_{21}
\]
Thus, \( X1_{b,i} \) is the effect of \( M_i \) under Condition 2.
---
### 5.3 C1-Measurement Coefficients
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.
---
## 6. Summary of Regression Equations
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.
---
### 6.1 Difference Model
\[
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
\]
---
### 6.2 Defined parameters
\[
\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
\]
\[
X0_{b,i} = X1_{b,i} - d_i
\]
---
### Summary
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.
---