## ----include = FALSE---------------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ## ----Specify_1---------------------------------------------------------------- library(CLCM) N <- 2000 number.timepoints <- 2 item.type <- c('Ordinal', 'Nominal', 'Poisson', 'Neg_Binom', 'ZINB', 'ZIP', 'Normal', 'Beta') sim.categories.j <- c(4, 4, 30, 30, 30, 30, NA, NA) J <- length(item.type) item.names <- paste0('Item_', 1:J) Q <- matrix(1, nrow = length(item.type), ncol = 1, dimnames = list(paste0('Item_', 1:length(item.type)), NULL)) K <- ncol(Q) ## ----Specify_2---------------------------------------------------------------- set.seed(09092026) number.groups <- 2 dat <- data.frame( 'USUBJID' = rep(paste0('Subject_', formatC(1:N, width = 4, flag = '0')), length.out= N*number.timepoints), 'Group' = rep(paste0('Group_', 1:number.groups), length.out = N*number.timepoints), 'Time' = rep(paste0('Time_', 1:number.timepoints), each = N), stringsAsFactors=FALSE) # Design Matrix X <- model.matrix( ~ Group*Time, data = dat) # Beta Beta <- matrix(0, nrow = ncol(X), dimnames=list(colnames(X), 'param')) # Small separation between txa: Beta[grepl('Group_2:Time', rownames(Beta)), ] <- 1 Beta # Matrix multiply: XB <- X %*% Beta p <- exp(XB)/(1 + exp(XB)) lca <- matrix(runif(n = nrow(p)), nrow = nrow(p), ncol = 1) < p lca <- lca + 1 post <- matrix(0, nrow = nrow(dat), ncol = 2^K) post[ cbind(1:nrow(lca), lca) ] <- 1 # True posterior distributions - pass to simulation function ## ----Simulate----------------------------------------------------------------- set.seed(03082021) sim.dat <- simulate_clcm(N = N, number.timepoints = number.timepoints, Q = Q, item.names = item.names, item.type = item.type, categories.j = sim.categories.j, post = post) ## ----prep--------------------------------------------------------------------- dat.cov <- merge(x = dat, y = sim.dat$dat, by = c('USUBJID', 'Time')) ## ----Estimate----------------------------------------------------------------- mod <- clcm(dat = dat.cov, item.type = item.type, item.names = item.names, Q = Q, max.diff = 0.001, lat.reg = list('Time_1' = NULL, 'Time_2' = 'Group')) ## ----Results------------------------------------------------------------------ Beta # Generating parameter mod$lat.reg.param # estimate transition_matrix_clcm(mod = mod, stratification = TRUE, covariate = 'Group') ## ----Results_2---------------------------------------------------------------- lca.hat <- mod$dat$lca lca.true <- mod$dat$true_lca table(lca.true == lca.hat) xtabs( ~ lca.true + lca.hat)