Page 1 of 1

Identify respondents in Latent class logit model

Posted: 14 Jul 2023, 17:25
by qcng
Dear Prof. Hess, Prof. Palma

I have run LC logit model with two classes a and b. I would like to identify which respondents belong to class a and b.
There are 4 non-price attributes (barn, range, organic, vitamin) and price attribute.
Below is my code

Code: Select all

# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS                       ####
# ################################################################# #

### Clear memory
rm(list = ls())

### Load Apollo library
library(apollo)
library(openxlsx)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName       = "LC_no_covariates",
  modelDescr      = "Simple LC model, no covariates in class allocation model",
  indivID         = "ID",
  nCores          = 2,
  outputDirectory = "output"
)

# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS                     ####
# ################################################################# #

dtaApollo <- read.xlsx("dtaApollo.xlsx", sheet = "data", colNames = T)

database <- dtaApollo


# ################################################################# #
#### DEFINE MODEL PARAMETERS                                     ####
# ################################################################# #

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(asc_alt3     = 0,
                
                beta_barn_a = 0,
                beta_barn_b = 0,
                
                beta_range_a = 0,
                beta_range_b = 0,
                
                beta_organic_a = 0,
                beta_organic_b = 0,
                
                beta_vitamin_a = 0,
                beta_vitamin_b = 0,
                
                beta_price_a = 0,
                beta_price_b = 0,
                
                delta_a   = 0,
                delta_b   = 0)

### Vector with names (in quotes) of parameters to be kept fixed at their starting value in apollo_beta, use apollo_beta_fixed = c() if none
apollo_fixed = c("delta_b")

# ################################################################# #
#### DEFINE LATENT CLASS COMPONENTS                              ####
# ################################################################# #

apollo_lcPars=function(apollo_beta, apollo_inputs){
  lcpars = list()
  
  lcpars[["beta_barn"]]    = list(beta_barn_a, beta_barn_b)
  lcpars[["beta_range"]]   = list(beta_range_a, beta_range_b)
  lcpars[["beta_organic"]] = list(beta_organic_a, beta_organic_b)
  lcpars[["beta_vitamin"]] = list(beta_vitamin_a, beta_vitamin_b)
  lcpars[["beta_price"]]   = list(beta_price_a, beta_price_b)
  
  V=list()
  V[["class_a"]] = delta_a
  V[["class_b"]] = delta_b
  
  classAlloc_settings = list(
    classes      = c(class_a=1, class_b=2), 
    utilities    = V
  )
  
  lcpars[["pi_values"]] = apollo_classAlloc(classAlloc_settings)
  
  return(lcpars)
}

# ################################################################# #
#### GROUP AND VALIDATE INPUTS                                   ####
# ################################################################# #

apollo_inputs = apollo_validateInputs()

# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION                        ####
# ################################################################# #

apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){
  
  ### Attach inputs and detach after function exit
  apollo_attach(apollo_beta, apollo_inputs)
  on.exit(apollo_detach(apollo_beta, apollo_inputs))
  
  ### Create list of probabilities P
  P = list()
  
  ### Define settings for MNL model component that are generic across classes
  mnl_settings = list(
    alternatives = c(alt1=1, alt2=2, alt3=3),
    avail        = list(alt1=1, alt2=1, alt3=1),
    choiceVar    = choice
  )
  
  ### Loop over classes
  for(s in 1:2){
    
    ### Compute class-specific utilities
    V=list()
    V[["alt1"]] = (           beta_barn[[s]]*barn_alt1 + beta_range[[s]]*range_alt1 + beta_organic[[s]]*organic_alt1 + beta_vitamin[[s]]*vitamin_alt1 + beta_price[[s]]*price_alt1)
    V[["alt2"]] = (           beta_barn[[s]]*barn_alt2 + beta_range[[s]]*range_alt2 + beta_organic[[s]]*organic_alt2 + beta_vitamin[[s]]*vitamin_alt2 + beta_price[[s]]*price_alt2)
    V[["alt3"]] = (asc_alt3 + beta_barn[[s]]*barn_alt3 + beta_range[[s]]*range_alt3 + beta_organic[[s]]*organic_alt3 + beta_vitamin[[s]]*vitamin_alt3 + beta_price[[s]]*price_alt3)

    
    mnl_settings$utilities = V
    #mnl_settings$componentName = paste0("Class_",s)
    
    ### Compute within-class choice probabilities using MNL model
    P[[paste0("Class_",s)]] = apollo_mnl(mnl_settings, functionality)
    
    ### Take product across observation for same individual
    P[[paste0("Class_",s)]] = apollo_panelProd(P[[paste0("Class_",s)]], apollo_inputs ,functionality)
  }
  
  ### Compute latent class model probabilities
  lc_settings   = list(inClassProb = P, classProb=pi_values)
  P[["model"]] = apollo_lc(lc_settings, apollo_inputs, functionality)
  
  ### Prepare and return outputs of function
  P = apollo_prepareProb(P, apollo_inputs, functionality)
  return(P)
}

# ################################################################# #
#### MODEL ESTIMATION                                            ####
# ################################################################# #

### Optional starting values search
# apollo_beta=apollo_searchStart(apollo_beta, apollo_fixed,apollo_probabilities, apollo_inputs)

model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
Would you please let me know how to get this respondent assignment?

Thanks in advance

Cuong

Re: Identify respondents in Latent class logit model

Posted: 17 Jul 2023, 09:24
by stephanehess
Dear Cuong

in a latent class model, the class membership is probabilistic. You cannot say that an individual belongs to a class, they only do so up to a probability.

What you can do after estimation is to work out the class allocation probabilities at the person level, either just on the basis of the model (unconditionals), or also by taking into account the choices that this person made, which will help you narrow down which class they are most likely to belong to. But again, you cannot say with certainty which class someone falls into.

All of this is covered in the manual, and also in the online example files. Did you look at those?

Stephane

Re: Identify respondents in Latent class logit model

Posted: 18 Jul 2023, 20:53
by qcng
Dear Prof. Hess, thanks for your explanation.

Based on this, I guess the probabilities can be collected from the code

Code: Select all

conditionals = apollo_conditionals(model,apollo_probabilities, apollo_inputs)
Then, for example, the probability for respondent ID8 is
ID X1 X2
8 8.136859e-03 9.918631e-01
We could say that the respondent ID8 mostly belongs to class b as the probability is 0.9918631. Is that correct?

Regarding number of classes, I wonder if we have some criteria to choose this number of classes. In other words, how we could decide there are two or three classes in LC model.

Other things, is it possible to get beta values for each of respondents?

Please give me some feedbacks.

Best regards,

Cuong

Re: Identify respondents in Latent class logit model

Posted: 19 Jul 2023, 09:55
by stephanehess
Cuong

what you can say is that, conditional on the model you have estimated, and conditional on the choices you have observed for this respondent, there is a 99% posterior probability of falling into class b.

Regarding beta values for each respondent, this would again be making the mistake of seeing class allocation as deterministic. You can compute posterior marginal rates of substitution (see the online examples). You should only do that for ratios of parameters, not individual parameters given the potential scale differences across classes.

In relation to the number of classes, this is discussed extensively in the literature. See for example my book chapter and the references therein

Hess, S. (2014), Latent class structures: taste heterogeneity and beyond, in Hess, S. & Daly, A.J. (Eds.), Handbook of Choice Modelling, chapter 14, pp. 311-330, Edward Elgar publishers, Cheltenham.

http://www.stephanehess.me.uk/papers/bo ... s_2014.pdf

Stephane

Re: Identify respondents in Latent class logit model

Posted: 07 Sep 2023, 10:17
by esadergin
qcng wrote: 18 Jul 2023, 20:53
Based on this, I guess the probabilities can be collected from the code

Code: Select all

conditionals = apollo_conditionals(model,apollo_probabilities, apollo_inputs)
Then, for example, the probability for respondent ID8 is
ID X1 X2
8 8.136859e-03 9.918631e-01
We could say that the respondent ID8 mostly belongs to class b as the probability is 0.9918631. Is that correct?
Hello Cuong,

How did you calculate the probabilities?

Thanks in advance.

Re: Identify respondents in Latent class logit model

Posted: 18 Sep 2023, 09:23
by stephanehess
It means that, conditional on the choices we have observed and the model we have estimated, there is a 99% chance that this person belongs to class 2

The details on how this is calculated are in the manual