#Packages

library(lavaan)
## This is lavaan 0.6-16
## lavaan is FREE software! Please report any bugs.
library(semPlot)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(psych)
## 
## Attaching package: 'psych'
## 
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
## 
## The following object is masked from 'package:lavaan':
## 
##     cor2cov
library(jtools)
library(interactions)
library(ggplot2)
library(tidySEM)
## Loading required package: OpenMx
## OpenMx may run faster if it is compiled to take advantage of multiple cores.
## 
## Attaching package: 'OpenMx'
## 
## The following object is masked from 'package:psych':
## 
##     tr
## 
## Registered S3 method overwritten by 'tidySEM':
##   method          from  
##   predict.MxModel OpenMx
## 
## Attaching package: 'tidySEM'
## 
## The following object is masked from 'package:jtools':
## 
##     get_data
library(dplyr)
library(lmtest)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## 
## The following object is masked from 'package:psych':
## 
##     logit
## 
## The following object is masked from 'package:dplyr':
## 
##     recode
## 
## The following object is masked from 'package:purrr':
## 
##     some
library(interactions)
library(ggplot2)

#Import data

Black_Data <- read.csv("Black_Data.csv", header = TRUE)
d <- read.csv("Black_Data.csv", header = TRUE)

#data set with only variables of interest

d <- data.frame(d$JH, d$SLESQ, d$CSA, d$PCL5)

d <- rename(d, John_Henryism = d.JH, Stressful_Life_Events  = d.SLESQ, Central_Sensitization = d.CSA, Trauma_Symptoms = d.PCL5)

#remove NA variables

d <- na.omit(d)

Simple moderation (IV: stressful life events, DV: trauma symptoms, Moderator: John Henryism)

model1 <- lm(Trauma_Symptoms ~ Stressful_Life_Events * John_Henryism, d)
summary(model1)
## 
## Call:
## lm(formula = Trauma_Symptoms ~ Stressful_Life_Events * John_Henryism, 
##     data = d)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.03305 -0.73612  0.03766  0.64431  2.50859 
## 
## Coefficients:
##                                     Estimate Std. Error t value Pr(>|t|)
## (Intercept)                          0.08550    2.16376   0.040    0.969
## Stressful_Life_Events                2.23994    1.70867   1.311    0.191
## John_Henryism                       -0.38311    0.56784  -0.675    0.500
## Stressful_Life_Events:John_Henryism -0.01786    0.44744  -0.040    0.968
## 
## Residual standard error: 0.9433 on 255 degrees of freedom
## Multiple R-squared:  0.2336, Adjusted R-squared:  0.2245 
## F-statistic:  25.9 on 3 and 255 DF,  p-value: 1.167e-14

#graph interaction

interactions::interact_plot(model1, pred = Trauma_Symptoms, modx = John_Henryism, main.title = "Main Analyses with Path Coefficients") +
    ylim(1, 10)
## Warning: Trauma_Symptoms and John_Henryism are not included in an interaction with
## one another in the model.
## `geom_path()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?

Conduct simple moderation (stressful life events = IV, central sensitization = DV, John Henryism = Moderator)

model2 <- lm(Central_Sensitization ~ Stressful_Life_Events * John_Henryism, d)
summary(model2)
## 
## Call:
## lm(formula = Central_Sensitization ~ Stressful_Life_Events * 
##     John_Henryism, data = d)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.45354 -0.45727 -0.03608  0.41164  1.78493 
## 
## Coefficients:
##                                     Estimate Std. Error t value Pr(>|t|)
## (Intercept)                          1.47820    1.44839   1.021    0.308
## Stressful_Life_Events                1.51704    1.14375   1.326    0.186
## John_Henryism                       -0.48112    0.38011  -1.266    0.207
## Stressful_Life_Events:John_Henryism -0.05549    0.29951  -0.185    0.853
## 
## Residual standard error: 0.6314 on 255 degrees of freedom
## Multiple R-squared:  0.326,  Adjusted R-squared:  0.318 
## F-statistic: 41.11 on 3 and 255 DF,  p-value: < 2.2e-16
interactions::interact_plot(model2, pred = Central_Sensitization, modx = John_Henryism, main.title = "Main Analyses with Path Coefficients") +
    ylim(1, 10)
## Warning: Central_Sensitization and John_Henryism are not included in an interaction
## with one another in the model.
## `geom_path()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?

Simple mediation (IV: stressful life events, DV: central sensitization, Mediator: trauma symptoms as measured by the PCL 5)

# Y = central_sensitization; X = stressful_life_events; M = trauma_symptoms
LMedModel <- "
          Central_Sensitization ~ b*Trauma_Symptoms + c_p*Stressful_Life_Events 
          Trauma_Symptoms ~a*Stressful_Life_Events
          
          #intercepts
          Stressful_Life_Events ~ Stressful_Life_Events.mean*1
          Central_Sensitization ~ Central_Sensitization.mean*1
          
          indirect :=  a*b
          direct  := c_p
          total_c  := c_p + (a*b)
          "
set.seed(230925)  #required for reproducible results because lavaan introduces randomness into the calculations
LMed_fit <- lavaan::sem(LMedModel, data = d, se = "bootstrap", missing = "fiml")
LMed_Sum <- lavaan::summary(LMed_fit, standardized = T, rsq = T, ci = TRUE)
LMed_ParEsts <- lavaan::parameterEstimates(LMed_fit, boot.ci.type = "bca.simple",
    standardized = TRUE)
LMed_Sum
## lavaan 0.6.16 ended normally after 7 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         9
## 
##   Number of observations                           259
##   Number of missing patterns                         1
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Parameter Estimates:
## 
##   Standard errors                            Bootstrap
##   Number of requested bootstrap draws             1000
##   Number of successful bootstrap draws            1000
## 
## Regressions:
##                           Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##   Central_Sensitization ~                                                      
##     Trm_Symp   (b)           0.410    0.038   10.673    0.000    0.334    0.486
##     Strs_L_E (c_p)           0.345    0.189    1.822    0.068   -0.034    0.728
##   Trauma_Symptoms ~                                                            
##     Strs_L_E   (a)           2.096    0.298    7.045    0.000    1.533    2.711
##    Std.lv  Std.all
##                   
##     0.410    0.574
##     0.345    0.096
##                   
##     2.096    0.418
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##     Str_L_E (S_L_)    1.299    0.013  100.273    0.000    1.274    1.325
##    .Cntrl_S (C_S.)    0.344    0.226    1.524    0.127   -0.089    0.783
##    .Trm_Sym          -1.230    0.387   -3.174    0.002   -2.062   -0.495
##    Std.lv  Std.all
##     1.299    6.090
##     0.344    0.450
##    -1.230   -1.150
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##    .Centrl_Snstztn    0.358    0.028   13.008    0.000    0.300    0.409
##    .Trauma_Symptms    0.943    0.072   13.152    0.000    0.788    1.078
##     Strssfl_Lf_Evn    0.045    0.003   13.986    0.000    0.039    0.052
##    Std.lv  Std.all
##     0.358    0.614
##     0.943    0.825
##     0.045    1.000
## 
## R-Square:
##                    Estimate
##     Centrl_Snstztn    0.386
##     Trauma_Symptms    0.175
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##     indirect          0.860    0.151    5.698    0.000    0.585    1.182
##     direct            0.345    0.189    1.822    0.069   -0.034    0.728
##     total_c           1.204    0.206    5.847    0.000    0.815    1.626
##    Std.lv  Std.all
##     0.860    0.240
##     0.345    0.096
##     1.204    0.336

Moderated mediation of all 4 of these variables (IV: stressful life events, DV: central sensitization, Mediator: trauma symptoms, Moderator: John Henryism)

Combined <- '
    #equations
    Trauma_Symptoms ~ a1*Stressful_Life_Events + a2*John_Henryism + a3*Stressful_Life_Events:John_Henryism
    Central_Sensitization ~ c_p1*Stressful_Life_Events + c_p2*John_Henryism + c_p3*Stressful_Life_Events:John_Henryism + b*Trauma_Symptoms

    #intercepts
    Trauma_Symptoms ~ Trauma_Symptoms.mean*1
   Central_Sensitization ~ Central_Sensitization.mean*1

    #means, variances of W for simple slopes
    John_Henryism ~ John_Henryism.mean*1
    John_Henryism ~~ John_Henryism.var*John_Henryism
    
    #index of moderated mediation, there will be an a and b path in the product
    #if the a and/or b path is moderated, select the label that represents the moderation
    
    imm := a3*b
#Note that we first create the indirect product, then add to it the product of the imm and the W level
    indirect.SDbelow := a1*b + imm*(John_Henryism.mean - sqrt(John_Henryism.var))
    indirect.mean := a1*b + imm*(John_Henryism.mean)
    indirect.SDabove := a1*b + imm*(John_Henryism.mean + sqrt(John_Henryism.var))

    #direct effect is also moderated so calculate with c_p1 + c_p3
    direct.SDbelow := c_p1 + c_p3*(John_Henryism.mean - sqrt(John_Henryism.var)) 
    direct.Smean := c_p1 + c_p3*(John_Henryism.mean)
    direct.SDabove := c_p1 + c_p3*(John_Henryism.mean + sqrt(John_Henryism.var))

 '
set.seed(230925) #required for reproducible results because lavaan introduces randomness into the calculations
Combined_fit <- lavaan::sem(Combined, data = d, se = "bootstrap", missing = 'fiml', fixed.x= FALSE , bootstrap = 1000)
cFITsum <- lavaan::summary(Combined_fit, standardized = TRUE, rsq=T, ci=TRUE)    
cParamEsts <- lavaan::parameterEstimates(Combined_fit, boot.ci.type = "bca.simple", standardized=TRUE)
cFITsum
## lavaan 0.6.16 ended normally after 26 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        18
## 
##   Number of observations                           259
##   Number of missing patterns                         1
## 
## Model Test User Model:
##                                                       
##   Test statistic                               945.503
##   Degrees of freedom                                 2
##   P-value (Chi-square)                           0.000
## 
## Parameter Estimates:
## 
##   Standard errors                            Bootstrap
##   Number of requested bootstrap draws             1000
##   Number of successful bootstrap draws            1000
## 
## Regressions:
##                           Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##   Trauma_Symptoms ~                                                            
##     Str_L_E   (a1)           2.240    1.776    1.261    0.207   -0.997    5.733
##     Jhn_Hnr   (a2)          -0.383    0.610   -0.628    0.530   -1.510    0.795
##     S_L_E:J   (a3)          -0.018    0.475   -0.038    0.970   -0.942    0.832
##   Central_Sensitization ~                                                      
##     Str_L_E (c_p1)           0.761    1.065    0.715    0.475   -1.330    2.825
##     Jhn_Hnr (c_p2)          -0.352    0.334   -1.053    0.292   -1.033    0.286
##     S_L_E:J (c_p3)          -0.049    0.274   -0.181    0.857   -0.575    0.500
##     Trm_Sym    (b)           0.337    0.037    9.159    0.000    0.265    0.409
##    Std.lv  Std.all
##                   
##     2.240    0.445
##    -0.383   -0.229
##    -0.018   -0.020
##                   
##     0.761    0.216
##    -0.352   -0.300
##    -0.049   -0.077
##     0.337    0.481
## 
## Covariances:
##                            Estimate  Std.Err  z-value  P(>|z|) ci.lower
##   Stressful_Life_Events ~~                                             
##     Strssf_L_E:J_H            0.182    0.017   10.522    0.000    0.147
##  ci.upper   Std.lv  Std.all
##                            
##     0.214    0.182    0.725
## 
## Intercepts:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##    .Trm_Sym (T_S.)    0.085    2.308    0.037    0.970   -4.358    4.427
##    .Cntrl_S (C_S.)    1.449    1.299    1.116    0.264   -1.066    3.988
##     Jhn_Hnr (J_H.)    3.696    0.039   93.661    0.000    3.613    3.773
##     Str_L_E           1.299    0.013  100.273    0.000    1.274    1.325
##     S_L_E:J           4.809    0.071   67.553    0.000    4.655    4.941
##    Std.lv  Std.all
##     0.085    0.080
##     1.449    1.927
##     3.696    5.771
##     1.299    6.090
##     4.809    4.083
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##     Jhn_Hnr (J_H.)    0.410    0.042    9.793    0.000    0.334    0.499
##    .Trm_Sym           0.876    0.064   13.585    0.000    0.738    0.993
##    .Cntrl_S           0.293    0.023   12.762    0.000    0.242    0.331
##     Str_L_E           0.045    0.003   13.986    0.000    0.039    0.052
##     S_L_E:J           1.387    0.126   11.036    0.000    1.147    1.638
##    Std.lv  Std.all
##     0.410    1.000
##     0.876    0.762
##     0.293    0.518
##     0.045    1.000
##     1.387    1.000
## 
## R-Square:
##                    Estimate
##     Trauma_Symptms    0.238
##     Centrl_Snstztn    0.482
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##     imm              -0.006    0.162   -0.037    0.970   -0.339    0.296
##     indirect.SDblw    0.737    0.161    4.571    0.000    0.439    1.086
##     indirect.mean     0.733    0.126    5.841    0.000    0.523    1.007
##     indirect.SDabv    0.730    0.164    4.435    0.000    0.420    1.069
##     direct.SDbelow    0.610    0.283    2.153    0.031    0.064    1.185
##     direct.Smean      0.579    0.191    3.027    0.002    0.205    0.947
##     direct.SDabove    0.547    0.232    2.359    0.018    0.091    1.020
##    Std.lv  Std.all
##    -0.006   -0.009
##     0.737    0.169
##     0.733    0.160
##     0.730    0.150
##     0.610   -0.154
##     0.579   -0.231
##     0.547   -0.309