I am getting this error code in apollo when estimating a MMNL model, assuming here that the parameter of my cost attibute is distributed as lognormal and the rest of my parameters are distributed as normal.
Erreur dans apollo_mnl(c(mnl_settings, componentName2 = "model"), functionality) :
CALCULATION ISSUE - Some utilities for model component "model" contain values that are not finite numbers!
I did try with finite numbers as starting values as mean_log_b_cost = -3 and sd_log_b_cost = 0, but then I get the following error code: Erreur dans apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, :
CALCULATION ISSUE - Log-likelihood calculation fails at values close to the starting values!
I wonder if this is due to how my cost variable is coded, my levels are high amount of money: 45 000, 50 000, 55 000, 60 000 euros, I therefore get a very small negative coeff for this attribute when estimating a regular mnl as -9.674e-05.
This is my code below.
Thanks in advance for the help.
Code: Select all
### Clear memory
rm(list = ls())
### Load Apollo library
library(apollo)
### Initialise code
apollo_initialise()
parallel::detectCores()
### Set core controls
apollo_control = list(
modelName = "MMNL_uncorrelated",
modelDescr = "Mixed logit model WITH sobol draws",
indivID = "RID",
nCores = 11,
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)
setwd("C:/Users/***/Nextcloud/Documents/Choice Experiment/Apollo")
database = read.csv("widedata.csv",header=TRUE)
### for data dictionary, use ?apollo_swissRouteChoiceData
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(asc_alt3 = 0,
mean_b_suppnone = 0,
sd_b_suppnone = 0,
mean_b_suppindiv = 0.48,
sd_b_suppindiv = 0,
mean_b_suppcoll = 0.60,
sd_b_suppcoll = 0,
mean_b_tvxindiv = 0,
sd_b_tvxindiv = 0,
mean_b_tvxindgp = -0.04,
sd_b_tvxindgp = 0,
mean_b_tvxcoop = -0.10,
sd_b_tvxcoop = 0,
mean_b_pulvindiv = 0,
sd_b_pulvindiv = 0,
mean_b_pulvindgp = -0.64,
sd_b_pulvindgp = 0,
mean_b_pulvcoop = -0.84,
sd_b_pulvcoop = 0,
mean_b_techindiv = 0,
sd_b_techindiv = 0,
mean_b_techcoll = -0.22,
sd_b_techcoll = 0,
mean_log_b_cost = NA,
sd_log_b_cost = NA)
### 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("mean_b_suppnone", "sd_b_suppnone", "mean_b_tvxindiv", "sd_b_tvxindiv", "mean_b_pulvindiv", "sd_b_pulvindiv", "mean_b_techindiv","sd_b_techindiv")
# ################################################################# #
#### DEFINE RANDOM COMPONENTS ####
# ################################################################# #
### Set parameters for generating draws
apollo_draws = list(
interDrawsType = "sobol",
interNDraws = 500,
interNormDraws = c("draws_suppindiv","draws_suppcoll","draws_tvxindgp","draws_tvxcoop", "draws_cost", "draws_pulvindgp", "draws_pulvcoop", "draws_techcoll")
)
### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
randcoeff = list()
randcoeff[["b_suppindiv"]] = mean_b_suppindiv + sd_b_suppindiv * draws_suppindiv
randcoeff[["b_suppcoll"]] = mean_b_suppcoll + sd_b_suppcoll * draws_suppcoll
randcoeff[["b_tvxindgp"]] = mean_b_tvxindgp + sd_b_tvxindgp * draws_tvxindgp
randcoeff[["b_tvxcoop"]] = mean_b_tvxcoop + sd_b_tvxcoop * draws_tvxcoop
randcoeff[["b_cost"]] = -exp( mean_log_b_cost + sd_log_b_cost * draws_cost )
randcoeff[["b_pulvindgp"]] = mean_b_pulvindgp + sd_b_pulvindgp * draws_pulvindgp
randcoeff[["b_pulvcoop"]] = mean_b_pulvcoop + sd_b_pulvcoop * draws_pulvcoop
randcoeff[["b_techcoll"]] = mean_b_techcoll + sd_b_techcoll * draws_techcoll
return(randcoeff)
}
# ################################################################# #
#### GROUP AND VALIDATE INPUTS ####
# ################################################################# #
apollo_inputs = apollo_validateInputs()
# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION ####
# ################################################################# #
apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){
### Function initialisation: do not change the following three commands
### 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[["alt1"]] = mean_b_suppnone*(SUPP1==1) + b_suppindiv*(SUPP1==2) + b_suppcoll*(SUPP1==3) + mean_b_tvxindiv*(TVX1==1) + b_tvxindgp*(TVX1==2) + b_tvxcoop*(TVX1==3) + b_cost*COST1 + mean_b_pulvindiv*(PULV1==1) + b_pulvindgp*(PULV1==2) + b_pulvcoop*(PULV1==3) + mean_b_techindiv*(TECH1==1) + b_techcoll*(TECH1==2)
V[["alt2"]] = mean_b_suppnone*(SUPP2==1) + b_suppindiv*(SUPP2==2) + b_suppcoll*(SUPP2==3) + mean_b_tvxindiv*(TVX2==1) + b_tvxindgp*(TVX2==2) + b_tvxcoop*(TVX2==3) + b_cost*COST2 + mean_b_pulvindiv*(PULV2==1) + b_pulvindgp*(PULV2==2) + b_pulvcoop*(PULV2==3) + mean_b_techindiv*(TECH2==1) + b_techcoll*(TECH2==2)
V[["alt3"]] = asc_alt3 + mean_b_suppnone*(SUPP3==1)
### Define settings for MNL model component
mnl_settings = list(
alternatives = c("alt1"=1, "alt2"=2, "alt3"=3),
avail = 1,
choiceVar = prefapollo,
utilities = V
)
### Compute probabilities using MNL model
P[["model"]] = apollo_mnl(mnl_settings, functionality)
### Take product across observation for same individual
P = apollo_panelProd(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 ####
# ################################################################# #
model = apollo_estimate(apollo_beta, apollo_fixed,apollo_probabilities, apollo_inputs)