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

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

colnames(mediation.data) <- paste0("V", 1:4)

mediation.model <- '

 V3 ~ a*V1
 V2 ~ b*V1
 V4 ~ c*V1 + d*V2 + e*V3
 V2 ~~ V3
 
 # Indirect Effects
 ae := a*e
 bd := b*d
 
 # Total Effect
 t := a*e + b*d + c

'

mediation.fit <- sem(mediation.model, data = mediation.data, se = "bootstrap")
library(lavaan)
summary(mediation.fit, fit.measures = T, standardized = F)
## lavaan 0.6.9 ended normally after 48 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         9
## 
##   Number of observations                          1000
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Model Test Baseline Model:
## 
##   Test statistic                               589.591
##   Degrees of freedom                                 6
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    1.000
##   Tucker-Lewis Index (TLI)                       1.000
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -8059.340
##   Loglikelihood unrestricted model (H1)      -8059.340
##                                                       
##   Akaike (AIC)                               16136.680
##   Bayesian (BIC)                             16180.849
##   Sample-size adjusted Bayesian (SABIC)      16152.265
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.000
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.000
##   P-value H_0: RMSEA <= 0.050                       NA
##   P-value H_0: RMSEA >= 0.080                       NA
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           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|)
##   V3 ~                                                
##     V1         (a)   -0.158    0.029   -5.531    0.000
##   V2 ~                                                
##     V1         (b)    0.149    0.027    5.478    0.000
##   V4 ~                                                
##     V1         (c)   13.457    0.666   20.203    0.000
##     V2         (d)    8.804    0.727   12.106    0.000
##     V3         (e)    0.046    0.718    0.065    0.948
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##  .V3 ~~                                               
##    .V2                0.244    0.049    4.998    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .V3                1.573    0.059   26.629    0.000
##    .V2                1.583    0.058   27.135    0.000
##    .V4              826.683   35.926   23.010    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     ae               -0.007    0.116   -0.063    0.950
##     bd                1.308    0.262    4.995    0.000
##     t                14.758    0.685   21.554    0.000
summary(mediation.fit, fit.measures = T, standardized = T)
## lavaan 0.6.9 ended normally after 48 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         9
## 
##   Number of observations                          1000
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Model Test Baseline Model:
## 
##   Test statistic                               589.591
##   Degrees of freedom                                 6
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    1.000
##   Tucker-Lewis Index (TLI)                       1.000
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -8059.340
##   Loglikelihood unrestricted model (H1)      -8059.340
##                                                       
##   Akaike (AIC)                               16136.680
##   Bayesian (BIC)                             16180.849
##   Sample-size adjusted Bayesian (SABIC)      16152.265
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.000
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.000
##   P-value H_0: RMSEA <= 0.050                       NA
##   P-value H_0: RMSEA >= 0.080                       NA
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           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|)   Std.lv  Std.all
##   V3 ~                                                                  
##     V1         (a)   -0.158    0.029   -5.531    0.000   -0.158   -0.173
##   V2 ~                                                                  
##     V1         (b)    0.149    0.027    5.478    0.000    0.149    0.163
##   V4 ~                                                                  
##     V1         (c)   13.457    0.666   20.203    0.000   13.457    0.507
##     V2         (d)    8.804    0.727   12.106    0.000    8.804    0.303
##     V3         (e)    0.046    0.718    0.065    0.948    0.046    0.002
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .V3 ~~                                                                 
##    .V2                0.244    0.049    4.998    0.000    0.244    0.155
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .V3                1.573    0.059   26.629    0.000    1.573    0.970
##    .V2                1.583    0.058   27.135    0.000    1.583    0.974
##    .V4              826.683   35.926   23.010    0.000  826.683    0.602
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##     ae               -0.007    0.116   -0.063    0.950   -0.007   -0.000
##     bd                1.308    0.262    4.995    0.000    1.308    0.049
##     t                14.758    0.685   21.554    0.000   14.758    0.556

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