Page 1 of 1

Error in estimating Latent class model with RP-SP data

Posted: 02 May 2024, 10:15
by Aditya249
Hi!
Prof. Hess, and Dr. Palma

I am trying to develop a latent class model with Joint RP-SP data. However, I am getting the following error: object 'mnl_settings_RP' not found

Could you please help me out in resolving this?

The code is as follows:

Code: Select all

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

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

### Load Apollo library
library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName       = "Apollo_IPA",
  modelDescr      = "RP_SP_HCM",
  indivID         = "ID", 
  nCores          = 16,
  outputDirectory = "output"
)

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

database = read.csv("RP_SP_CP_3.csv")

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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(asc_local               = 0,
              asc_metro               = 0,
              asc_car                 = 0,
              asc_tw                  = 0,

              b_tt_a                    = 0,
              b_tc_a                    = 0,
              b_at_a                    = 0,

              b_tt_b                    = 0,
              b_tc_b                    = 0,
              b_at_b                    = 0,

              delta_a                   = 0,
              delta_b                   = 0,

              female_a                  = 0,
              Income_0_50k_a            = 0,
              Age_18_30_a               = 0,

              female_b                  = 0,
              Income_0_50k_b            = 0,
              Age_18_30_b               = 0,
            

              mu_RP                   = 1,
              mu_SP                   = 1)

### 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("mu_RP", "asc_car", "female_a")


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

apollo_lcPars=function(apollo_beta, apollo_inputs){
  lcpars = list()
  lcpars[["b_tt"]] = list(b_tt_a, b_tt_b)
  lcpars[["b_tc"]] = list(b_tc_a, b_tc_b)
  lcpars[["b_at"]] = list(b_at_a, b_at_b)

### Utilities of class allocation model
  V=list()


  V[["class_a"]] = delta_a + female_a*female + Income_0_50k_a*Income_0_50k + Age_18_30_a*Age_18_30
  V[["class_b"]] = delta_b + female_b*female + Income_0_50k_b*Income_0_50k + Age_18_30_b*Age_18_30

### Settings for class allocation models
  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()

  ### Loop over classes
  for(s in 1:2){

 ### List of utilities (before applying scales): these must use the same names as in mnl_settings, order is irrelevant
  V = list()
  V[["local"]]  = asc_local + b_tt[[s]]*tt_local + b_tc[[s]]*tc_local + b_at[[s]]*at_local


  V[["metro"]]  = asc_metro  + b_tt[[s]]*tt_metro + b_tc[[s]]*tc_metro + b_at[[s]]*at_metro


  V[["car"]]    = asc_car    + b_tt[[s]]*tt_car + b_tc[[s]]*tc_car        
                  

  V[["tw"]]     = asc_tw     + b_tt[[s]]*tt_tw + b_tc[[s]]*tc_tw 


    mnl_settings_RP$componentName = paste0("Class_",s)
    mnl_settings_SP$componentName = paste0("Class_",s)

### Compute probabilities for the RP part of the data using MNL model
  mnl_settings_RP = list(
       alternatives  = c(local=1, metro=2, car=3, tw=4), 
       avail         = list(local=av_local, metro=av_metro, car=av_car, tw=av_tw), 
       choiceVar     = Choice, 
       utilities     = list(local = mu_RP*V[["local"]],
                            metro = mu_RP*V[["metro"]],
                            car   = mu_RP*V[["car"]],
                            tw    = mu_RP*V[["tw"]]),
       rows          = (RP==1)

  )

    mnl_settings_RP$componentName = paste0("Class_",s)


 P[["RP"]] = apollo_mnl(mnl_settings_RP, functionality)

### Compute probabilities for the RP part of the data using MNL model
  mnl_settings_SP = list(
       alternatives  = c(local=1, metro=2, car=3, tw=4), 
       avail         = list(local=av_local, metro=av_metro, car=av_car, tw=av_tw), 
       choiceVar     = Choice, 
       utilities     = list(local  = mu_SP*V[["local"]],
                            metro  = mu_SP*V[["metro"]],
                            car    = mu_SP*V[["car"]],
                            tw     = mu_SP*V[["tw"]]),
       rows          = (SP==1)

  )

    mnl_settings_SP$componentName = paste0("Class_",s)

 P[["SP"]] = apollo_mnl(mnl_settings_SP, functionality)
   



### Compute within-class choice probabilities using MNL model
    P[[paste0("Class_",s)]] = apollo_mnl(mnl_settings_RP, functionality)
    P[[paste0("Class_",s)]] = apollo_mnl(mnl_settings_SP, functionality)
    
    ### Take product across observation for same individual
    P[[paste0("Class_",s)]] = apollo_panelProd(P[[paste0("Class_",s)]], apollo_inputs ,functionality)
    
  }

  ### Combined model
  P = apollo_combineModels(P, apollo_inputs, functionality)

 ### Compute latent class model probabilities
  lc_settings  = list(inClassProb = P, classProb=pi_values)
  P[["RP"]] = apollo_lc(lc_settings, apollo_inputs, functionality)
  P[["SP"]] = apollo_lc(lc_settings, apollo_inputs, functionality)

  ### Prepare and return outputs of function
  P = apollo_prepareProb(P, apollo_inputs, functionality)
  return(P)
}


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

### Estimate model
model = apollo_estimate(apollo_beta, apollo_fixed, 
                        apollo_probabilities, apollo_inputs)

### Show output in screen
apollo_modelOutput(model)

### Save output to file(s)


# ################################################################# #
#### MODEL OUTPUTS                                               ####
# ################################################################# #

# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN)                               ----
# ----------------------------------------------------------------- #

apollo_modelOutput(model)












Re: Error in estimating Latent class model with RP-SP data

Posted: 05 May 2024, 10:49
by stephanehess
Hi

simple fix. Have a look at your code. You have

Code: Select all

mnl_settings_RP$componentName = paste0("Class_",s)
mnl_settings_SP$componentName = paste0("Class_",s)
before you have:

Code: Select all

mnl_settings_RP = list(
       alternatives  = c(local=1, metro=2, car=3, tw=4), 
...
so the list mnl_settings_RP does not yet exist when you try to set the componentName element inside it

Stephane

Re: Error in estimating Latent class model with RP-SP data

Posted: 05 May 2024, 12:42
by Aditya249
Hi!
Thank you for your prompt response. Based on your suggestion, I have revised the code. However, now I am getting the following error:

Error in apollo_combineModels(P, apollo_inputs, functionality) :
SPECIFICATION ISSUE - A component called model already exists in P before calling apollo_combineModels!


Please help in resolving this.

The revised code is as follows:

Code: Select all

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

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

### Load Apollo library
library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName       = "Apollo_IPA",
  modelDescr      = "RP_SP_HCM",
  indivID         = "ID", 
  nCores          = 16,
  outputDirectory = "output"
)

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

database = read.csv("RP_SP_CP_3.csv")

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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(asc_local               = 0,
              asc_metro               = 0,
              asc_car                 = 0,
              asc_tw                  = 0,

              b_tt_a                    = 0,
              b_tc_a                    = 0,
              b_at_a                    = 0,

              b_tt_b                    = 0,
              b_tc_b                    = 0,
              b_at_b                    = 0,

              delta_a                   = 0,
              delta_b                   = 0,

              female_a                  = 0,
              Income_0_50k_a            = 0,
              Age_18_30_a               = 0,

              female_b                  = 0,
              Income_0_50k_b            = 0,
              Age_18_30_b               = 0,
            

              mu_RP                   = 1,
              mu_SP                   = 1)

### 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("mu_RP", "asc_car", "female_a")


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

apollo_lcPars=function(apollo_beta, apollo_inputs){
  lcpars = list()
  lcpars[["b_tt"]] = list(b_tt_a, b_tt_b)
  lcpars[["b_tc"]] = list(b_tc_a, b_tc_b)
  lcpars[["b_at"]] = list(b_at_a, b_at_b)

### Utilities of class allocation model
  V=list()


  V[["class_a"]] = delta_a + female_a*female + Income_0_50k_a*Income_0_50k + Age_18_30_a*Age_18_30
  V[["class_b"]] = delta_b + female_b*female + Income_0_50k_b*Income_0_50k + Age_18_30_b*Age_18_30

### Settings for class allocation models
  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()

  ### Loop over classes
  for(s in 1:2){

 ### List of utilities (before applying scales): these must use the same names as in mnl_settings, order is irrelevant
  V = list()
  V[["local"]]  = asc_local + b_tt[[s]]*tt_local + b_tc[[s]]*tc_local + b_at[[s]]*at_local


  V[["metro"]]  = asc_metro  + b_tt[[s]]*tt_metro + b_tc[[s]]*tc_metro + b_at[[s]]*at_metro


  V[["car"]]    = asc_car    + b_tt[[s]]*tt_car + b_tc[[s]]*tc_car        
                  

  V[["tw"]]     = asc_tw     + b_tt[[s]]*tt_tw + b_tc[[s]]*tc_tw 


### Compute probabilities for the RP part of the data using MNL model
  mnl_settings_RP = list(
       alternatives  = c(local=1, metro=2, car=3, tw=4), 
       avail         = list(local=av_local, metro=av_metro, car=av_car, tw=av_tw), 
       choiceVar     = Choice, 
       utilities     = list(local = mu_RP*V[["local"]],
                            metro = mu_RP*V[["metro"]],
                            car   = mu_RP*V[["car"]],
                            tw    = mu_RP*V[["tw"]]),
       rows          = (RP==1)

  )


 P[["RP"]] = apollo_mnl(mnl_settings_RP, functionality)

   

### Compute probabilities for the RP part of the data using MNL model
  mnl_settings_SP = list(
       alternatives  = c(local=1, metro=2, car=3, tw=4), 
       avail         = list(local=av_local, metro=av_metro, car=av_car, tw=av_tw), 
       choiceVar     = Choice, 
       utilities     = list(local  = mu_SP*V[["local"]],
                            metro  = mu_SP*V[["metro"]],
                            car    = mu_SP*V[["car"]],
                            tw     = mu_SP*V[["tw"]]),
       rows          = (SP==1)

  )

 P[["SP"]] = apollo_mnl(mnl_settings_SP, functionality)



mnl_settings_RP$componentName = paste0("Class_",s)    
mnl_settings_SP$componentName = paste0("Class_",s)

   
### Compute within-class choice probabilities using MNL model
    P[[paste0("Class_",s)]] = apollo_mnl(mnl_settings_RP, functionality)
    P[[paste0("Class_",s)]] = apollo_mnl(mnl_settings_SP, functionality)

 
### Combined model
    P = apollo_combineModels(P, apollo_inputs, functionality)


### Take product across observation for same individual
    P[[paste0("Class_",s)]] = apollo_panelProd(P[[paste0("Class_",s)]], apollo_inputs ,functionality)

    P = apollo_panelProd(P, 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                                            ####
# ################################################################# #

### Estimate model
model = apollo_estimate(apollo_beta, apollo_fixed, 
                        apollo_probabilities, apollo_inputs)

### Show output in screen
apollo_modelOutput(model)

### Save output to file(s)


# ################################################################# #
#### MODEL OUTPUTS                                               ####
# ################################################################# #

# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN)                               ----
# ----------------------------------------------------------------- #

apollo_modelOutput(model)

Re: Error in estimating Latent class model with RP-SP data

Posted: 06 May 2024, 07:55
by stephanehess
You'll need to be a bit careful here as combineModels is used inside the LC. probably best to work with temporary lists rather than P for RP and SP. You also need to tell it which ones to combine

Something like this (look where I have used P_temp and where I have used P

You probably need to make some more changes to this, but it's a start for you

Code: Select all

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()
[color=#FF0000]  P_temp = list()[/color]

  ### Loop over classes
  for(s in 1:2){

 ### List of utilities (before applying scales): these must use the same names as in mnl_settings, order is irrelevant
  V = list()
  V[["local"]]  = asc_local + b_tt[[s]]*tt_local + b_tc[[s]]*tc_local + b_at[[s]]*at_local
  V[["metro"]]  = asc_metro  + b_tt[[s]]*tt_metro + b_tc[[s]]*tc_metro + b_at[[s]]*at_metro
  V[["car"]]    = asc_car    + b_tt[[s]]*tt_car + b_tc[[s]]*tc_car        
  V[["tw"]]     = asc_tw     + b_tt[[s]]*tt_tw + b_tc[[s]]*tc_tw 


### Compute probabilities for the RP part of the data using MNL model
  mnl_settings_RP = list(
       alternatives  = c(local=1, metro=2, car=3, tw=4), 
       avail         = list(local=av_local, metro=av_metro, car=av_car, tw=av_tw), 
       choiceVar     = Choice, 
       utilities     = list(local = mu_RP*V[["local"]],
                            metro = mu_RP*V[["metro"]],
                            car   = mu_RP*V[["car"]],
                            tw    = mu_RP*V[["tw"]]),
       rows          = (RP==1)

  )

 [color=#FF0000]P_temp[[paste0("RP_class_",s)]][/color] = apollo_mnl(mnl_settings_RP, functionality)

### Compute probabilities for the RP part of the data using MNL model
  mnl_settings_SP = list(
       alternatives  = c(local=1, metro=2, car=3, tw=4), 
       avail         = list(local=av_local, metro=av_metro, car=av_car, tw=av_tw), 
       choiceVar     = Choice, 
       utilities     = list(local  = mu_SP*V[["local"]],
                            metro  = mu_SP*V[["metro"]],
                            car    = mu_SP*V[["car"]],
                            tw     = mu_SP*V[["tw"]]),
       rows          = (SP==1)

  )

 [color=#FF0000]P_temp[[paste0("SP_class_",s)]][/color] = apollo_mnl(mnl_settings_SP, functionality)

### Combined model
[color=#FF0000]    PP[[paste0("Class_",s)]] = apollo_combineModels(P_temp, apollo_inputs, functionality,components=c(paste0("RP_class_",s),paste0("SP_class_",s)))[/color]

### Take product across observation for same individual
[color=#FF0000]    P[[paste0("Class_",s)]] = apollo_panelProd(P[[paste0("Class_",s)]], apollo_inputs ,functionality)[/color]

[color=#FF0000]    P[[paste0("Class_",s)]] = apollo_panelProd(P[[paste0("Class_",s)]], apollo_inputs, functionality)[/color]

}
    
 ### 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)
}

Re: Error in estimating Latent class model with RP-SP data

Posted: 06 May 2024, 18:02
by Aditya249
Hi!

I tried multiple times to work on this code and modify it based on the provided suggestion. Now I am getting the following error: Testing influence of parametersError in apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, :
SPECIFICATION ISSUE - Parameters b_tt_a, b_tc_a, b_at_a, female_b do not influence the log-likelihood of your model!


I request you to please help in resolving this.

The modified code is as follows:
# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################################################# #

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

### Load Apollo library
library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
modelName = "Apollo_IPA",
modelDescr = "RP_SP_HCM",
indivID = "ID",
nCores = 16,
outputDirectory = "output"
)

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

database = read.csv("RP_SP_CP_3.csv")

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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(asc_local = 0,
asc_metro = 0,
asc_car = 0,
asc_tw = 0,

b_tt_a = 0,
b_tc_a = 0,
b_at_a = 0,

b_tt_b = 0,
b_tc_b = 0,
b_at_b = 0,

delta_a = 0,
delta_b = 0,

female_a = 0,
Income_0_50k_a = 0,
Age_18_30_a = 0,

female_b = 0,
Income_0_50k_b = 0,
Age_18_30_b = 0,


mu_RP = 1,
mu_SP = 1)

### 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("mu_RP", "asc_car", "female_a")


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

apollo_lcPars=function(apollo_beta, apollo_inputs){
lcpars = list()
lcpars[["b_tt"]] = list(b_tt_a, b_tt_b)
lcpars[["b_tc"]] = list(b_tc_a, b_tc_b)
lcpars[["b_at"]] = list(b_at_a, b_at_b)

### Utilities of class allocation model
V=list()
V[["class_a"]] = delta_a + female_a*female + Income_0_50k_a*Income_0_50k + Age_18_30_a*Age_18_30
V[["class_b"]] = delta_b + female_b*female + Income_0_50k_b*Income_0_50k + Age_18_30_b*Age_18_30

### Settings for class allocation models
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()
P_temp = list()


### Loop over classes
for(s in 1:2){

### List of utilities (before applying scales): these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[["local"]] = asc_local + b_tt[[s]]*tt_local + b_tc[[s]]*tc_local + b_at[[s]]*at_local
V[["metro"]] = asc_metro + b_tt[[s]]*tt_metro + b_tc[[s]]*tc_metro + b_at[[s]]*at_metro
V[["car"]] = asc_car + b_tt[[s]]*tt_car + b_tc[[s]]*tc_car
V[["tw"]] = asc_tw + b_tt[[s]]*tt_tw + b_tc[[s]]*tc_tw


### Compute probabilities for the RP part of the data using MNL model
mnl_settings_RP = list(
alternatives = c(local=1, metro=2, car=3, tw=4),
avail = list(local=av_local, metro=av_metro, car=av_car, tw=av_tw),
choiceVar = Choice,
utilities = list(local = mu_RP*V[["local"]],
metro = mu_RP*V[["metro"]],
car = mu_RP*V[["car"]],
tw = mu_RP*V[["tw"]]),
rows = (RP==1)

)

P_temp[[paste0("RP_class_",s)]] = apollo_mnl(mnl_settings_RP, functionality)

### Compute probabilities for the RP part of the data using MNL model
mnl_settings_SP = list(
alternatives = c(local=1, metro=2, car=3, tw=4),
avail = list(local=av_local, metro=av_metro, car=av_car, tw=av_tw),
choiceVar = Choice,
utilities = list(local = mu_SP*V[["local"]],
metro = mu_SP*V[["metro"]],
car = mu_SP*V[["car"]],
tw = mu_SP*V[["tw"]]),
rows = (SP==1)

)

P_temp[[paste0("SP_class_",s)]] = apollo_mnl(mnl_settings_SP, functionality)


### Combined model
P[[paste0("Class_",s)]] = apollo_combineModels(P_temp, apollo_inputs, functionality,components=c(paste0("RP_class_",s),paste0("SP_class_",s)))

### 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 ####
# ################################################################# #

### Estimate model
model = apollo_estimate(apollo_beta, apollo_fixed,
apollo_probabilities, apollo_inputs)

### Show output in screen
apollo_modelOutput(model)

### Save output to file(s)


# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #

# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #

apollo_modelOutput(model)

Re: Error in estimating Latent class model with RP-SP data

Posted: 07 May 2024, 07:55
by stephanehess
Can you share your code and data with me outside the forum, and I'll try to debug it for you

Re: Error in estimating Latent class model with RP-SP data

Posted: 07 May 2024, 09:59
by Aditya249
Thank You for offering the help, Prof. Hess.

As suggested, I have shared the code and data with you at stephane.hess@gmail.com

Re: Error in estimating Latent class model with RP-SP data

Posted: 07 May 2024, 10:24
by stephanehess
Hi

two changes are needed in your code.

As apollo_combineModels is called inside the latent class model, you need

Code: Select all

P[[paste0("Class_",s)]] = apollo_combineModels(P_temp, apollo_inputs, functionality,components=c(paste0("RP_class_",s),paste0("SP_class_",s)),asList=FALSE)
instead of

Code: Select all

P[[paste0("Class_",s)]] = apollo_combineModels(P_temp, apollo_inputs, functionality,components=c(paste0("RP_class_",s),paste0("SP_class_",s)))
Also, your model is currently overspecified. You cannot estimate the parameters in the class allocation model for both classes. So you need

Code: Select all

apollo_fixed = c("mu_RP", "asc_car", 
                 "delta_a",
                 "female_a",      
                 "Income_0_50k_a",
                 "Age_18_30_a"   )
Stephane

Re: Error in estimating Latent class model with RP-SP data

Posted: 07 May 2024, 11:07
by Aditya249
Thank you Prof. Hess for the suggestion.

The code is working fine after altering the starting values of those parameters.

Re: Error in estimating Latent class model with RP-SP data

Posted: 07 May 2024, 15:57
by stephanehess
yes, having all the starting values at zero also led to an issue with the initial check of the gradients. It's always good practice to use different starting values in different classes