During the next 12 days, I will learn and repeat the basics
of structural equation modeling (SEM) using lavaan and semPlot packages in R.
You can search my lavaan posts by typing:
#UsmanZafarParacha_lavaan , and semPlot posts by typing:
#UsmanZafarParacha_semPlot
=============
During this day, lavaan and semPlot packages are loaded.
Then, an SEM model is defined. Following lines of codes can be used:
# Load packages
library(lavaan)
library(semPlot)
# Define the SEM model
model <- '
# Direct effect
retirement_planning
~ financial_literacy
# Indirect effects
financial_literacy ~
income_level
savings_behavior ~
income_level + financial_literacy
retirement_planning
~ savings_behavior
'
Then, a supposed data is prepared using the following lines
of codes:
# Simulate example data
set.seed(123)
data <- data.frame(
income_level =
rnorm(100, mean = 50000, sd = 15000), #
Income level
financial_literacy =
rnorm(100, mean = 60, sd = 10), #
Financial literacy score
savings_behavior =
rnorm(100, mean = 5000, sd = 2000), #
Savings behavior
retirement_planning
= rnorm(100, mean = 70, sd = 15) #
Retirement planning score
)
The SEM model is fit in the data, and eventually the SEM
model is visualized using the semPlot.
# Fit the SEM model
fit <- sem(model, data = data)
# View the summary of the model fit
summary(fit, standardized = TRUE, fit.measures = TRUE)
Source:
ChatGPT
No comments:
Post a Comment