--- title: "GenerateModelCN" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{GenerateModelCN} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ## Introduction The `GenerateModelCN` function dynamically generates a Structural Equation Model (SEM) formula to analyze chained or nested mediation for 'lavaan' based on the prepared dataset. This document explains the mathematical principles and the structure of the generated model.

serial within-subject mediation model

--- ## 1. Difference Model Description ### 1.1 Regression for \( Y_{\text{diff}} \) and \( M_{\text{diff}} \) For \( N \) mediators \( M_1, M_2, \dots, M_N \), the difference model is defined as: 1. **Outcome Difference Model (\( Y_{\text{diff}} \)):** \[ Y_{\text{diff}} = cp + \sum_{i=1}^N \left( b_i M_{\text{diff},i} + d_i M_{\text{avg},i} \right) + e \] 2. **Mediator Difference Model (\( M_{\text{diff},i} \)):** \[ M_{\text{diff},i} = a_i + \sum_{j 0 \): Pathway \( \text{path}_1 \) has a stronger indirect effect. - \( CI_{\text{path}_1\text{vs}\text{path}_2} < 0 \): Pathway \( \text{path}_2 \) has a stronger indirect effect. --- ### 4.2 Example: Three Mediators \( M_1, M_2, M_3 \) #### Indirect Effects For three mediators, the following indirect effects are defined: 1. **Direct Path Effects:** \[ \text{indirect}_1 = a_1 \cdot b_1 \] \[ \text{indirect}_2 = a_2 \cdot b_2 \] \[ \text{indirect}_3 = a_3 \cdot b_3 \] 2. **Chained Path Effects:** \[ \text{indirect}_{12} = a_1 \cdot b_{12} \cdot b_2 \] \[ \text{indirect}_{23} = a_2 \cdot b_{23} \cdot b_3 \] \[ \text{indirect}_{123} = a_1 \cdot b_{12} \cdot b_{23} \cdot b_3 \] #### Comparisons The indirect effects are compared as follows: \[ CI_{1\text{vs}2} = \text{indirect}_1 - \text{indirect}_2 \] \[ CI_{1\text{vs}3} = \text{indirect}_1 - \text{indirect}_3 \] \[ CI_{2\text{vs}3} = \text{indirect}_2 - \text{indirect}_3 \] \[ CI_{1\text{vs}12} = \text{indirect}_1 - \text{indirect}_{12} \] \[ CI_{1\text{vs}23} = \text{indirect}_1 - \text{indirect}_{23} \] \[ CI_{1\text{vs}123} = \text{indirect}_1 - \text{indirect}_{123} \] \[ CI_{2\text{vs}12} = \text{indirect}_2 - \text{indirect}_{12} \] \[ CI_{2\text{vs}23} = \text{indirect}_2 - \text{indirect}_{23} \] \[ CI_{2\text{vs}123} = \text{indirect}_2 - \text{indirect}_{123} \] \[ CI_{3\text{vs}12} = \text{indirect}_3 - \text{indirect}_{12} \] \[ CI_{3\text{vs}23} = \text{indirect}_3 - \text{indirect}_{23} \] \[ CI_{3\text{vs}123} = \text{indirect}_3 - \text{indirect}_{123} \] \[ CI_{12\text{vs}23} = \text{indirect}_{12} - \text{indirect}_{23} \] \[ CI_{12\text{vs}123} = \text{indirect}_{12} - \text{indirect}_{123} \] \[ CI_{23\text{vs}123} = \text{indirect}_{23} - \text{indirect}_{123} \] --- ## 5. C1 and C2 Coefficients For C1- and C2-measurement conditions, the coefficients are calculated as follows: 1. **C2-Measurement Coefficient (\( X1_{b,i} \)):** \[ X1_{b,i} = b_i + d_i \] 2. **C1-Measurement Coefficient (\( X0_{b,i} \)):** \[ X0_{b,i} = X1_{b,i} - d_i \] For chained pathways: 1. **C2-Measurement Coefficient (\( X1_{b,ij} \)):** \[ X1_{b,ij} = b_{ij} + d_{ij} \] 2. **C1-Measurement Coefficient (\( X0_{b,ij} \)):** \[ X0_{b,ij} = X1_{b,ij} - d_{ij} \] --- For three mediators \( M_1, M_2, M_3 \), the coefficients are calculated as follows: - C2-Measurement Coefficient: \[ X1_{b,1} = b_1 + d_1 \] - C1-Measurement Coefficient: \[ X0_{b,1} = X1_{b,1} - d_1 \] - C2-Measurement Coefficient: \[ X1_{b,2} = b_2 + d_2 \] - C1-Measurement Coefficient: \[ X0_{b,2} = X1_{b,2} - d_2 \] - C2-Measurement Coefficient: \[ X1_{b,3} = b_3 + d_3 \] - C1-Measurement Coefficient: \[ X0_{b,3} = X1_{b,3} - d_3 \] - C2-Measurement Coefficient: \[ X1_{b,12} = b_{12} + d_{12} \] - C1-Measurement Coefficient: \[ X0_{b,12} = X1_{b,12} - d_{12} \] --- ## 6. Summary of Regression Equations This section summarizes all the regression equations: 1. **Outcome Difference Model (\( Y_{\text{diff}} \)):** \[ Y_{\text{diff}} = cp + \sum_{i=1}^N \left( b_i M_{\text{diff},i} + d_i M_{\text{avg},i} \right) + e \] 2. **Mediator Difference Model (\( M_{\text{diff},i} \)):** \[ M_{\text{diff},i} = a_i + \sum_{j