Page 1 of 1

Error message "duplicated componentName" in Hybrid Choice Model using Bayesian approach

Posted: 15 Nov 2020, 00:56
by Derek
When I run the example "hybrid_model_bayesian.r" in this website using Apollo v 0.2.1, the following error message appears:

Apollo found a model component of type OL without a componentName. The name was set to "OL" by default.
Error in apollo_ol(ol_settings2, functionality) :
Duplicated componentName found (OL). Names must be different for each component.


This happens not only for the ordered logit model component. When I used the continuous measurement model component with the following likelihood of indicators,

### Likelihood of indicators
normalDensity_settings1 = list(outcomeNormal= comop,
xNormal=zeta_LV1_comop * LV1,
mu=0,
sigma=sigma_LV1_comop,
rows=(ques==1))
normalDensity_settings2 = list(outcomeNormal= dirop,
xNormal=zeta_LV2_dirop * LV2,
mu=0,
sigma=sigma_LV2_dirop,
rows=(ques==1))
normalDensity_settings3 = list(outcomeNormal= dis_op,
xNormal=zeta_LV3_dis_op * LV3,
mu=0,
sigma=sigma_LV3_dis_op,
rows=(ques==1))
normalDensity_settings4 = list(outcomeNormal= un_avoid,
xNormal=zeta_LV4_un_avoid * LV4,
mu=0,
sigma=sigma_LV4_un_avoid,
rows=(ques==1))

P[["indic_comop"]] = apollo_normalDensity(normalDensity_settings1, functionality)
P[["indic_dirop"]] = apollo_normalDensity(normalDensity_settings2, functionality)
P[["indic_dis_op"]] = apollo_normalDensity(normalDensity_settings3, functionality)
P[["indic_un_avoid"]] = apollo_normalDensity(normalDensity_settings4, functionality)



A similar error message appears:

Apollo found a model component of type NormD without a componentName. The name was set to "NormD" by
default.

Summary statistics for NORMD model component NormD:
Min. 1st Qu. Median Mean 3rd Qu. Max.
-2.7529 -0.7529 0.2471 0 0.2471 3.2471
Apollo found a model component of type NormD without a componentName. The name was set to "NormD" by
default.
Error in apollo_normalDensity(normalDensity_settings2, functionality) :
Duplicated componentName found (NormD). Names must be different for each component.


Thanks a lot.

Re: Error message "duplicated componentName" in Hybrid Choice Model using Bayesian approach

Posted: 16 Nov 2020, 17:37
by dpalma
Hi Derek,

Thank you for letting us know about this bug. The issue arises in v0.2.1 when using multiple components of the same type (e.g. ordered logit) in a model using Bayesian estimation.

We have fixed the bug in version 0.2.2, which you can find on the webpage, section "code": http://www.apollochoicemodelling.com/code.html

Best wishes
David

Re: Error message "duplicated componentName" in Hybrid Choice Model using Bayesian approach

Posted: 29 Jul 2021, 18:11
by DavidKL
Hi,

I get the same error message.
"Error in apollo_normalDensity(Qq2, functionality) :
Duplicated componentName found (NormD). Names must be different for each component."

My specification is the following:

Code: Select all

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

### Initialise
rm(list = ls())
library(apollo)
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName  = "HLC",
  modelDescr = "Hybrid latent class model",
  indivID    = "ID",
  mixing     = TRUE,
  nCores     = 5
)

database = read.csv("data.csv",header=TRUE)

database$Q19_r1 <- database$Q19_r1 - mean(database$Q19_r1)
database$Q19_r2 <- database$Q19_r2 - mean(database$Q19_r2)
database$Q19_r3 <- database$Q19_r3 - mean(database$Q19_r3)
database$Q19_r4 <- database$Q19_r4 - mean(database$Q19_r4)
database$Q19_r5 <- database$Q19_r5 - mean(database$Q19_r5)



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

### Vector of parameters
apollo_beta = c(# Structural equation params
  gamma_city= 0, gamma_age_above_49= 0, gamma_high_income= 0, 
  # measurement model params
  zeta_q1= 0, zeta_q2= 0, zeta_q3= 0, 
  zeta_q4= 0, zeta_q5= 0,
  sigma_q1= 1, sigma_q2= 1, sigma_q3= 1, 
  sigma_q4= 1, sigma_q5= 1,
  # Class allocation params
  piConsA= 0, piLVA= 0,
  # Choice params
  asc_nc= 0, 
  # class 1 params
  b_eP_a= 0, b_sQ_a= 0, 
  b_gF_a= 0, b_aF_a= 0, b_price_a= 0, 
  # class 2 params
  b_eP_b= 0, b_sQ_b= 0, 
  b_gF_b= 0, b_aF_b= 0, b_price_b= 0)

### Name of fixed parameters. c() for none.
apollo_fixed = c()

# ################################################################# #
#### DEFINE RANDOM COMPONENTS                                    ####
# ################################################################# #

### Set parameters for generating draws
apollo_draws = list(
  interDrawsType = "halton", 
  interNDraws    = 500,          
  interUnifDraws = c(),      
  interNormDraws = c("eta")
)

### Create random parameters
apollo_randCoeff=function(apollo_beta, apollo_inputs){
  r = list()
  
  r[["LV"]] = gamma_city*city + gamma_age_above_49*age_above_49 + gamma_high_income*high_income + eta
  
  return(r)
}

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

apollo_lcPars=function(apollo_beta, apollo_inputs){
  lcpars = list()
  
  ### Define lists of parameters for each class
  ###                      classA   classB  ...
  lcpars[["b_eP"]] = list(b_eP_a, b_eP_b)
  lcpars[["b_sQ"]] = list(b_sQ_a, b_sQ_b)
  lcpars[["b_gF"]] = list(b_gF_a, b_gF_b)
  lcpars[["b_aF"]] = list(b_aF_a, b_aF_b)
  lcpars[["b_price"]] = list(b_price_a, b_price_b)
  

  VA  = piConsA + piLVA*LV
  VB  = 0
  piA = exp(VA)/(exp(VA) + exp(VB))
  piB = exp(VB)/(exp(VA) + exp(VB))
  lcpars[["pi_values"]] = apollo_firstRow(list(piA, piB), 
                                          apollo_inputs)
  
  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 = list()
  PClass = list()
  
  ### Likelihood of indicators
  Qq1 = list(outcomeNormal=Q19_r1, xNormal=zeta_q1*LV, mu=0, sigma=sigma_q1, rows=(task==1))
  Qq2 = list(outcomeNormal=Q19_r2, xNormal=zeta_q2*LV, mu=0, sigma=sigma_q2, rows=(task==1))
  Qq3 = list(outcomeNormal=Q19_r3, xNormal=zeta_q3*LV, mu=0, sigma=sigma_q3, rows=(task==1))
  Qq4 = list(outcomeNormal=Q19_r4, xNormal=zeta_q4*LV, mu=0, sigma=sigma_q4, rows=(task==1))
  Qq5 = list(outcomeNormal=Q19_r5, xNormal=zeta_q5*LV, mu=0, sigma=sigma_q5, rows=(task==1))
  
  ### Keep only the first row, as it is the only one that matters
  P[["Indicator_1"]] = apollo_firstRow( apollo_normalDensity(Qq1, functionality), apollo_inputs )
  P[["Indicator_2"]] = apollo_firstRow( apollo_normalDensity(Qq2, functionality), apollo_inputs )
  P[["Indicator_3"]] = apollo_firstRow( apollo_normalDensity(Qq3, functionality), apollo_inputs )
  P[["Indicator_4"]] = apollo_firstRow( apollo_normalDensity(Qq4, functionality), apollo_inputs )
  P[["Indicator_5"]] = apollo_firstRow( apollo_normalDensity(Qq5, functionality), apollo_inputs )
  
  ### Likelihood of CHOICES
  ### Define settings for MNL model component that are generic across classes
  cho = list(
    alternatives =    c(alt1=1, alt2=2, alt3=3, altnc=4),
    avail        = list(alt1=av_alt1, alt2=av_alt2, alt3=av_alt3, altnc=av_altnc),
    choiceVar    = choice,
    V            = list() )
  ### Loop over classes
  S = 2 
  for(s in 1:S){
    ### Class-specific utilities
    cho$V[['alt1']]  = b_eP[[s]]  * ( l_alt1 == 2) + b_sQ[[s]]  * ( l_alt1 == 3) + b_gF[[s]]  * ( h_alt1 == 2) + b_aF[[s]]  * ( h_alt1 == 3) + b_price[[s]]  * price_alt1 
    cho$V[['alt2']]  = b_eP[[s]]  * ( l_alt2 == 2) + b_sQ[[s]]  * ( l_alt2 == 3) + b_gF[[s]]  * ( h_alt2 == 2) + b_aF[[s]]  * ( h_alt2 == 3) + b_price[[s]]  * price_alt2
    cho$V[['alt3']]  = b_eP[[s]]  * ( l_alt3 == 2) + b_sQ[[s]]  * ( l_alt3 == 3) + b_gF[[s]]  * ( h_alt3 == 2) + b_aF[[s]]  * ( h_alt3 == 3) + b_price[[s]]  * price_alt3
    cho$V[['altnc']] = asc_nc
    
    label = paste0("choiceClass",s)
    PClass[[label]] = apollo_mnl(cho, functionality)
    PClass[[label]] = apollo_panelProd(PClass[[label]], apollo_inputs, functionality)
  }
  ### Mix the probabilities from each class
  lc_settings   = list(inClassProb=PClass, classProb=pi_values)
  P[["choice"]] = apollo_lc(lc_settings, apollo_inputs, functionality)
  
  ### Likelihood
  P = apollo_combineModels(P, apollo_inputs, functionality)
  ### Average across inter-individual draws
  P = apollo_avgInterDraws(P, apollo_inputs, functionality)
  ### Prepare and return outputs of function
  P = apollo_prepareProb(P, apollo_inputs, functionality)
  return(P)
}


# ################################################################# #
#### MODEL ESTIMATION AND OUTPUT                                 ####
# ################################################################# #
s = list(writeIter=FALSE)#, estimationRoutine="bhhh")
model = apollo_estimate(apollo_beta, apollo_fixed, 
                        apollo_probabilities, apollo_inputs,
                        estimate_settings=s)

apollo_modelOutput(model)
s = list(saveEst=FALSE, saveCov=FALSE, saveCorr=FALSE, 
         saveModelObject=FALSE)
apollo_saveOutput(model, saveOutput_settings=s)
I use the latest (0.2.4) version of Apollo.

How could I solve this problem?

Thank you!

David

Re: Error message "duplicated componentName" in Hybrid Choice Model using Bayesian approach

Posted: 12 Aug 2021, 11:16
by dpalma
Hi David,

Please try modifying the settings of your linear models (measurement equations) as follows:

Code: Select all

### Likelihood of indicators
Qq1 = list(outcomeNormal=Q19_r1, xNormal=zeta_q1*LV, mu=0, sigma=sigma_q1, rows=(task==1), componentName='Indicator_1')
Qq2 = list(outcomeNormal=Q19_r2, xNormal=zeta_q2*LV, mu=0, sigma=sigma_q2, rows=(task==1), componentName='Indicator_2')
Qq3 = list(outcomeNormal=Q19_r3, xNormal=zeta_q3*LV, mu=0, sigma=sigma_q3, rows=(task==1), componentName='Indicator_3')
Qq4 = list(outcomeNormal=Q19_r4, xNormal=zeta_q4*LV, mu=0, sigma=sigma_q4, rows=(task==1), componentName='Indicator_4')
Qq5 = list(outcomeNormal=Q19_r5, xNormal=zeta_q5*LV, mu=0, sigma=sigma_q5, rows=(task==1), componentName='Indicator_5')
Apollo needs unique names for each model component. Normally, it should automatically assign the name used inside the list P to each component, but for some reason that doesn't seem to be working in your case. We will look into that. In the meantime, try manually assigning the component names, as described above.

If that doesn't work, please share a bit of your data (a few lines will do) so that we can look into the problem in more detail. You can email me the data piece if you prefer to d.palma [at] leeds.ac.uk

Best
David

Re: Error message "duplicated componentName" in Hybrid Choice Model using Bayesian approach

Posted: 13 Aug 2021, 15:16
by DavidKL
Hi David!

Thank you very much!

It worked perfectly!

Best wishes,
David