## ----include = FALSE---------------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ## ----Specify------------------------------------------------------------------ library(CLCM) library(ggplot2) N <- 500 number.timepoints <- 1 item.type <- c('Ordinal', 'Nominal', 'Poisson', 'Neg_Binom', 'ZINB', 'ZIP', 'Normal', 'Beta') sim.categories.j <- c(4, 4, 30, 30, 30, 30, NA, NA) lc.prop <- list('Time_1' = c(0.5, 0.5), 'Time_2' = c(0.5, 0.5) ) Q <- matrix(1, nrow = length(item.type), ncol = 1, dimnames = list(paste0('Item_', 1:length(item.type)), 'F1')) Q ## ----Generate----------------------------------------------------------------- set.seed(03102021) sim.dat <- simulate_clcm(N = N, Q = Q, number.timepoints = number.timepoints, item.type = item.type, categories.j = sim.categories.j, lc.prop = lc.prop) ## ----Estimate----------------------------------------------------------------- mod <- clcm(dat = sim.dat$dat, item.type = sim.dat$item.type, item.names = sim.dat$item.names, Q = sim.dat$Q) ## ----Fit---------------------------------------------------------------------- aic_bic_clcm(mod = mod) ## ----Results------------------------------------------------------------------ lca.hat <- mod$dat$lca lca.true <- mod$dat$true_lca prop.table(table(lca.true == lca.hat)) xtabs( ~ lca.true + lca.hat) ## ----Plot Results, out.width="80%"-------------------------------------------- library(ggplot2) dat.plot <- mod$dat dat.plot$lca <- factor(dat.plot$lca, levels = sort(unique(dat.plot$lca)), labels = paste0('Class ', sort(unique(dat.plot$lca)) )) item.names <- mod$item.names for(j in 1:length(item.names) ){ if(item.type[j] %in% c('Ordinal', 'Nominal')){ pp <- ggplot(data = dat.plot, aes(fill=lca, x= as.factor(get(item.names[j])) )) + geom_bar(aes(y = after_stat(count / sum(count))), color="#e9ecef", alpha=0.6, position="dodge", stat="count") + scale_y_continuous(labels=scales::percent_format(accuracy = 1)) + scale_fill_manual(name = 'Latent Classes', values=c("blue2", "red2")) + theme_minimal() + theme(legend.position = 'bottom') + labs(x = 'Item Responses', y = 'Percentage', title = paste0('Item Name: ', item.names[j]), subtitle = paste0('Item Type: ', item.type[j]), caption = 'Note: Simulated data') suppressMessages(plot(pp)) }# Ordinal if(item.type[j] %in% c('Poisson', 'Neg_Binom', 'ZINB', 'ZIP', 'Normal', 'Beta')){ pp <- ggplot(data = dat.plot, aes(x = get(item.names[j]), group = lca, fill = lca)) + geom_histogram(aes(y = after_stat(count / sum(count))), color="#e9ecef", alpha=0.6, position = 'identity') + scale_y_continuous(labels=scales::percent_format(accuracy = 1)) + scale_fill_manual(name = 'Latent Classes', values=c("blue2", "red2")) + theme_minimal() + theme(legend.position = 'bottom') + labs(x = 'Item Responses', y = 'Percentage', title = paste0('Item Name: ', item.names[j]), subtitle = paste0('Item Type: ', item.type[j]), caption = 'Note: Simulated data') suppressMessages(plot(pp)) }# Count items }# end loop over items