Important: Read this before posting to this forum

  1. This forum is for questions related to the use of Apollo. We will answer some general choice modelling questions too, where appropriate, and time permitting. We cannot answer questions about how to estimate choice models with other software packages.
  2. There is a very detailed manual for Apollo available at http://www.ApolloChoiceModelling.com/manual.html. This contains detailed descriptions of the various Apollo functions, and numerous examples are available at http://www.ApolloChoiceModelling.com/examples.html. In addition, help files are available for all functions, using e.g. ?apollo_mnl
  3. Before asking a question on the forum, users are kindly requested to follow these steps:
    1. Check that the same issue has not already been addressed in the forum - there is a search tool.
    2. Ensure that the correct syntax has been used. For any function, detailed instructions are available directly in Apollo, e.g. by using ?apollo_mnl for apollo_mnl
    3. Check the frequently asked questions section on the Apollo website, which discusses some common issues/failures. Please see http://www.apollochoicemodelling.com/faq.html
    4. Make sure that R is using the latest official release of Apollo.
  4. If the above steps do not resolve the issue, then users should follow these steps when posting a question:
    1. provide full details on the issue, including the entire code and output, including any error messages
    2. posts will not immediately appear on the forum, but will be checked by a moderator first. This may take a day or two at busy times. There is no need to submit the post multiple times.

Identify respondents in Latent class logit model

Ask questions about post-estimation functions (e.g. prediction, conditionals, etc) or other processing of results.
Post Reply
qcng
Posts: 16
Joined: 25 Jun 2023, 12:22

Identify respondents in Latent class logit model

Post 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
stephanehess
Site Admin
Posts: 998
Joined: 24 Apr 2020, 16:29

Re: Identify respondents in Latent class logit model

Post 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
--------------------------------
Stephane Hess
www.stephanehess.me.uk
qcng
Posts: 16
Joined: 25 Jun 2023, 12:22

Re: Identify respondents in Latent class logit model

Post 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
stephanehess
Site Admin
Posts: 998
Joined: 24 Apr 2020, 16:29

Re: Identify respondents in Latent class logit model

Post 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
--------------------------------
Stephane Hess
www.stephanehess.me.uk
esadergin
Posts: 7
Joined: 10 Jan 2021, 13:17

Re: Identify respondents in Latent class logit model

Post 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.
stephanehess
Site Admin
Posts: 998
Joined: 24 Apr 2020, 16:29

Re: Identify respondents in Latent class logit model

Post 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
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Post Reply