if (FALSE) {
# Load data set "HolzingerSwineford1939" in the lavaan package
data("HolzingerSwineford1939", package = "lavaan")
# Run LCA with k = 1 to k = 6 classes
mplus.lca(HolzingerSwineford1939, ind = c("x1", "x2", "x3", "x4"),
run.mplus = TRUE)
# Example 1a: Read Mplus output files, create result table, write table, and save plots
result.lca(write = "LCA.xlsx", plot = TRUE)
# Example 1b: Write results into a text file
result.lca(write = "LCA.txt")
#-------------------------------------------------------------------------------
# Example 2: Draw bar chart manually
library(ggplot2)
# Collect LCA results
lca.result <- result.lca()
# Result table with means
means <- lca.result$result$mean
# Extract results from variance-covariance structure A with 4 latent classes
plotdat <- means[means$folder == "A_Invariant-Theta_Diagonal-Sigma" & means$nclass == 4, ]
# Draw bar chart
ggplot(plotdat, aes(ind, est, group = class, fill = class)) +
geom_bar(stat = "identity", position = "dodge", color = "black",
linewidth = 0.1) +
geom_errorbar(aes(ymin = low, ymax = upp), width = 0.23,
linewidth = 0.2, position = position_dodge(0.9)) +
scale_x_discrete("") +
scale_y_continuous("Mean Value", limits = c(0, 9),
breaks = seq(0, 9, by = 1)) +
labs(fill = "Latent Class") +
guides(fill = guide_legend(nrow = 1L)) +
theme(axis.title = element_text(size = 11),
axis.text = element_text(size = 11),
legend.position = "bottom",
legend.key.size = unit(0.5 , 'cm'),
legend.title = element_text(size = 11),
legend.text = element_text(size = 11),
legend.box.spacing = unit(-9L, "pt"))
# Save bar chart
ggsave("LCA_4-Class.png", dpi = 600, width = 6, height = 4)
}
Run the code above in your browser using DataLab