The R-Code provided below is the brief introduction into estimating a mediated path model with Lavaan (Rosseel, 2012; Code: R-Code; Data: Data). The results are presented in the figure below, which was created using lucidchart.com. If you have any questions about the R-Code please email me!

## Written by Ian A. Silver
## Written on 11.4.2020
## Estimating a three variable path model with Lavaan
# Getting Started ####
library(pastecs)
library(lavaan)
options(scipen=100, digits=6)
options(max.print = 500000000)
# Loading Data (THE DIRECTORY SHOULD CHANGE) ####
## You can find the directory for the file by right clicking on the file and clicking on the properties option
A<-read.csv("C:/Desktop/Mediation Path Data.csv") #Personal Directory should be placed for the data file (make sure you use "/")
# Estimating A Mediated Path Model With Lavaan ####
stat.desc(A) # Provides descriptive statistics for all of the variables in the dataframe
#Creating the formula for the Lavaan Model
F<-'
# First Equation Z (mediator) regressed on X (exogenous variable)
Z~a*X # "a*" informs Lavaan to save the estimate as the letter "a"
# Second equation Y (endogenous variable) regressed on Z (mediator) and X (exogenous variable)
Y~b*Z+c*X # "b*" informs Lavaan to save the estimate as the letter "b"; "c*" informs Lavaan to save the estimate as the letter "c"
# Calculating the Indirect effects (the path from X to Z multiplied by the path from Z to Y)
ab := a*b # SE, p-values, and CI are calculated using the delta method (Described in Sobel 1982)
# Calculating the total effects(the path from X to Y plus the path from X to Z multiplied by the path from Z to Y)
abc := c+(a*b) # SE, p-values, and CI are calculated using the delta method (Described in Sobel 1982)
'
# Estimating the Model
M<-sem(F, data = A, estimator = "ML")
# Summary of the Results
## The summary provides: Global fit statistics, Regression Estimates, residual variances, direct and indirect estimates, and standardized estimates
print(summary(M, standardized = TRUE, ci = TRUE))
# Residuals
print(resid(M, type = "cor"))
# Modification indices
print(modindices(M)) # produced in a manner similar to MPLUS
## Citation
# Sobel M. E. (1982). Asymptotic confidence intervals for indirect effects in structural equation models. Sociological Methodology 13, 290-312.
License: Creative Commons Attribution 4.0 International (CC By 4.0)