Error during RRM estimation
Posted: 03 Aug 2021, 16:54
Hello,
I am having problems to estimate my RRM model.
When I run the code bellow, I have this error:
Testing likelihood function...
Error in apollo_preprocess(inputs = mnl_settings, modelType, functionality, :
The "alternatives" argument for model component "model" needs to be a named vector
Can anyone help, please?
Thanks a lot,
Sílvia
My code:
# ################################################################# #
#### 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 ="mnl_rrm 1",
modelDescr ="Simple MNL model with 30 alternatives",
indivID ="num",
nCores = 6
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
### For this example, we generate a database with 2000 observations
# following a logit generation process.
# No panel data (i.e. each obs is from a different individual).
# 100 alternatives, each with two attributes: x1 and x2
# True coefficients are 1 and -1 for x1 and x2, respectively.
N = 4183 #Obs
J = 338 #Alternativas
I = 338
#database <- read.csv("database_com_30_corrigido.csv")
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(b1 = 0, b2 = 0, b3 = 0)
### 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()
# ################################################################# #
#### 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()
for(j in 1:J) {
V[[paste0("alt",j)]] =
for(i in 1:I) {
log(2) + log(2) + log(2) +
- get(paste0("AVAIL_",i)) * log(1+exp(b1*(get(paste0("TTi",i))- get(paste0("TTi",j))))) +
- get(paste0("AVAIL_",i)) * log(1+exp(b1*(get(paste0("COi",i))- get(paste0("COi",j))))) +
- get(paste0("AVAIL_",i)) * log(1+exp(b1*(get(paste0("EMPi",i))- get(paste0("EMPi",j)))))
}
}
### Define settings for MNL model component
mnl_settings = list(
alternatives = setNames(1:J, names(V)),
avail = setNames(apollo_inputs$database[,paste0("AVAIL_",1:J)], names(V)),
choiceVar = choice,
V = V
)
### Compute probabilities using MNL model
P[['model']] = apollo_mnl(mnl_settings, functionality)
### Prepare and return outputs of function
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}
# ################################################################# #
#### MODEL ESTIMATION ####
# ################################################################# #
model_rrm = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
I am having problems to estimate my RRM model.
When I run the code bellow, I have this error:
Testing likelihood function...
Error in apollo_preprocess(inputs = mnl_settings, modelType, functionality, :
The "alternatives" argument for model component "model" needs to be a named vector
Can anyone help, please?
Thanks a lot,
Sílvia
My code:
# ################################################################# #
#### 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 ="mnl_rrm 1",
modelDescr ="Simple MNL model with 30 alternatives",
indivID ="num",
nCores = 6
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
### For this example, we generate a database with 2000 observations
# following a logit generation process.
# No panel data (i.e. each obs is from a different individual).
# 100 alternatives, each with two attributes: x1 and x2
# True coefficients are 1 and -1 for x1 and x2, respectively.
N = 4183 #Obs
J = 338 #Alternativas
I = 338
#database <- read.csv("database_com_30_corrigido.csv")
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(b1 = 0, b2 = 0, b3 = 0)
### 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()
# ################################################################# #
#### 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()
for(j in 1:J) {
V[[paste0("alt",j)]] =
for(i in 1:I) {
log(2) + log(2) + log(2) +
- get(paste0("AVAIL_",i)) * log(1+exp(b1*(get(paste0("TTi",i))- get(paste0("TTi",j))))) +
- get(paste0("AVAIL_",i)) * log(1+exp(b1*(get(paste0("COi",i))- get(paste0("COi",j))))) +
- get(paste0("AVAIL_",i)) * log(1+exp(b1*(get(paste0("EMPi",i))- get(paste0("EMPi",j)))))
}
}
### Define settings for MNL model component
mnl_settings = list(
alternatives = setNames(1:J, names(V)),
avail = setNames(apollo_inputs$database[,paste0("AVAIL_",1:J)], names(V)),
choiceVar = choice,
V = V
)
### Compute probabilities using MNL model
P[['model']] = apollo_mnl(mnl_settings, functionality)
### Prepare and return outputs of function
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}
# ################################################################# #
#### MODEL ESTIMATION ####
# ################################################################# #
model_rrm = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)