Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Latent Class Choice Model (LCCM) Automation Script

This R script provides an automated code for estimating Latent Class Choice Models (LCCMs) using the apollo package.

It is designed to be "plug-and-play." You do not need to modify the mathematical engine or likelihood functions at the bottom of the script. Instead, you only need to update the Configuration section at the top of the script for any cross-sectional dataset.


Prerequisites

Before running this script, ensure you have R and RStudio installed, along with the following R packages. You can install them by running this code in your console:

install.packages(c("apollo", "tidyr", "openxlsx", "rstudioapi"))

How to Customize for Your Own Data

To use this script with new data, open the R script and look for the # Configuration section at the very top. Update the following variables to match your dataset.


1. Data and Output Files

  • DATA_PATH: The exact name of your dataset file (must be a CSV). E.g., "my_new_data.csv".
  • OUTPUT_FILE: The name you want to give the final Excel report. E.g., "My_Results.xlsx".

2. Core Choice Setup

  • INDIV_ID: The column name representing the unique ID for each person/respondent.
  • CHOICE_VAR: The column name containing the actual choice the person made.
  • FILTER_COL: Usually the same as CHOICE_VAR. The script removes any rows where this column is blank (missing data).
  • ALT_CODES: The numeric codes representing the available choices in your dataset. The last item in this list is automatically treated as your baseline/reference alternative (Utility = 0).
  • AVAIL_COLS: The columns in your dataset indicating if an alternative was available to the respondent (usually 1 for yes, 0 for no). The names here must perfectly match the names in ALT_CODES.

3. Number of Classes to Test

  • CLASS_MIN: The minimum number of hidden groups to test (usually 2).
  • CLASS_MAX: The maximum number of hidden groups to test. Note: Estimating 4+ classes can take a long time depending on your computer's speed.

4. Defining Your Variables

Variables are split into two lists.

CHOICE_VARS (Factors affecting the choice)

These are the variables that influence the actual decision. Add variables to this list using this format:

list(col = "column_name", name = "short_name", type = "specific", start_val = 0.0)
  • Use type = "generic" if the variable has the same impact across all alternatives within a class.
  • Use type = "specific" if the variable has a different impact on each alternative within a class.

MEMBERSHIP_VARS (Factors predicting the class)

These are variables (like age, income, or attitudes) that determine which hidden group a person belongs to. Add variables using this format:

list(col = "column_name", name = "short_name")

5. Other Settings

  • USE_SPLIT_DATA: Set to TRUE if you want to train the model on a portion of your data and test it on the rest to check predictive accuracy. Set to FALSE to use all data for estimation.
  • TRAIN_SPLIT_RATIO: If splitting data, the percentage used for training (e.g., 0.80 means 80% train, 20% test).
  • SEARCH_CANDS: The number of random starting points the model tests. Increasing this number (e.g., to 20) helps prevent the model from getting stuck in a "local maximum" but makes the script run slower.

Understanding the Outputs

Once the script finishes running, it will generate an Excel file (specified by your OUTPUT_FILE name) in the same folder as the script.

The workbook contains multiple sheets:

  • Overall_Performance: A summary table comparing the models (2-class vs 3-class, etc.) using Log-Likelihood, AIC, BIC, and Accuracy. It also includes confusion matrices showing how well the model predicted actual choices.
  • Marginal_Effects: A Sensitivity Analysis showing how changes in your variables impact the probability of each choice being selected. It automatically handles binary (0 vs 1), categorical, and continuous (1% increase) variables.
  • Individual Class Models: A dedicated sheet for every successful model (e.g., "2_Class_Model") detailing the estimated coefficients, standard errors, t-ratios, and p-values so you can see exactly which variables are statistically significant.

About

Automated Latent Class Choice Model (LCCM) with Apollo

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages