library(lavaan)
## This is lavaan 0.6-17
## lavaan is FREE software! Please report any bugs.
library(lavaanPlot)
## Warning: package 'lavaanPlot' was built under R version 4.3.3

Built data

set.seed(123)
X<-rnorm(100)
M<-0.6*X + rnorm(100)
Y<-0.8*M+rnorm(100)
data<-data.frame(X,M,Y)
head(data)
##             X          M          Y
## 1 -0.56047565 -1.0466920  1.3614568
## 2 -0.23017749  0.1187772  1.4074347
## 3  1.55870831  0.6885331  0.2856814
## 4  0.07050839 -0.3052376  0.2990040
## 5  0.12928774 -0.8740459 -1.1135767
## 6  1.71506499  0.9840113  0.3109621

model

model<-"#Direct effect of X on Y
Y~X_to_Y*X
#effect of X on M
M~X_to_M*X
#effect of M on Y
Y~M_to_Y*M
# LABEL EFFECTS 
Direct:=X_to_Y
Indirect:=X_to_M*M_to_Y
Total effect :=Direct+Indirect
Prop_mediated:=Direct/Indirect"

fit the model

fit<-sem(model=model,data=data,se="bootstrap")

summary of the model

summary(fit)
## lavaan 0.6.17 ended normally after 1 iteration
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         5
## 
##   Number of observations                           100
## 
## 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|)
##   Y ~                                                 
##     X       (X__Y)   -0.147    0.124   -1.187    0.235
##   M ~                                                 
##     X       (X__M)    0.548    0.105    5.223    0.000
##   Y ~                                                 
##     M       (M__Y)    0.824    0.103    7.990    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .Y                 0.878    0.112    7.826    0.000
##    .M                 0.923    0.146    6.321    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     Direct           -0.147    0.124   -1.187    0.235
##     Indirect          0.451    0.099    4.573    0.000
##     Totaleffect       0.304    0.157    1.931    0.053
##     Prop_mediated    -0.327    0.321   -1.018    0.309

parameter estimates(i.e confidence interval e.t.c)

parameterEstimates(fit)
##              lhs op             rhs         label    est    se      z pvalue
## 1              Y  ~               X        X_to_Y -0.147 0.124 -1.187  0.235
## 2              M  ~               X        X_to_M  0.548 0.105  5.223  0.000
## 3              Y  ~               M        M_to_Y  0.824 0.103  7.990  0.000
## 4              Y ~~               Y                0.878 0.112  7.826  0.000
## 5              M ~~               M                0.923 0.146  6.321  0.000
## 6              X ~~               X                0.825 0.000     NA     NA
## 7         Direct :=          X_to_Y        Direct -0.147 0.124 -1.187  0.235
## 8       Indirect :=   X_to_M*M_to_Y      Indirect  0.451 0.099  4.573  0.000
## 9    Totaleffect := Direct+Indirect   Totaleffect  0.304 0.157  1.931  0.053
## 10 Prop_mediated := Direct/Indirect Prop_mediated -0.327 0.321 -1.018  0.309
##    ci.lower ci.upper
## 1    -0.386    0.113
## 2     0.330    0.733
## 3     0.620    1.026
## 4     0.641    1.072
## 5     0.631    1.211
## 6     0.825    0.825
## 7    -0.386    0.113
## 8     0.259    0.642
## 9    -0.006    0.613
## 10   -1.015    0.267
lavaanPlot(model = fit, coefs = TRUE, stand = TRUE, sig = 0.05) #standardized regression paths, showing only paths with p<= .05