Skip to contents

Dynamically generates a structural equation modeling (SEM) syntax for chained mediation analysis based on the prepared dataset. The function computes regression equations for mediators and the outcome variable, indirect effects along multi-step mediation paths, total effects, contrasts between indirect effects, and coefficients in different conditions.

Usage

GenerateModelCN(prepared_data, MP = character(0))

Arguments

prepared_data

A data frame returned by PrepareData(), containing the processed within-subject mediator and outcome variables. The data frame must include columns for difference scores (Mdiff) and average scores (Mavg) of mediators, as well as the outcome difference score (Ydiff).

MP

A character vector specifying which paths are moderated by variable(s) W. Valid entries include: - "a2", "a3", ...: moderation on the a paths (W → Mdiff), for mediators beyond M1. - "b2", "b3", ...: moderation on the b paths (Mdiff × W → Ydiff). - "b_1_2", "b_2_3", ...: moderation on cross-paths from one mediator to the next (e.g., M1 → M2). - "d2", "d3", ...: moderation on the d paths (Mavg × W → Ydiff). - "d_1_2", "d_2_3", ...: moderation on cross-paths from one Mavg to the next Mdiff. - "cp": moderation on the direct effect from X to Y (i.e., W → Ydiff).

      The function detects and inserts the correct interaction terms (e.g., \code{int_M2diff_W1}) based on these labels.

Value

A character string representing the SEM model syntax for the specified chained mediation analysis.

Details

This function is used to construct SEM models for chained mediation analysis. It automatically parses variable names from the prepared dataset and dynamically creates the necessary model syntax, including:

  • Outcome regression: Defines the relationship between the difference scores of the outcome (Ydiff) and the mediators (Mdiff) as well as their average scores (Mavg).

  • Mediator regressions: Defines the sequential regression models for each mediator's difference score, incorporating prior mediators as predictors.

  • Indirect effects: Computes the indirect effects along all possible multi-step mediation paths using the product of path coefficients.

  • Total indirect effect: Calculates the sum of all indirect effects from the chained mediation paths.

  • Total effect: Combines the direct effect (cp) and the total indirect effect.

  • Contrasts of indirect effects: Optionally calculates the pairwise contrasts between the indirect effects for different mediation paths.

  • Coefficients in different 'X' conditions: Calculates path coefficients in different X conditions to observe the moderation effect of X.

This model is suitable for chained mediation designs where mediators influence each other in a sequential manner, forming multi-step mediation paths.

Examples

# Example prepared data
prepared_data <- data.frame(
  M1diff = rnorm(100),
  M2diff = rnorm(100),
  M3diff = rnorm(100),
  M1avg = rnorm(100),
  M2avg = rnorm(100),
  M3avg = rnorm(100),
  Ydiff = rnorm(100)
)

# Generate SEM model syntax
sem_model <- GenerateModelCN(prepared_data)
cat(sem_model)
#> Ydiff ~ cp*1 + b1*M1diff + d1*M1avg + b2*M2diff + d2*M2avg + b3*M3diff + d3*M3avg
#> M1diff ~ a1*1
#> M2diff ~ a2*1 + b_1_2*M1diff + d_1_2*M1avg
#> M3diff ~ a3*1 + b_1_3*M1diff + d_1_3*M1avg + b_2_3*M2diff + d_2_3*M2avg
#> indirect_1 := a1 * b1
#> indirect_2 := a2 * b2
#> indirect_3 := a3 * b3
#> indirect_1_2 := a1 * b_1_2 * b2
#> indirect_1_3 := a1 * b_1_3 * b3
#> indirect_2_3 := a2 * b_2_3 * b3
#> indirect_1_2_3 := a1 * b_1_2 * b_2_3 * b3
#> total_indirect := indirect_1 + indirect_2 + indirect_3 + indirect_1_2 + indirect_1_3 + indirect_2_3 + indirect_1_2_3
#> total_effect := cp + total_indirect