Moderated Mediation(model no 7)

library(processR)
library(lavaan)
library(MPsychoR)

data(Paskvan)

labels=list(X="wintense",M="cogapp",Y="emotion",W="pclimate")
pmacroModel(7,labels=labels)

moderator=list(name="pclimate",site=list("a"))

model1=tripleEquation(labels=labels,mode=1)
cat(model1)
cogapp~wintense
emotion~cogapp+wintense
fit=eq2fit(model1,data=Paskvan)
summary(fit[[1]])

Call:
lm(formula = as.formula(eq[i]), data = data)

Residuals:
     Min       1Q   Median       3Q      Max 
-2.92667 -0.36259  0.05572  0.45502  2.63811 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  1.81612    0.08611   21.09   <2e-16 ***
wintense     0.54577    0.02373   23.00   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.6421 on 801 degrees of freedom
Multiple R-squared:  0.3976,    Adjusted R-squared:  0.3969 
F-statistic: 528.8 on 1 and 801 DF,  p-value: < 2.2e-16
summary(fit[[2]])

Call:
lm(formula = as.formula(eq[i]), data = data)

Residuals:
    Min      1Q  Median      3Q     Max 
-3.3928 -0.6739  0.0562  0.6594  4.0171 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  0.08933    0.16690   0.535    0.593    
cogapp       0.61143    0.05491  11.135   <2e-16 ***
wintense     0.24926    0.04753   5.245    2e-07 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.998 on 800 degrees of freedom
Multiple R-squared:  0.3184,    Adjusted R-squared:  0.3167 
F-statistic: 186.9 on 2 and 800 DF,  p-value: < 2.2e-16
modelsSummaryTable(fit,labels=labels)

Consequent

M(cogapp)

Y(emotion)

Antecedent

Coef

SE

t

p

Coef

SE

t

p

X(wintense)

0.546

0.024

22.995

<.001

0.249

0.048

5.245

<.001

M(cogapp)

0.611

0.055

11.135

<.001

Constant

1.816

0.086

21.090

<.001

0.089

0.167

0.535

.593

Observations

803

803

R2

0.398

0.318

Adjusted R2

0.397

0.317

Residual SE

0.642 ( df = 801)

0.998 ( df = 800)

F statistic

F(1,801) = 528.787, p < .001

F(2,800) = 186.888, p < .001

model=tripleEquation(labels=labels,moderator=moderator)
cat(model)
cogapp~a1*wintense+a2*pclimate+a3*wintense:pclimate
emotion~b1*cogapp+c1*wintense
pclimate ~ pclimate.mean*1
pclimate ~~ pclimate.var*pclimate
indirect :=(a1+a3*pclimate.mean)*(b1)
direct :=c1
total := direct + indirect
prop.mediated := indirect / total
indirect.below :=(a1+a3*(pclimate.mean-sqrt(pclimate.var)))*(b1)
indirect.above :=(a1+a3*(pclimate.mean+sqrt(pclimate.var)))*(b1)
direct.below:=c1
direct.above:=c1
total.below := direct.below + indirect.below
total.above := direct.above + indirect.above
prop.mediated.below := indirect.below / total.below
prop.mediated.above := indirect.above / total.above
semfit=sem(model=model,data=Paskvan,se="boot",bootstrap=10)
summary(semfit,ci=TRUE)
lavaan 0.6-3 ended normally after 26 iterations

  Optimization method                           NLMINB
  Number of free parameters                         16

  Number of observations                           803

  Estimator                                         ML
  Model Fit Test Statistic                    2031.202
  Degrees of freedom                                 4
  P-value (Chi-square)                           0.000

Parameter Estimates:

  Standard Errors                            Bootstrap
  Number of requested bootstrap draws               10
  Number of successful bootstrap draws              10

Regressions:
                   Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
  cogapp ~                                                              
    wintense  (a1)    0.685    0.063   10.950    0.000    0.613    0.796
    pclimate  (a2)    0.053    0.080    0.670    0.503   -0.062    0.211
    wntns:pcl (a3)   -0.066    0.020   -3.303    0.001   -0.110   -0.043
  emotion ~                                                             
    cogapp    (b1)    0.611    0.068    9.055    0.000    0.476    0.712
    wintense  (c1)    0.249    0.058    4.274    0.000    0.139    0.341

Covariances:
                   Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
  wintense ~~                                                           
    wintense:pclmt    1.709    0.132   12.985    0.000    1.516    1.966

Intercepts:
                   Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
    pclimat (pcl.)    2.783    0.019  146.068    0.000    2.771    2.828
   .cogapp            1.810    0.265    6.841    0.000    1.341    2.175
   .emotion           0.089    0.076    1.172    0.241    0.031    0.249
    wintens           3.500    0.017  207.625    0.000    3.439    3.503
    wntns:p           9.490    0.092  102.950    0.000    9.294    9.582

Variances:
                   Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
    pclimat (pcl.)    0.804    0.034   23.696    0.000    0.750    0.855
   .cogapp            0.385    0.021   17.912    0.000    0.356    0.427
   .emotion           0.992    0.053   18.595    0.000    0.925    1.136
    wintens           0.912    0.033   27.814    0.000    0.897    1.007
    wntns:p          12.814    0.463   27.700    0.000   11.990   13.716

Defined Parameters:
                   Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
    indirect          0.306    0.043    7.043    0.000    0.229    0.374
    direct            0.249    0.061    4.055    0.000    0.139    0.341
    total             0.555    0.029   18.856    0.000    0.490    0.571
    prop.mediated     0.551    0.098    5.602    0.000    0.401    0.725
    indirect.below    0.342    0.044    7.720    0.000    0.275    0.421
    indirect.above    0.270    0.045    6.055    0.000    0.182    0.327
    direct.below      0.249    0.061    4.055    0.000    0.139    0.341
    direct.above      0.249    0.061    4.055    0.000    0.139    0.341
    total.below       0.591    0.033   17.855    0.000    0.518    0.617
    total.above       0.519    0.028   18.229    0.000    0.462    0.546
    prop.medtd.blw    0.579    0.092    6.302    0.000    0.447    0.748
    prop.meditd.bv    0.520    0.106    4.881    0.000    0.348    0.698
statisticalDiagram(7,labels=labels,fit=semfit,whatLabel = "est",radx=0.12)

modmedSummaryTable(semfit,mod="pclimate")

Indirect Effect
(a1+a3*W)*(b1)

Direct Effect
c1

pclimate(W)

estimate

95% Bootstrap CI

p

estimate

95% Bootstrap CI

p

1.887

0.342

(0.275 to 0.421)

<.001

0.249

(0.139 to 0.341)

<.001

2.783

0.306

(0.229 to 0.374)

<.001

0.249

(0.139 to 0.341)

<.001

3.680

0.270

(0.182 to 0.327)

<.001

0.249

(0.139 to 0.341)

<.001

boot.ci.type = bca.simple

conditionalEffectPlot(semfit,data=Paskvan,mod="pclimate")