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:
```{r}
install(qgraph)
# Create a new plot object for the updated plot with bootstrapped means
boot_cfa_plot <- initial_cfa_plot
#qgraph::qgraph(boot_cfa_plot)
# Filter boot_param_means to only include relationships (factor loadings)
boot_factor_loadings <- boot_param_means[grep("=~", names(boot_param_means))]
# Update the edge labels using these filtered factor loadings
for (i in 1:length(boot_factor_loadings)) {
boot_cfa_plot$Arguments$edge.labels[i] <- round(boot_factor_loadings[i], 2)
}
# Extracting edge labels from initial_cfa_plot
initial_labels <- initial_cfa_plot$Arguments$edge.labels
# Extracting edge labels from boot_cfa_plot
boot_labels <- boot_cfa_plot$Arguments$edge.labels
# Printing out the values for comparison
print("Initial CFA Plot Edge Labels:")
print(initial_labels)
print("Bootstrapped CFA Plot Edge Labels:")
print(boot_labels)
# Display the updated plot
qgraph::qgraph(boot_cfa_plot)