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