I have been using bootstrapLavaan for the first time, to perform confirmatory factor analysis. It runs fine and I have been able to summarise the CFA output. However, I have not been able to create a path diagram. Here is how I create the model:
install(lavaan)
# Fit initial CFA model using lavaan
initial_cfa_model <- lavaan::cfa(initial_cfa_model_syntax, data = cfa_data_rmv, missing = "fiml")
# Create summary
initial_cfa <- summary(initial_cfa_model, fit.measures=TRUE, standardized=TRUE, rsquare=TRUE)
# Custom function to extract measures from bootstrapLavaan
custom_extract <- function(lavaan_obj) {
# Extract fit measures
fit_measures <- lavaan::fitMeasures(lavaan_obj, fit.measures = c("chisq", "cfi", "tli", "rmsea", "srmr", "aic", "bic"))
# Extract coefficients
coefs <- lavaan::coef(lavaan_obj)
# Combine fit measures and coefficients
combined_results <- c(fit_measures, coefs)
# Return combined results
return(combined_results)
}
# Bootstrap the initial CFA model
boot_cfa_model <- lavaan::bootstrapLavaan(initial_cfa_model,
R = boot_iterations,
iseed = seed,
FUN = custom_extract
)
And here is how I successfully plot the initial CFA model:
install(semPaths)
# Plot the initial CFA model
initial_cfa_plot <- semPaths(initial_cfa_model,
whatLabels = "est",
edge.label.cex = 0.7,
layout = "tree",
intercepts = FALSE,
residuals = FALSE,
style = "lisrel",
curveAdjacent = TRUE,
rotation = 2)
initial_cfa_plot
But this won't work with the code below (primarily because bootstrapLavaan saves its output as a vector):
# Plot the boot CFA model
boot_cfa_plot <- semPaths(boot_cfa_model,
whatLabels = "est",
edge.label.cex = 0.7,
layout = "tree",
intercepts = FALSE,
residuals = FALSE,
style = "lisrel",
curveAdjacent = TRUE,
rotation = 2)
boot_cfa_plot
So, I'm not sure how to make this work and I've been at this for many hours now. I have to assume that there is a solution as such a plot is an expected output from a CFA.
Any help received with gratitude. Thanks!
After too many more hours on this, I found a solution to update the initial plot with the parameters from the bootstrapped model. To do this, the clue was to look closeley at the structure of the initial CFA plot
str(initial_cfa_plot, which then showed the label parameters that could be changed. Here is my code: