Page 1 of 1

Error with "Duplicated componentName found"

Posted: 07 Oct 2022, 16:27
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)
}


Re: Error with "Duplicated componentName found"

Posted: 07 Oct 2022, 18:03
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

Re: Error with "Duplicated componentName found"

Posted: 09 Oct 2022, 18:56
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)
}


Re: Error with "Duplicated componentName found"

Posted: 10 Oct 2022, 16:54
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

Re: Error with "Duplicated componentName found"

Posted: 11 Oct 2022, 20:37
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)


Re: Error with "Duplicated componentName found"

Posted: 12 Oct 2022, 07:20
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