library(lavaan)
setwd(mypath)  # change it to the path of your own data folder

mediation.data <- read.table("Mediation_Exercise_data.csv", sep = ",", header = F)

colnames(mediation.data) <- c("climate",
                           "control",
                           "burnout",
                           "attitude",
                           "educout",
                           "psychout")

mediation.model <- '

 burnout ~ a*climate + b*control
 attitude ~ c*burnout + d*climate + e*control
 educout ~ f*attitude
 psychout ~ g*attitude
 
 educout ~~ psychout
 
 ac := a*c
 bc := b*c
 fd := f*d
 fe := f*e
 fc := f*c
 gd := g*d
 ge := g*e
 gc := g*c

'

mediation.fit <- sem(mediation.model, data = mediation.data, se = "bootstrap")
summary(mediation.fit, fit.measures = T, standardized = T)
## lavaan 0.6.5 ended normally after 42 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                        12
## 
##   Number of observations                           109
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 3.885
##   Degrees of freedom                                 6
##   P-value (Chi-square)                           0.692
## 
## Model Test Baseline Model:
## 
##   Test statistic                               192.120
##   Degrees of freedom                                14
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    1.000
##   Tucker-Lewis Index (TLI)                       1.028
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -1101.180
##   Loglikelihood unrestricted model (H1)      -1099.238
##                                                       
##   Akaike (AIC)                                2226.361
##   Bayesian (BIC)                              2258.657
##   Sample-size adjusted Bayesian (SABIC)       2220.739
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.000
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.096
##   P-value H_0: RMSEA <= 0.050                    0.809
##   P-value H_0: RMSEA >= 0.080                    0.088
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.032
## 
## 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|)   Std.lv  Std.all
##   burnout ~                                                             
##     climate    (a)   -0.380    0.082   -4.630    0.000   -0.380   -0.409
##     control    (b)    0.292    0.091    3.220    0.001    0.292    0.249
##   attitude ~                                                            
##     burnout    (c)    0.025    0.011    2.264    0.024    0.025    0.232
##     climate    (d)    0.015    0.009    1.591    0.112    0.015    0.150
##     control    (e)   -0.056    0.012   -4.683    0.000   -0.056   -0.443
##   educout ~                                                             
##     attitude   (f)    2.215    0.290    7.645    0.000    2.215    0.633
##   psychout ~                                                            
##     attitude   (g)    3.447    0.319   10.795    0.000    3.447    0.691
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .educout ~~                                                            
##    .psychout          1.139    0.983    1.159    0.246    1.139    0.105
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .burnout          67.858    8.124    8.353    0.000   67.858    0.719
##    .attitude          0.886    0.106    8.395    0.000    0.886    0.799
##    .educout           8.166    1.018    8.021    0.000    8.166    0.600
##    .psychout         14.411    1.781    8.092    0.000   14.411    0.522
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##     ac               -0.010    0.005   -2.021    0.043   -0.010   -0.095
##     bc                0.007    0.004    1.753    0.080    0.007    0.058
##     fd                0.033    0.022    1.522    0.128    0.033    0.095
##     fe               -0.125    0.032   -3.893    0.000   -0.125   -0.281
##     fc                0.056    0.026    2.116    0.034    0.056    0.147
##     gd                0.052    0.033    1.572    0.116    0.052    0.103
##     ge               -0.195    0.044   -4.449    0.000   -0.195   -0.307
##     gc                0.087    0.037    2.345    0.019    0.087    0.160

© Copyright 2024 @Yi Feng and @Gregory R. Hancock.