The LCAKB’s Code Repository is designed to be a “one-stop shop” to download sample code for latent class models. Many of the code examples come from projects and workshops conducted by Drs. Bethany Bray, John Dziak, and Stephanie Lanza when they were investigators at The Methodology Center at Penn State and supported in part by National Institute on Drug Abuse Center of Excellence awards from 1996-2021 (P50 DA039838 and P50 DA010075). In addition, many of the code examples come from the work of their collaborators and trainees, including those supported by the Prevention and Methodology Training Program, a National Institute on Drug Abuse Training Program (T32 DA017629).
Below you will find a list of all available models and code “snippets.” You can use the filters on the sidebar to narrow down the models for which you are looking. The LCAKB Code Repository is under active development and is currently being expanded. New models and code snippets will be published soon. Please sign up to our mailing list below to be informed of when they are published. If you would like to contribute a piece of code to help your fellow researchers, please email Dr. Bethany Bray at bcbray@latentclassanalysis.com.
All Models
LCA: Adding outcomes using an adjusted 3-step approach (automated, BCH)
Description This code adds a binary and a continuous outcome to the 4-class baseline LCA model with all binary indicators from Exercise 1 in the linked page. The binary outcome is political beliefs (not conservative vs. conservative) and the continuous outcome is the number of evenings out per week (average 0-7). This code uses the automated 3-step BCH approach as we explore the association between latent class membership and each outcome. Software Downloads Mplus Exercises Exercise 1 This exercise asks you to fit a 4-class, latent-class model for marijuana use and attitudes using 7 binary indicators of the latent class...
LCA: Adding outcomes using an adjusted 3-step approach (manual, BCH)
Description This code adds a binary and a continuous outcome to the 4-class baseline LCA model with all binary indicators from Exercise 1 in the linked page. The binary outcome is political beliefs (not conservative vs. conservative) and the continuous outcome is the number of evenings out per week (average 0-7). This code uses the the manual 3-step BCH approach as we explore the association between latent class membership and each outcome. Software Downloads Mplus Exercises Exercise 1 This exercise asks you to fit a 4-class, latent-class model for marijuana use and attitudes using 7 binary indicators of the latent...
LCA: Baseline LCA with 3+ level categorical indicators
This code fits a longitudinal latent class model, using categorical indicators with 3+ levels, to identify latent classes indicated by multidimensional experiences of racism and heterosexism during the transition to adulthood among sexual minority men of color.
LCA: Baseline LCA with all binary indicators
This code fits a 4-class, baseline, latent-class model for marijuana use and attitudes using 7 binary indicators of the latent class variable. This code also plots the item-response probabilities using a line graph.
LCA: Latent class moderation
This code demonstrates how to use a latent class moderator to examine heterogeneity in intervention effects among adolescents receiving treatment for cannabis use. First, the code identifies latent classes of contextual and individual risk at baseline using LCA. Then, it uses an adjusted 3-step approach with BCH weights to regress the outcomes on level of care, latent class membership, the interaction between them, and covariates.
LCA: LCA with a covariate (1-step approach)
This code fits a 4-class, latent-class model for marijuana use and attitudes using a model-based approach (1-step approach). It includes a covariate for grades in the model.
LCA: LCA with a covariate and a grouping variable (1-step approach)
This code fits a 4-class, latent-class model for marijuana use and attitudes using a model-based approach (1-step approach). It includes a covariate for grades and a grouping variable for year in the model.
LCA: LCA with a grouping variable and measurement invariance
This code fits a 4-class, latent-class model for marijuana use and attitudes using 7 binary indicators of the latent class variable. It includes a grouping variable for year, and observations came from 3 different years.
LCA: LCA with a grouping variable and without measurement variance
This code fits a 4-class, latent-class model for marijuana use and attitudes using 7 binary indicators of the latent class variable. It includes a grouping variable for year, and observations came from 3 different years. Measurement invariance across groups is not imposed resulting in an unrestricted latent class model with multiple groups.
LPA: Baseline LPA with all continuous indicators
This code fits a 5-class, baseline, latent-profile model for the “Big 5” personality traits using 5 continuous indicators of the latent class variable.
LPA: Baseline LPA with all continuous indicators and a covariate
This code fits a baseline, latent-profile model for the “Big 5” personality traits using 5 continuous indicators of the latent class variable and biological sex as a covariate.
LPA: Baseline LPA with all continuous indicators and a grouping variable with measurement invariance
This code fits a baseline, latent-profile model for the “Big 5” personality traits using 5 continuous indicators of the latent class variable and biological sex as the grouping variable. It also imposes measurement invariance across the groups.
LPA: Baseline LPA with continuous and categorical indicators (mixed indicator model)
This code fits a mixed indicator latent-profile model (using both continuous and categorical indicators) to identify family subgroups that conform to risk factors associated with adolescent antisocial behavior.
LPA: LPA with a grouping variable with measurement invariance across means and variances
Description This code fits a baseline, latent-profile model to identify and describe profiles of financial stress responses. It also imposes measurement invariance across the groups with means and variances equal. This code corresponds to the research paper titled “Financial stress response profiles and psychosocial functioning in low-income parents” published in Journal of Family Psychology in 2018. The paper can be found here: https://pubmed.ncbi.nlm.nih.gov/29878812/ Software Downloads Mplus Model Features Model Category Your Content Goes Here Model Type Your Content Goes Here Indicator Type Your Content Goes Here Software Options Your Content Goes Here Measurement Invariance Your Content Goes Here Approach to...
LPA: LPA with a grouping variable without measurement invariance
Description This code fits a baseline, latent-profile model to identify and describe profiles of financial stress responses. It doesn’t impose measurement invariance across the groups. This model is similar to the model in the research paper titled “Financial stress response profiles and psychosocial functioning in low-income parents” published in Journal of Family Psychology in 2018. One key difference is that this model DOES NOT impose measurement invariance while the model in the paper DOES impose measurement invariance. The paper can be found here: https://pubmed.ncbi.nlm.nih.gov/29878812/ The code for the model in the paper (i.e. with measurement invariance) can be found here....
LTA: Baseline Latent Transition Analysis with Categorical Indicators
This code fits a 4-class, latent-class model for marijuana use and attitudes using 7 binary indicators of the latent class variable. It includes a grouping variable for year, and observations came from 3 different years. Measurement invariance across groups is imposed such that analogous item-response probabilities within classes are restricted to be equal to each other across groups.
LTA: Baseline LTA with 2 times, all binary indicators, and measurement invariance
This code fits a 2-time, 5-class, latent-transition model for delinquency over time using 6 binary indicators of the latent class variable. Measurement invariance across time is imposed such that analogous item-response probabilities within classes are restricted to be equal to each other across times.
MLCA: Marginal approach with covariates at day-level and person-level
Description
This model estimates six day-level latent classes of substance use consequences using a marginal modeling approach to account for the nested data structure. One one day-level covariate (daily positive affect) and person-level covariate (student is under legal drinking age) are included to predict consequence latent class membership.
Software Downloads
Model Features
Model Category
Your Content Goes Here
Model Type
Your Content Goes Here
Indicator Type
Your Content Goes Here
Software Options
Your Content Goes Here
Measurement Invariance
Your Content Goes Here
Approach to Outcomes
Contributors
Your Content Goes Here
MLCA: Marginal approach without covariates
Description
This model estimates six day-level latent classes of substance use consequences using a marginal modeling approach to account for the nested data structure.
Software Downloads
Model Features
Model Category
Your Content Goes Here
Model Type
Your Content Goes Here
Indicator Type
Your Content Goes Here
Software Options
Your Content Goes Here
Measurement Invariance
Your Content Goes Here
Approach to Outcomes
Contributors
Your Content Goes Here
MLCA: Non-parametric approach with covariates at the day-level and person-level
Description
This code fits a 2-level latent-class model with covariates using a “non-parametric approach.” This model simultaneously estimates day-level classes of substance-related consequences and person-level classes that group individuals based on proportion of each type of day. This model includes covariates at each level to estimate associations with both the day-level and person-level classes.
Software Downloads
Model Features
Model Category
Your Content Goes Here
Model Type
Your Content Goes Here
Indicator Type
Your Content Goes Here
Software Options
Your Content Goes Here
Measurement Invariance
Your Content Goes Here
Approach to Outcomes
Contributors
Your Content Goes Here
MLCA: Non-parametric approach without covariates
Description
This code fits a 2-level latent-class model without covariates using a “non-parametric approach.” This model simultaneously estimates day-level classes of substance-related consequences and person-level classes that group individuals based on proportion of each type of day.
Software Downloads
Model Features
Model Category
Your Content Goes Here
Model Type
Your Content Goes Here
Indicator Type
Your Content Goes Here
Software Options
Your Content Goes Here
Measurement Invariance
Your Content Goes Here
Approach to Outcomes
Contributors
Your Content Goes Here
Multilevel LPA: Baseline two-level LPA with classes at level 1 and level 2
This code fits a 2-level latent-profile model using a “non-parametric approach” to identify mother-father-adolescent relationship structures and dynamics on a daily basis.
Shiny app for 2-level MLCA output
These web apps use R Shiny to make it easier to understand output from complex models, such as multilevel latent class analysis.
Let’s stay in touch.
We are in this together. Receive an email whenever a new model or resource is added to the Knowledge Base website.