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.

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

Report bugs or highlight issues with Apollo functions. At a minimum, please include the part of the output where you believe the bug is manifested. Ideally, please share your model file.
Post Reply
Derek
Posts: 4
Joined: 26 Apr 2020, 17:23

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

Post 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.
dpalma
Posts: 190
Joined: 24 Apr 2020, 17:54

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

Post 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
DavidKL
Posts: 15
Joined: 27 Mar 2021, 16:30

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

Post 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
dpalma
Posts: 190
Joined: 24 Apr 2020, 17:54

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

Post 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
DavidKL
Posts: 15
Joined: 27 Mar 2021, 16:30

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

Post by DavidKL »

Hi David!

Thank you very much!

It worked perfectly!

Best wishes,
David
Post Reply