# install.packages("slca") # install.packages("webshot") library("slca") library("magrittr") library("webshot") # Model specification of slca slca(L1[3] ~ x1 + y1 + z1, L2[3] ~ x2 + y2 + z2, L3[3] ~ x3 + y3 + z3, P[3] ~ L1 + L2 + L3, constraints = c("L1", "L2", "L3")) slca(L1[3] ~ x1 + y1 + z1, L2[3] ~ x2 + y2 + z2, L3[3] ~ x3 + y3 + z3, L1 ~ L2, L2 ~ L3, constraints = c("L1 ~ L2", "L2 -> L3")) # examples for model construction lta1 <- slca(L1[3] ~ x1 + y1 + z1, L2[3] ~ x2 + y2 + z2, L3[3] ~ x3 + y3 + z3, L1 ~ L2, L2 ~ L3, constraints = c("L1", "L2", "L3")) lta2 <- slca(LG[2] ~ x1 + x2 + x3, LC[3] ~ y1 + y2 + y3 + y4, LG ~ LC) lta1 lta2 ## Figure 1 plot(lta1); plot(lta2) jlca <- slca(L1[3] ~ x1 + x2 + x3, L2[2] ~ y1 + y2 + y3, L3[4] ~ z1 + z2 + z3, JC[3] ~ L1 + L2 + L3) lcpa <- slca(L1[3] ~ x1 + y1 + z1, L2[3] ~ x2 + y2 + z2, L3[3] ~ x3 + y3 + z3, PF[3] ~ L1 + L2 + L3, constraints = c("L1", "L2", "L3")) jlca lcpa ## Figure 2 plot(jlca); plot(lcpa) ## Figure 3 model <- slca(L1[3] ~ x1 + x2 + x3, L2[4] ~ y1 + y2 + y3, L3[2] ~ z1 + z2 + z3, G1[3] ~ g1 + g2 + g3, JC[3] ~ L1 + L2 + L3, G1 ~ JC) plot(model) # Using pipeline operator model <- slca(status(3) ~ PAPRES + PADEG + MADEG) estimate(model, data = gss7677) slca(status(3) ~ PAPRES + PADEG + MADEG) %>% estimate(data = gss7677) # Example 1 (gss7677 data) set.seed(33) ## LC model for tolerance to minority (TOL) m2 <- slca(TOL(2) ~ TOLRAC + TOLCOM + TOLHOMO + TOLATH + TOLMIL) %>% estimate(data = gss7677) m3 <- slca(TOL(3) ~ TOLRAC + TOLCOM + TOLHOMO + TOLATH + TOLMIL) %>% estimate(data = gss7677) m4 <- slca(TOL(4) ~ TOLRAC + TOLCOM + TOLHOMO + TOLATH + TOLMIL) %>% estimate(data = gss7677) m5 <- slca(TOL(5) ~ TOLRAC + TOLCOM + TOLHOMO + TOLATH + TOLMIL) %>% estimate(data = gss7677, control = list(em.iterlim = 10000)) ## model selection including bootstrap test (takes long time) gof(m2, m3, m4, m5, test = "boot") compare(m4, m5, test = "boot") param(m4) ## regression on covariates m4 %>% regress(TOL ~ PADEG, data = gss7677) m4 %>% regress(TOL ~ MADEG, data = gss7677) m4 %>% regress(TOL ~ PAPRES, data = gss7677) ## LC model for social status (STATUS) set.seed(2) m3 <- slca(STATUS(3) ~ PAPRES + PADEG + MADEG) %>% estimate(data = gss7677) ## print parameters param(m3) ## latent transition analysis with TOL and STATUS set.seed(6) lta <- slca( STATUS(3) ~ PAPRES + PADEG + MADEG, TOL(4) ~ TOLRAC + TOLCOM + TOLHOMO + TOLATH + TOLMIL, STATUS ~ TOL ) %>% estimate(data = gss7677) param(lta) # Example 2 (addhealth data) set.seed(0) ## LC model for adolescents depression (DEP1, DEP2) lta4 <- slca( DEP1(4) ~ S1w1 + S2w1 + S3w1 + S4w1 + D1w1 + D2w1 + F1w1 + F2w1, DEP2(4) ~ S1w2 + S2w2 + S3w2 + S4w2 + D1w2 + D2w2 + F1w2 + F2w2, DEP1 ~ DEP2 ) %>% estimate(data = addhealth) lta5 <- slca( DEP1(5) ~ S1w1 + S2w1 + S3w1 + S4w1 + D1w1 + D2w1 + F1w1 + F2w1, DEP2(5) ~ S1w2 + S2w2 + S3w2 + S4w2 + D1w2 + D2w2 + F1w2 + F2w2, DEP1 ~ DEP2 ) %>% estimate(data = addhealth) lta6 <- slca( DEP1(6) ~ S1w1 + S2w1 + S3w1 + S4w1 + D1w1 + D2w1 + F1w1 + F2w1, DEP2(6) ~ S1w2 + S2w2 + S3w2 + S4w2 + D1w2 + D2w2 + F1w2 + F2w2, DEP1 ~ DEP2 ) %>% estimate(data = addhealth) ## model selection gof(lta4, lta5, lta6) ## test for measurement invariance assumption lta5inv <- slca( DEP1(5) ~ S1w1 + S2w1 + S3w1 + S4w1 + D1w1 + D2w1 + F1w1 + F2w1, DEP2(5) ~ S1w2 + S2w2 + S3w2 + S4w2 + D1w2 + D2w2 + F1w2 + F2w2, DEP1 ~ DEP2, constraints = c("DEP1", "DEP2") ) %>% estimate(data = addhealth) compare(lta5, lta5inv, test = "chisq") ## print parameters param(lta5inv) ## reorder latent classes and restrict paremeters to zero lta5sort <- lta5inv %>% reorder(DEP1 = c(2, 1, 5, 3, 4)) param(lta5sort) lta5sort %>% param(index = TRUE) lta5fix <- lta5sort %>% estimate(fix2zero = c(13, 17)) ## test for parameter constraints (fix to zero) compare(lta5inv, lta5fix, test = "chisq") # Example 3 (nlsy97 data) set.seed(0) names(nlsy97) ## joint latent class profile model for substance use jlcpa <- slca( # Smoking behavior at each time point SMK_98(3) ~ ESMK_98 + FSMK_98 + DSMK_98 + HSMK_98, SMK_03(3) ~ ESMK_03 + FSMK_03 + DSMK_03 + HSMK_03, SMK_08(3) ~ ESMK_08 + FSMK_08 + DSMK_08 + HSMK_08, # Drinking behavior at each time point DRK_98(3) ~ EDRK_98 + CDRK_98 + WDRK_98 + BDRK_98, DRK_03(3) ~ EDRK_03 + CDRK_03 + WDRK_03 + BDRK_03, DRK_08(3) ~ EDRK_08 + CDRK_08 + WDRK_08 + BDRK_08, # Marijuana use behavior at each time point MRJ_98(3) ~ EMRJ_98 + CMRJ_98 + OMRJ_98 + SMRJ_98, MRJ_03(3) ~ EMRJ_03 + CMRJ_03 + OMRJ_03 + SMRJ_03, MRJ_08(3) ~ EMRJ_08 + CMRJ_08 + OMRJ_08 + SMRJ_08, # Overall substance use behavior at each time point SUB_98(5) ~ SMK_98 + DRK_98 + MRJ_98, SUB_03(5) ~ SMK_03 + DRK_03 + MRJ_03, SUB_08(5) ~ SMK_08 + DRK_08 + MRJ_08, # Longitudinal shifts of overall substance use behavior PROF(4) ~ SUB_98 + SUB_03 + SUB_08, # Measurement invariance of latent class variables constraints = list( c("SMK_98", "SMK_03", "SMK_08"), c("DRK_98", "DRK_03", "DRK_08"), c("MRJ_98", "MRJ_03", "MRJ_08"), c("SUB_98 ~ SMK_98", "SUB_03 ~ SMK_03", "SUB_08 ~ SMK_08"), c("SUB_98 ~ DRK_98", "SUB_03 ~ DRK_03", "SUB_08 ~ DRK_08"), c("SUB_98 ~ MRJ_98", "SUB_03 ~ MRJ_03", "SUB_08 ~ MRJ_08") ) ) jlcpa jlcpa_fit <- estimate(jlcpa, data = nlsy97) ## reorder and print parameters (to better description) ## this is omitted in manuscript jlcpa_fit <- jlcpa_fit %>% reorder( SMK_98 = c(1, 3, 2), DRK_98 = c(3, 2, 1), MRJ_98 = c(3, 1, 2), SUB_98 = c(3, 4, 5, 1, 2), PROF = c(4, 1, 3, 2) ) param(jlcpa_fit) ## Figure 4 plot(jlcpa, dir = "LR")