Sunday, October 27, 2024

Day 12: A challenge to learn basics of Structural Equation Modeling (SEM) using lavaan and semPlot packages in R

 

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, I learned and studied about Confirmatory Factor Analysis (CFA) while implementing a polychoric matrix and use the robust diagonally weighted least squares (RDWLS) method to evaluate the model fit using several fit indices such as RMSEA, CFI, TLI, SRMR, and chi-square/DF. This same thing can also be found in one of the research papers, such as that conducted Almeida et al. (2024).

Initially, I loaded the essential libraries and created supposed data using the following lines of codes:

 

library(lavaan)

library(semPlot)

library(psych)

 

set.seed(123)  # For reproducibility

n <- 200  # Number of participants

 

# Simulate ordinal data (Likert scale: 1 to 5)

item1 <- sample(1:5, n, replace = TRUE)

item2 <- sample(1:5, n, replace = TRUE)

item3 <- sample(1:5, n, replace = TRUE)

item4 <- sample(1:5, n, replace = TRUE)

item5 <- sample(1:5, n, replace = TRUE)

 

# Create a data frame

data <- data.frame(item1, item2, item3, item4, item5)

 

Then, using the psych package, the polychoric correlation matrix was determined:

 

polychoric_matrix <- polychoric(data)$rho

 

The CFA model was specified:

 

cfa_model <- '

  Factor1 =~ item1 + item2 + item3

  Factor2 =~ item4 + item5

'

 

fit <- cfa(cfa_model, sample.cov = polychoric_matrix, sample.nobs = n, estimator = "ML")

 

and the model fit was checked:

 

summary(fit, fit.measures = TRUE, standardized = TRUE)

 

Eventually, the CFA model was visualized:

 

semPaths(fit, what = "std", edge.label.cex = 1.2, layout = "tree", style = "lisrel", rotation = 2)

 

 

Sources:

Almeida, D. M., Santos-de-Araújo, A. D., Júnior, J. M. C. B., Cacere, M., Pontes-Silva, A., Costa, C. P., ... & Bassi-Dibai, D. (2024). The best internal structure of the Diabetes Quality of Life Measure (DQOL) in Brazilian patients. BMC Public Health24(1), 580.

ChatGPT


No comments: