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 with "Duplicated componentName found"

Ask questions about errors you encouunter. Please make sure to include full details about your model specifications, and ideally your model file.
Post Reply
rafael_lionello
Posts: 10
Joined: 26 Apr 2020, 04:24

Error with "Duplicated componentName found"

Post by rafael_lionello »

Dear Professors,

I am trying to fit a RP/SP model with random parameters.

However, I am getting the following error:
Error in apollo_mnl(mnl_settings = mnl_settings_SP, functionality) :
Duplicated componentName found (MNL). Names must be different for each component.

Any suggestions are highly appreciated.


Many thanks in advance!

Kind regards,

Rafael.


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       = "MMNL_preference_space",
  modelDescr      = "Mixed logit model on choice data, uncorrelated Lognormals in preference space",
  indivID         = "Resposta",  
  #mixing          = TRUE,
  #nCores          = 4L,
  outputDirectory = "output"
)

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

### Loading data from package
### if data is to be loaded from a file (e.g. called data.csv), 
### the code would be: database = read.csv("data.csv",header=TRUE)
database = read.csv("C:/Users/rafae/Downloads/database_conjoint.csv", sep = ";", encoding = "UTF-8")

### for data dictionary, use ?apollo_swissRouteChoiceData




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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(asc_brand_1   = 0,
                asc_brand_2  = 0,
                asc_brand_3   = 0,
                asc_brand_4  = 0,
                asc_brand_5  = 0,
                asc_brand_6   = 0,
                asc_brand_9   = 0,
                asc_brand_7 = 0,
                asc_brand_8 = 0,
                asc_other    = 0,
                b_price_highTier_app2   = 0,
                b_price_middleTier_app2 = 0,
                b_price_lowTier_app2    = 0,
                b_price_lastTier_app2   = 0,
                b_price_highTier_app3   = 0,
                b_price_middleTier_app3 = 0,
                b_price_lowTier_app3    = 0,
                b_price_lastTier_app3   = 0,
                mu_log_b_price_highTier      = -3,
                sigma_log_b_price_highTier   = -0.01,
                mu_log_b_price_middleTier    = -3,
                sigma_log_b_price_middleTier = -0.01,
                mu_log_b_price_lowTier       = -3,
                sigma_log_b_price_lowTier    = -0.01,
                mu_log_b_price_lastTier      = -3,
                sigma_log_b_price_lastTier   = -0.01,
                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")

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

### Set parameters for generating draws
apollo_draws = list(
  interDrawsType = "halton",
  interNDraws    = 50,
  interUnifDraws = c(),
  interNormDraws = c("draws_price_highTier","draws_price_middleTier","draws_price_lowTier","draws_price_lastTier"),
  intraDrawsType = "halton",
  intraNDraws    = 0,
  intraUnifDraws = c(),
  intraNormDraws = c()
)

### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
  randcoeff = list()
  
  randcoeff[["b_price_highTier"]]   = -exp( mu_log_b_price_highTier   + sigma_log_b_price_highTier   * draws_price_highTier )
  randcoeff[["b_price_middleTier"]] = -exp( mu_log_b_price_middleTier + sigma_log_b_price_middleTier * draws_price_middleTier )
  randcoeff[["b_price_lowTier"]]    = -exp( mu_log_b_price_lowTier    + sigma_log_b_price_lowTier    * draws_price_lowTier )
  randcoeff[["b_price_lastTier"]]   = -exp( mu_log_b_price_lastTier   + sigma_log_b_price_lastTier   * draws_price_lastTier )
  
  return(randcoeff)
}

# ################################################################# #
#### 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()
  
  ### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
  V = list()
  
  V[["brand_1"]]   = asc_brand_1   + b_price_highTier   * price_brand_1   + b_price_highTier_app2   * price_brand_1   * Aplicacao_2 + b_price_highTier_app3   * price_brand_1   * Aplicacao_3
  V[["brand_2"]]  = asc_brand_2  + b_price_highTier   * price_brand_2  + b_price_highTier_app2   * price_brand_2  * Aplicacao_2 + b_price_highTier_app3   * price_brand_2  * Aplicacao_3
  V[["brand_3"]]   = asc_brand_3   + b_price_highTier   * price_brand_3   + b_price_highTier_app2   * price_brand_3   * Aplicacao_2 + b_price_highTier_app3   * price_brand_3   * Aplicacao_3
  
  V[["brand_4"]]  = asc_brand_4  + b_price_middleTier * price_brand_4  + b_price_middleTier_app2 * price_brand_4  * Aplicacao_2 + b_price_middleTier_app3 * price_brand_4  * Aplicacao_3
  V[["brand_5"]]  = asc_brand_5  + b_price_middleTier * price_brand_5  + b_price_middleTier_app2 * price_brand_5  * Aplicacao_2 + b_price_middleTier_app3 * price_brand_5  * Aplicacao_3
  
  V[["brand_6"]]   = asc_brand_6   + b_price_lowTier    * price_brand_6   + b_price_lowTier_app2    * price_brand_6   * Aplicacao_2 + b_price_lowTier_app3    * price_brand_6   * Aplicacao_3
  V[["brand_9"]]   = asc_brand_9   + b_price_lowTier    * price_brand_9   + b_price_lowTier_app2    * price_brand_9   * Aplicacao_2 + b_price_lowTier_app3    * price_brand_9   * Aplicacao_3
  V[["brand_7"]] = asc_brand_7 + b_price_lowTier    * price_brand_7 + b_price_lowTier_app2    * price_brand_7 * Aplicacao_2 + b_price_lowTier_app3    * price_brand_7 * Aplicacao_3
  
  V[["brand_8"]] = asc_brand_8 + b_price_lastTier   * price_brand_8 + b_price_lastTier_app2   * price_brand_8 * Aplicacao_2 + b_price_lastTier_app3   * price_brand_8 * Aplicacao_3
  
  V[["other"]]    = asc_other
  V[["none"]]     = 0
  
  
  ### Compute probabilities for the RP part of the data using MNL model
  mnl_settings_RP = list(
    alternatives = c(
      brand_1 = 2L, 
      brand_9 = 3L,
      brand_5 = 5L, 
      brand_2 = 6L, 
      brand_3 = 10L, 
      brand_4 = 11L, 
      brand_8 = 12L, 
      brand_7 = 14L, 
      brand_6 = 15L, 
      other = 97L, 
      none = 98L
    ),
    avail = list(
      brand_1 = av_brand_1, 
      brand_2 = av_brand_2, 
      brand_3 = av_brand_3, 
      brand_4 = av_brand_4, 
      brand_5 = av_brand_5, 
      brand_6 = av_brand_6, 
      brand_9 = av_brand_9, 
      brand_7 = av_brand_7, 
      brand_8 = av_brand_8, 
      other = av_other, 
      none = av_none
    ),
    choiceVar = Choice,
    utilities = list(
      brand_1   = mu_RP*V[["brand_1"]], 
      brand_9   = mu_RP*V[["brand_9"]], 
      brand_5  = mu_RP*V[["brand_5"]], 
      brand_2  = mu_RP*V[["brand_2"]], 
      brand_3   = mu_RP*V[["brand_3"]], 
      brand_4  = mu_RP*V[["brand_4"]], 
      brand_8 = mu_RP*V[["brand_8"]], 
      brand_7 = mu_RP*V[["brand_7"]], 
      brand_6   = mu_RP*V[["brand_6"]], 
      other    = mu_RP*V[["other"]], 
      none     = mu_RP*V[["none"]]),
    rows = (Rp==1)
  )
  
  P[["RP"]] = apollo_mnl(mnl_settings_RP, functionality)
  
  
  
  ### Compute probabilities for the SP part of the data using MNL model
  mnl_settings_SP = list(
    alternatives = c(
      brand_1 = 2L, 
      brand_9 = 3L,
      brand_5 = 5L, 
      brand_2 = 6L, 
      brand_3 = 10L, 
      brand_4 = 11L, 
      brand_8 = 12L, 
      brand_7 = 14L, 
      brand_6 = 15L, 
      other = 97L, 
      none = 98L
    ),
    avail = list(
      brand_1 = av_brand_1, 
      brand_2 = av_brand_2, 
      brand_3 = av_brand_3, 
      brand_4 = av_brand_4, 
      brand_5 = av_brand_5, 
      brand_6 = av_brand_6, 
      brand_9 = av_brand_9, 
      brand_7 = av_brand_7, 
      brand_8 = av_brand_8, 
      other = av_other, 
      none = av_none
    ),
    choiceVar = Choice,
    utilities = list(
      brand_1   = mu_SP*V[["brand_1"]], 
      brand_9   = mu_SP*V[["brand_9"]], 
      brand_5  = mu_SP*V[["brand_5"]], 
      brand_2  = mu_SP*V[["brand_2"]], 
      brand_3   = mu_SP*V[["brand_3"]], 
      brand_4  = mu_SP*V[["brand_4"]], 
      brand_8 = mu_SP*V[["brand_8"]], 
      brand_7 = mu_SP*V[["brand_7"]], 
      brand_6   = mu_SP*V[["brand_6"]], 
      other    = mu_SP*V[["other"]], 
      none     = mu_SP*V[["none"]]),
    rows = (Sp==1)
  )
  
  P[["SP"]] = apollo_mnl(mnl_settings = mnl_settings_SP, functionality)
  
  
  ### Combined model
  P = apollo_combineModels(P, apollo_inputs, functionality)
  
  ### Take product across observation for same individual
  P = apollo_panelProd(P, apollo_inputs, functionality)
  
  ### Prepare and return outputs of function
  P = apollo_prepareProb(P, apollo_inputs, functionality)
  return(P)
}

stephanehess
Site Admin
Posts: 998
Joined: 24 Apr 2020, 16:29

Re: Error with "Duplicated componentName found"

Post by stephanehess »

Hi

this is happening as the pre-processing of your model is failing for some other reason (again, we will soon release a version with a more helpful error message). If you want to share your model and data with me, then I'll find the real reason

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
rafael_lionello
Posts: 10
Joined: 26 Apr 2020, 04:24

Re: Error with "Duplicated componentName found"

Post by rafael_lionello »

Dear Professor,

Follow the database (link) and script below.

(I think, the first message has not attached the files.)

Thank you again.

Best, Rafael.



DATABASE:
drive.google.com/file/d/1VXJSN7LI82thnc ... sp=sharing

SCRIPT:

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       = "MMNL_preference_space",
  modelDescr      = "Mixed logit model on choice data, uncorrelated Lognormals in preference space",
  indivID         = "Resposta",  
  #mixing          = TRUE,
  #nCores          = 4L,
  outputDirectory = "output"
)

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

### Loading data from package
### if data is to be loaded from a file (e.g. called data.csv), 
### the code would be: database = read.csv("data.csv",header=TRUE)
database = read.csv("C:/Users/rafae/OneDrive/Área de Trabalho/database_conjoint.csv", sep = ",", encoding = "UTF-8")

### for data dictionary, use ?apollo_swissRouteChoiceData

saveRDS(database, file = "database_conjoint.R")


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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(asc_brand_1   = 0,
                asc_brand_4  = 0,
                asc_brand_5   = 0,
                asc_brand_6  = 0,
                asc_brand_3  = 0,
                asc_brand_9   = 0,
                asc_brand_2   = 0,
                asc_brand_8 = 0,
                asc_brand_7 = 0,
                asc_other    = 0,
                b_price_highTier_app2   = -0.01,
                b_price_middleTier_app2 = -0.01,
                b_price_lowTier_app2    = -0.01,
                b_price_lastTier_app2   = -0.01,
                b_price_highTier_app3   = -0.01,
                b_price_middleTier_app3 = -0.01,
                b_price_lowTier_app3    = -0.01,
                b_price_lastTier_app3   = -0.01,
                mu_log_b_price_highTier      = -3,
                sigma_log_b_price_highTier   = -0.1,
                mu_log_b_price_middleTier    = -3,
                sigma_log_b_price_middleTier = -0.01,
                mu_log_b_price_lowTier       = -3,
                sigma_log_b_price_lowTier    = -0.01,
                mu_log_b_price_lastTier      = -3,
                sigma_log_b_price_lastTier   = -0.01,
                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")

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

### Set parameters for generating draws
apollo_draws = list(
  interDrawsType = "halton",
  interNDraws    = 500,
  interUnifDraws = c(),
  interNormDraws = c("draws_price_highTier","draws_price_middleTier","draws_price_lowTier","draws_price_lastTier"),
  intraDrawsType = "halton",
  intraNDraws    = 0,
  intraUnifDraws = c(),
  intraNormDraws = c()
)

### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
  randcoeff = list()
  
  randcoeff[["b_price_highTier"]]   = -exp( mu_log_b_price_highTier   + sigma_log_b_price_highTier   * draws_price_highTier )
  randcoeff[["b_price_middleTier"]] = -exp( mu_log_b_price_middleTier + sigma_log_b_price_middleTier * draws_price_middleTier )
  randcoeff[["b_price_lowTier"]]    = -exp( mu_log_b_price_lowTier    + sigma_log_b_price_lowTier    * draws_price_lowTier )
  randcoeff[["b_price_lastTier"]]   = -exp( mu_log_b_price_lastTier   + sigma_log_b_price_lastTier   * draws_price_lastTier )
  
  return(randcoeff)
}

# ################################################################# #
#### 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()
  
  ### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
  V = list()
  
  V[["brand_1"]]   = asc_brand_1   + b_price_highTier   * price_brand_1   + b_price_highTier_app2   * price_brand_1   * Aplicacao_2 + b_price_highTier_app3   * price_brand_1   * Aplicacao_3
  V[["brand_4"]]  = asc_brand_4  + b_price_highTier   * price_brand_4  + b_price_highTier_app2   * price_brand_4  * Aplicacao_2 + b_price_highTier_app3   * price_brand_4  * Aplicacao_3
  V[["brand_5"]]   = asc_brand_5   + b_price_highTier   * price_brand_5   + b_price_highTier_app2   * price_brand_5   * Aplicacao_2 + b_price_highTier_app3   * price_brand_5   * Aplicacao_3
  
  V[["brand_6"]]  = asc_brand_6  + b_price_middleTier * price_brand_6  + b_price_middleTier_app2 * price_brand_6  * Aplicacao_2 + b_price_middleTier_app3 * price_brand_6  * Aplicacao_3
  V[["brand_3"]]  = asc_brand_3  + b_price_middleTier * price_brand_3  + b_price_middleTier_app2 * price_brand_3  * Aplicacao_2 + b_price_middleTier_app3 * price_brand_3  * Aplicacao_3
  
  V[["brand_9"]]   = asc_brand_9   + b_price_lowTier    * price_brand_9   + b_price_lowTier_app2    * price_brand_9   * Aplicacao_2 + b_price_lowTier_app3    * price_brand_9   * Aplicacao_3
  V[["brand_2"]]   = asc_brand_2   + b_price_lowTier    * price_brand_2   + b_price_lowTier_app2    * price_brand_2   * Aplicacao_2 + b_price_lowTier_app3    * price_brand_2   * Aplicacao_3
  V[["brand_8"]] = asc_brand_8 + b_price_lowTier    * price_brand_8 + b_price_lowTier_app2    * price_brand_8 * Aplicacao_2 + b_price_lowTier_app3    * price_brand_8 * Aplicacao_3
  
  V[["brand_7"]] = asc_brand_7 + b_price_lastTier   * price_brand_7 + b_price_lastTier_app2   * price_brand_7 * Aplicacao_2 + b_price_lastTier_app3   * price_brand_7 * Aplicacao_3
  
  V[["other"]]    = asc_other
  V[["none"]]     = 0
  
  
  ### Compute probabilities for the RP part of the data using MNL model
  mnl_settings_RP = list(
    alternatives = c(
      brand_1 = 2L, 
      brand_2 = 3L,
      brand_3 = 5L, 
      brand_4 = 6L, 
      brand_5 = 10L, 
      brand_6 = 11L, 
      brand_7 = 12L, 
      brand_8 = 14L, 
      brand_9 = 15L, 
      other = 97L, 
      none = 98L
    ),
    avail = list(
      brand_1 = av_brand_1, 
      brand_4 = av_brand_4, 
      brand_5 = av_brand_5, 
      brand_6 = av_brand_6, 
      brand_3 = av_brand_3, 
      brand_9 = av_brand_9, 
      brand_2 = av_brand_2, 
      brand_8 = av_brand_8, 
      brand_7 = av_brand_7, 
      other = av_other, 
      none = av_none
    ),
    choiceVar = Choice,
    utilities = list(
      brand_1   = mu_RP*V[["brand_1"]], 
      brand_2   = mu_RP*V[["brand_2"]], 
      brand_3  = mu_RP*V[["brand_3"]], 
      brand_4  = mu_RP*V[["brand_4"]], 
      brand_5   = mu_RP*V[["brand_5"]], 
      brand_6  = mu_RP*V[["brand_6"]], 
      brand_7 = mu_RP*V[["brand_7"]], 
      brand_8 = mu_RP*V[["brand_8"]], 
      brand_9   = mu_RP*V[["brand_9"]], 
      other    = mu_RP*V[["other"]], 
      none     = mu_RP*V[["none"]]),
    rows = (Rp==1)
  )
  
  P[["RP"]] = apollo_mnl(mnl_settings_RP, functionality)
  
  
  
  ### Compute probabilities for the SP part of the data using MNL model
  mnl_settings_SP = list(
    alternatives = c(
      brand_1 = 2L, 
      brand_2 = 3L,
      brand_3 = 5L, 
      brand_4 = 6L, 
      brand_5 = 10L, 
      brand_6 = 11L, 
      brand_7 = 12L, 
      brand_8 = 14L, 
      brand_9 = 15L, 
      other = 97L, 
      none = 98L
    ),
    avail = list(
      brand_1 = av_brand_1, 
      brand_4 = av_brand_4, 
      brand_5 = av_brand_5, 
      brand_6 = av_brand_6, 
      brand_3 = av_brand_3, 
      brand_9 = av_brand_9, 
      brand_2 = av_brand_2, 
      brand_8 = av_brand_8, 
      brand_7 = av_brand_7, 
      other = av_other, 
      none = av_none
    ),
    choiceVar = Choice,
    utilities = list(
      brand_1   = mu_SP*V[["brand_1"]], 
      brand_2   = mu_SP*V[["brand_2"]], 
      brand_3  = mu_SP*V[["brand_3"]], 
      brand_4  = mu_SP*V[["brand_4"]], 
      brand_5   = mu_SP*V[["brand_5"]], 
      brand_6  = mu_SP*V[["brand_6"]], 
      brand_7 = mu_SP*V[["brand_7"]], 
      brand_8 = mu_SP*V[["brand_8"]], 
      brand_9   = mu_SP*V[["brand_9"]], 
      other    = mu_SP*V[["other"]], 
      none     = mu_SP*V[["none"]]),
    rows = (Sp==1)
  )
  
  P[["SP"]] = apollo_mnl(mnl_settings = mnl_settings_SP, functionality)
  
  
  ### Combined model
  P = apollo_combineModels(P, apollo_inputs, functionality)
  
  ### Take product across observation for same individual
  P = apollo_panelProd(P, apollo_inputs, functionality)
  
  ### Prepare and return outputs of function
  P = apollo_prepareProb(P, apollo_inputs, functionality)
  return(P)
}

stephanehess
Site Admin
Posts: 998
Joined: 24 Apr 2020, 16:29

Re: Error with "Duplicated componentName found"

Post by stephanehess »

Hi Rafael

Apollo fails due to some errors and unfortunately ends up in the wrong place with the error message. We've fixed this in version 0.2.9 which will be available soon.

In terms of your model, you're missing the averaging across draws, so you need the line

Code: Select all

  P = apollo_avgInterDraws(P, apollo_inputs, functionality)
after

Code: Select all

P = apollo_panelProd(P, apollo_inputs, functionality)
Once you fix that, you get the following error message which tells you what you need to do with your data to get it to work:

WARNING: The data contains cases where alternative brand_1 is chosen for model component "SP" despite being listed as
unavailable. This will cause the chosen probability to be zero, and potentially lead to an invalid LL.

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
rafael_lionello
Posts: 10
Joined: 26 Apr 2020, 04:24

Re: Error with "Duplicated componentName found"

Post by rafael_lionello »

Hi Stephane,

I am sorry, but I have averaged across draws as you mentioned and then fixed the database.

However, the error persistis. I have just updated R, R Studio, and Apollo to make sure that it is not a update issue.

Again, follow the code and the (fixed) database: https://1drv.ms/u/s!AsZiRaIO20SDlOobvef ... Q?e=5ed4Xe

Thank you very much for your time.

Best regards, Rafael.


Code: Select all


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

### Load Apollo library
library(apollo)
library(tidyverse)



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

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName       = "MMNL_preference_space",
  modelDescr      = "Mixed logit model on choice data, uncorrelated Lognormals in preference space",
  indivID         = "Resposta",  
  #mixing          = TRUE,
  #nCores          = 4L,
  outputDirectory = "output"
)


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

### Loading data from package
### if data is to be loaded from a file (e.g. called data.csv), 
### the code would be: database = read.csv("data.csv",header=TRUE)
database = read.csv("C:/Users/rafae/OneDrive/Área de Trabalho/database_apollo_RP_SP.csv", sep = ",", encoding = "UTF-8")

### for data dictionary, use ?apollo_swissRouteChoiceData

# excluding the non chosen alternative in RP data
database <-
  database %>% 
  filter(Choice != 2)



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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(#asc_brand_1   = 2.19541,
  asc_brand_2  = 3.10474,
  asc_brand_3   = 5.18659,
  asc_brand_4  = 4.60255,
  asc_brand_5  = 3.42898,
  asc_brand_6   = 3.34893,
  asc_brand_7   = 3.92143,
  asc_brand_8 = 3.61563,
  asc_brand_9 = 4.33131,
  asc_other    = -0.21127,
  b_price_highTier_app2   = 0.00399,
  b_price_middleTier_app2 = -0.00446,
  b_price_lowTier_app2    = -0.00841,
  b_price_lastTier_app2   = 0.00333,
  b_price_highTier_app3   = -0.00604,
  b_price_middleTier_app3 = -0.01260,
  b_price_lowTier_app3    = -0.00679,
  b_price_lastTier_app3   = -0.00907,
  mu_log_b_price_highTier      = -4.68687,
  sigma_log_b_price_highTier   = -0.55416,
  mu_log_b_price_middleTier    = -3.61764,
  sigma_log_b_price_middleTier = -4.13189,
  mu_log_b_price_lowTier       = -4.87837,
  sigma_log_b_price_lowTier    = -1.23573,
  mu_log_b_price_lastTier      = -4.75179,
  sigma_log_b_price_lastTier   = 1.55980,
  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")

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

### Set parameters for generating draws
apollo_draws = list(
  interDrawsType = "halton",
  interNDraws    = 50,
  interUnifDraws = c(),
  interNormDraws = c("draws_price_highTier","draws_price_middleTier","draws_price_lowTier","draws_price_lastTier"),
  intraDrawsType = "halton",
  intraNDraws    = 0,
  intraUnifDraws = c(),
  intraNormDraws = c()
)

### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
  randcoeff = list()
 
  randcoeff[["b_price_highTier"]]   = -exp( mu_log_b_price_highTier   + sigma_log_b_price_highTier   * draws_price_highTier )
  randcoeff[["b_price_middleTier"]] = -exp( mu_log_b_price_middleTier + sigma_log_b_price_middleTier * draws_price_middleTier )
  randcoeff[["b_price_lowTier"]]    = -exp( mu_log_b_price_lowTier    + sigma_log_b_price_lowTier    * draws_price_lowTier )
  randcoeff[["b_price_lastTier"]]   = -exp( mu_log_b_price_lastTier   + sigma_log_b_price_lastTier   * draws_price_lastTier )
  
  
  return(randcoeff)
}

# ################################################################# #
#### 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()
  
  ### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
  V = list()
  
  #V[["brand_1"]]   = asc_brand_1   + b_price_highTier   * price_brand_1   + b_price_highTier_app2   * price_brand_1   * Aplicacao_2 + b_price_highTier_app3   * price_brand_1   * Aplicacao_3
  V[["brand_2"]]  = asc_brand_2  + b_price_highTier   * price_brand_2  + b_price_highTier_app2   * price_brand_2  * Aplicacao_2 + b_price_highTier_app3   * price_brand_2  * Aplicacao_3
  V[["brand_3"]]   = asc_brand_3   + b_price_highTier   * price_brand_3   + b_price_highTier_app2   * price_brand_3   * Aplicacao_2 + b_price_highTier_app3   * price_brand_3   * Aplicacao_3
  
  V[["brand_4"]]  = asc_brand_4  + b_price_middleTier * price_brand_4  + b_price_middleTier_app2 * price_brand_4  * Aplicacao_2 + b_price_middleTier_app3 * price_brand_4  * Aplicacao_3
  V[["brand_5"]]  = asc_brand_5  + b_price_middleTier * price_brand_5  + b_price_middleTier_app2 * price_brand_5  * Aplicacao_2 + b_price_middleTier_app3 * price_brand_5  * Aplicacao_3
  
  V[["brand_6"]]   = asc_brand_6   + b_price_lowTier    * price_brand_6   + b_price_lowTier_app2    * price_brand_6   * Aplicacao_2 + b_price_lowTier_app3    * price_brand_6   * Aplicacao_3
  V[["brand_7"]]   = asc_brand_7   + b_price_lowTier    * price_brand_7   + b_price_lowTier_app2    * price_brand_7   * Aplicacao_2 + b_price_lowTier_app3    * price_brand_7   * Aplicacao_3
  V[["brand_8"]] = asc_brand_8 + b_price_lowTier    * price_brand_8 + b_price_lowTier_app2    * price_brand_8 * Aplicacao_2 + b_price_lowTier_app3    * price_brand_8 * Aplicacao_3
  
  V[["brand_9"]] = asc_brand_9 + b_price_lastTier   * price_brand_9 + b_price_lastTier_app2   * price_brand_9 * Aplicacao_2 + b_price_lastTier_app3   * price_brand_9 * Aplicacao_3
  
  V[["other"]]    = asc_other
  V[["none"]]     = 0
  
  
  ### Compute probabilities for the RP part of the data using MNL model
  mnl_settings_RP = list(
    alternatives = c(
      #brand_1 = 2L, 
      brand_7 = 3L,
      brand_5 = 5L, 
      brand_2 = 6L, 
      brand_3 = 10L, 
      brand_4 = 11L, 
      brand_9 = 12L, 
      brand_8 = 14L, 
      brand_6 = 15L, 
      other = 97L, 
      none = 98L
    ),
    avail = list(
      #brand_1 = av_brand_1, 
      brand_2 = av_brand_2, 
      brand_3 = av_brand_3, 
      brand_4 = av_brand_4, 
      brand_5 = av_brand_5, 
      brand_6 = av_brand_6, 
      brand_7 = av_brand_7, 
      brand_8 = av_brand_8, 
      brand_9 = av_brand_9, 
      other = av_other, 
      none = av_none
    ),
    choiceVar = Choice,
    utilities = list(
      #brand_1   = mu_RP*V[["brand_1"]], 
      brand_7   = mu_RP*V[["brand_7"]], 
      brand_5  = mu_RP*V[["brand_5"]], 
      brand_2  = mu_RP*V[["brand_2"]], 
      brand_3   = mu_RP*V[["brand_3"]], 
      brand_4  = mu_RP*V[["brand_4"]], 
      brand_9 = mu_RP*V[["brand_9"]], 
      brand_8 = mu_RP*V[["brand_8"]], 
      brand_6   = mu_RP*V[["brand_6"]], 
      other    = mu_RP*V[["other"]], 
      none     = mu_RP*V[["none"]]),
    rows = (RP==1)
  )
  
  P[["RP"]] = apollo_mnl(mnl_settings_RP, functionality)
  
  
  
  ### Compute probabilities for the SP part of the data using MNL model
  mnl_settings_SP = list(
    alternatives = c(
      #brand_1 = 2L, 
      brand_7 = 3L,
      brand_5 = 5L, 
      brand_2 = 6L, 
      brand_3 = 10L, 
      brand_4 = 11L, 
      brand_9 = 12L, 
      brand_8 = 14L, 
      brand_6 = 15L, 
      other = 97L, 
      none = 98L
    ),
    avail = list(
      #brand_1 = av_brand_1, 
      brand_2 = av_brand_2, 
      brand_3 = av_brand_3, 
      brand_4 = av_brand_4, 
      brand_5 = av_brand_5, 
      brand_6 = av_brand_6, 
      brand_7 = av_brand_7, 
      brand_8 = av_brand_8, 
      brand_9 = av_brand_9, 
      other = av_other, 
      none = av_none
    ),
    choiceVar = Choice,
    utilities = list(
      #brand_1   = mu_SP*V[["brand_1"]], 
      brand_7   = mu_SP*V[["brand_7"]], 
      brand_5  = mu_SP*V[["brand_5"]], 
      brand_2  = mu_SP*V[["brand_2"]], 
      brand_3   = mu_SP*V[["brand_3"]], 
      brand_4  = mu_SP*V[["brand_4"]], 
      brand_9 = mu_SP*V[["brand_9"]], 
      brand_8 = mu_SP*V[["brand_8"]], 
      brand_6   = mu_SP*V[["brand_6"]], 
      other    = mu_SP*V[["other"]], 
      none     = mu_SP*V[["none"]]),
    rows = (SP==1)
  )
  
  P[["SP"]] = apollo_mnl(mnl_settings = mnl_settings_SP, functionality)
  
  
  ### Combined model
  P = apollo_combineModels(P, apollo_inputs, functionality)
  
  ### Take product across observation for same individual
  P = apollo_panelProd(P, apollo_inputs, functionality)
  P = apollo_avgInterDraws(P, apollo_inputs, functionality)
  
  ### Prepare and return outputs of function
  P = apollo_prepareProb(P, apollo_inputs, functionality)
  return(P)
}




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

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

stephanehess
Site Admin
Posts: 998
Joined: 24 Apr 2020, 16:29

Re: Error with "Duplicated componentName found"

Post by stephanehess »

Rafael

please download the development version (0.2.9) from http://apollochoicemodelling.com/code.html. This fixes the issue with the wrong error message, and tells you what is really going wrong with your model

Best wishes

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Post Reply