Hi,
I am trying to run hybrid choice model for a choice experiment data. I followed your example code and manual to set up my model. Unfortunately, I keep getting error message "Log-likelihood calculation fails at values close to the starting values!
In addition: Warning message:
In log(P[[j]]) : NaNs produced"
When I tried "apollo_beta=apollo_searchStart(apollo_beta, apollo_fixed,apollo_probabilities, apollo_inputs)" to find initial values of beta, I get the following error message:
"Testing probability function (apollo_probabilities)
Error: node stack overflow
Execution halted"
Could you please give me suggestions what I can do to address these? If required, I can share some description of the data and code. Thank you.
Important: Read this before posting to this forum
- 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.
- 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
- Before asking a question on the forum, users are kindly requested to follow these steps:
- Check that the same issue has not already been addressed in the forum - there is a search tool.
- 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
- Check the frequently asked questions section on the Apollo website, which discusses some common issues/failures. Please see http://www.apollochoicemodelling.com/faq.html
- Make sure that R is using the latest official release of Apollo.
- Users can check which version they are running by entering packageVersion("apollo").
- Then check what is the latest full release (not development version) at http://www.ApolloChoiceModelling.com/code.html.
- To update to the latest official version, just enter install.packages("apollo"). To update to a development version, download the appropriate binary file from http://www.ApolloChoiceModelling.com/code.html, and install the package from file
- If the above steps do not resolve the issue, then users should follow these steps when posting a question:
- provide full details on the issue, including the entire code and output, including any error messages
- 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.
/Hybrid choice model_Esitmation Issues
Re: /Hybrid choice model_Esitmation Issues
Hi,
Could you please share your code? It is hard to diagnose without looking at the actual code.
Cheers
David
Could you please share your code? It is hard to diagnose without looking at the actual code.
Cheers
David
Re: /Hybrid choice model_Esitmation Issues
Hi,
Thank you very much for the reply. Please see my code below:
### Description of variables
#Choice variable: binary: Status quo (SQ or S), Alternatives (Alt or A)
#Attributes: WeaAbsLin SpeAbsLin NatAbsLin Levy
#Explanatory variables: HHsize, IncCat_Mid, IncCat_High
#Indicator variable: attitude_Epl (rank 1 to 7)
### Clear memory
rm(list = ls())
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
### Load Apollo library
#install.packages("apollo", repos = "http://cran.us.r-project.org")
library(apollo)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "hybrid_model_WTP",
modelDescr = "Hybrid choice model on LCF_WTP",
indivID = "id",
mixing = TRUE,
nCores = 25
)
load("database.RData")
# ################################################################# #
# ################################################################# #
#### ANALYSIS OF CHOICES ####
# ################################################################# #
choiceAnalysis_settings <- list(
alternatives = c(SQ="S",Alt="A"),
avail = list(SQ=1, Alt=1),
choiceVar = database$Choice,
explanators = database[,c("HHsize", "IncCat_Mid", "IncCat_High" )]
)
apollo_choiceAnalysis(choiceAnalysis_settings, apollo_control, database)
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta <- c(b_Wea = 0,
b_Spe = 0,
b_Nat = 0,
b_Levy = 0,
b_IncMid = 0,
b_IncHigh = 0,
gamma_Wea_HHsize = 0,gamma_Spe_HHsize = 0,
gamma_Nat_HHsize = 0,
gamma_Wea_MidI = 0,gamma_Spe_MidI = 0,
gamma_Nat_MidI = 0,
gamma_Wea_HighI = 0,gamma_Spe_HighI = 0,
gamma_Nat_HighI = 0,
gamma_LV_HHsize = 0, gamma_LV_MidI = 0,
gamma_LV_HighI = 0,
mu_log_asc = 0,
sigma_log_asc = 1,lambda= 1,
zeta_Epl=1,
tau_Epl_1 =-3, tau_Epl_2 =-3,
tau_Epl_3 = -1, tau_Epl_4 = 1,tau_Epl_5 = 2,
tau_Epl_6 = 3
)
apollo_fixed <- c("mu_log_asc", "sigma_log_asc")
# ################################################################# #
#### DEFINE RANDOM COMPONENTS ####
# ################################################################# #
### Set parameters for generating draws
apollo_draws = list(
interDrawsType="halton",
interNDraws=500,
interNormDraws=c("draws_asc_inter","eta")
)
### Create random parameters
apollo_randCoeff <- function(apollo_beta, apollo_inputs) {
randcoef <- list()
randcoef[['asc']] <- mu_log_asc + b_IncMid * (IncCat=='Mid') + b_IncHigh * (IncCat=='High')-sigma_log_asc*draws_asc_inter
randcoef[["LV"]] = gamma_LV_HHsize*HHsize + gamma_LV_MidI*IncCat_Mid+ gamma_LV_HighI*IncCat_High + eta
return(randcoef)
}
# ################################################################# #
#### GROUP AND VALIDATE INPUTS ####
# ################################################################# #
apollo_inputs = apollo_validateInputs()
# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION ####
# ################################################################# #
#options(expressions=500000) ## avoid "node stack overflow" error
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()
### Create alternative specific constants
asc_SQ_value = 0
asc_Alt_value = asc
### Likelihood of choices
### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V <- list()
V[['SQ']] <- asc_SQ_value +
b_Wea * WeaAbsLin_S + b_Spe * SpeAbsLin_S + b_Nat * NatAbsLin_S+ lambda*LV
V[['Alt']] <- asc_Alt_value +
b_Wea * WeaAbsLin_A + b_Spe * SpeAbsLin_A + b_Nat* NatAbsLin_A + b_Levy * Lev_A + lambda*LV
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(SQ="S",Alt="A"),
avail = list(SQ=1, Alt=1),
choiceVar = Choice,
V = V
)
### initial values of beta: when I use this I find error message "node stack overflow"
#apollo_beta=apollo_searchStart(apollo_beta, apollo_fixed,apollo_probabilities, apollo_inputs)
#apollo_beta=apollo_bootstrap(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
### Compute probabilities for MNL model component
P[["Choice"]] = apollo_mnl(mnl_settings, functionality)
### Likelihood of indicators
ol_settings1 = list(outcomeOrdered=attitude_Epl,
V=zeta_Epl*LV,
tau=c(tau_Epl_1, tau_Epl_2, tau_Epl_3, tau_Epl_4,tau_Epl_5,tau_Epl_6),
rows=(task==1))
P[["indic_Epl"]] = apollo_ol(ol_settings1, functionality)
### Likelihood of the whole model
P = apollo_combineModels(P, apollo_inputs, 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)
}
# ################################################################# #
#### CHECK FOR COMPUTATIONAL REQUIREMENTS ####
# ################################################################# #
speedTest_settings=list(
nDrawsTry = c(250, 500, 1000),
nCoresTry = 1:3,
nRep = 10
)
apollo_speedTest(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs, speedTest_settings)
# ################################################################# #
#### MODEL ESTIMATION ####
# ################################################################# #
### Estimate model
model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #
apollo_modelOutput(model)
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name) ----
# ----------------------------------------------------------------- #
apollo_saveOutput(model)
Thank you very much for the reply. Please see my code below:
### Description of variables
#Choice variable: binary: Status quo (SQ or S), Alternatives (Alt or A)
#Attributes: WeaAbsLin SpeAbsLin NatAbsLin Levy
#Explanatory variables: HHsize, IncCat_Mid, IncCat_High
#Indicator variable: attitude_Epl (rank 1 to 7)
### Clear memory
rm(list = ls())
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
### Load Apollo library
#install.packages("apollo", repos = "http://cran.us.r-project.org")
library(apollo)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "hybrid_model_WTP",
modelDescr = "Hybrid choice model on LCF_WTP",
indivID = "id",
mixing = TRUE,
nCores = 25
)
load("database.RData")
# ################################################################# #
# ################################################################# #
#### ANALYSIS OF CHOICES ####
# ################################################################# #
choiceAnalysis_settings <- list(
alternatives = c(SQ="S",Alt="A"),
avail = list(SQ=1, Alt=1),
choiceVar = database$Choice,
explanators = database[,c("HHsize", "IncCat_Mid", "IncCat_High" )]
)
apollo_choiceAnalysis(choiceAnalysis_settings, apollo_control, database)
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta <- c(b_Wea = 0,
b_Spe = 0,
b_Nat = 0,
b_Levy = 0,
b_IncMid = 0,
b_IncHigh = 0,
gamma_Wea_HHsize = 0,gamma_Spe_HHsize = 0,
gamma_Nat_HHsize = 0,
gamma_Wea_MidI = 0,gamma_Spe_MidI = 0,
gamma_Nat_MidI = 0,
gamma_Wea_HighI = 0,gamma_Spe_HighI = 0,
gamma_Nat_HighI = 0,
gamma_LV_HHsize = 0, gamma_LV_MidI = 0,
gamma_LV_HighI = 0,
mu_log_asc = 0,
sigma_log_asc = 1,lambda= 1,
zeta_Epl=1,
tau_Epl_1 =-3, tau_Epl_2 =-3,
tau_Epl_3 = -1, tau_Epl_4 = 1,tau_Epl_5 = 2,
tau_Epl_6 = 3
)
apollo_fixed <- c("mu_log_asc", "sigma_log_asc")
# ################################################################# #
#### DEFINE RANDOM COMPONENTS ####
# ################################################################# #
### Set parameters for generating draws
apollo_draws = list(
interDrawsType="halton",
interNDraws=500,
interNormDraws=c("draws_asc_inter","eta")
)
### Create random parameters
apollo_randCoeff <- function(apollo_beta, apollo_inputs) {
randcoef <- list()
randcoef[['asc']] <- mu_log_asc + b_IncMid * (IncCat=='Mid') + b_IncHigh * (IncCat=='High')-sigma_log_asc*draws_asc_inter
randcoef[["LV"]] = gamma_LV_HHsize*HHsize + gamma_LV_MidI*IncCat_Mid+ gamma_LV_HighI*IncCat_High + eta
return(randcoef)
}
# ################################################################# #
#### GROUP AND VALIDATE INPUTS ####
# ################################################################# #
apollo_inputs = apollo_validateInputs()
# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION ####
# ################################################################# #
#options(expressions=500000) ## avoid "node stack overflow" error
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()
### Create alternative specific constants
asc_SQ_value = 0
asc_Alt_value = asc
### Likelihood of choices
### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V <- list()
V[['SQ']] <- asc_SQ_value +
b_Wea * WeaAbsLin_S + b_Spe * SpeAbsLin_S + b_Nat * NatAbsLin_S+ lambda*LV
V[['Alt']] <- asc_Alt_value +
b_Wea * WeaAbsLin_A + b_Spe * SpeAbsLin_A + b_Nat* NatAbsLin_A + b_Levy * Lev_A + lambda*LV
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(SQ="S",Alt="A"),
avail = list(SQ=1, Alt=1),
choiceVar = Choice,
V = V
)
### initial values of beta: when I use this I find error message "node stack overflow"
#apollo_beta=apollo_searchStart(apollo_beta, apollo_fixed,apollo_probabilities, apollo_inputs)
#apollo_beta=apollo_bootstrap(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
### Compute probabilities for MNL model component
P[["Choice"]] = apollo_mnl(mnl_settings, functionality)
### Likelihood of indicators
ol_settings1 = list(outcomeOrdered=attitude_Epl,
V=zeta_Epl*LV,
tau=c(tau_Epl_1, tau_Epl_2, tau_Epl_3, tau_Epl_4,tau_Epl_5,tau_Epl_6),
rows=(task==1))
P[["indic_Epl"]] = apollo_ol(ol_settings1, functionality)
### Likelihood of the whole model
P = apollo_combineModels(P, apollo_inputs, 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)
}
# ################################################################# #
#### CHECK FOR COMPUTATIONAL REQUIREMENTS ####
# ################################################################# #
speedTest_settings=list(
nDrawsTry = c(250, 500, 1000),
nCoresTry = 1:3,
nRep = 10
)
apollo_speedTest(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs, speedTest_settings)
# ################################################################# #
#### MODEL ESTIMATION ####
# ################################################################# #
### Estimate model
model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #
apollo_modelOutput(model)
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name) ----
# ----------------------------------------------------------------- #
apollo_saveOutput(model)
Re: /Hybrid choice model_Esitmation Issues
Hi,
It seems the starting value of the OL thresholds are invalid. You have:
tau_Epl_1 =-3, tau_Epl_2 =-3,
tau_Epl_3 = -1, tau_Epl_4 = 1,tau_Epl_5 = 2,
tau_Epl_6 = 3
But they need to be all different and increasing, so if you change it to:
tau_Epl_1 =-3, tau_Epl_2 =-2,
tau_Epl_3 = -1, tau_Epl_4 = 1,tau_Epl_5 = 2,
tau_Epl_6 = 3
It should work.
Also, if you define the thresholds as a list instead of a vector, the model might run faster, as follows.
ol_settings1 = list(outcomeOrdered=attitude_Epl,
V=zeta_Epl*LV,
tau=list(tau_Epl_1, tau_Epl_2, tau_Epl_3, tau_Epl_4,tau_Epl_5,tau_Epl_6),
rows=(task==1))
Best
David
It seems the starting value of the OL thresholds are invalid. You have:
tau_Epl_1 =-3, tau_Epl_2 =-3,
tau_Epl_3 = -1, tau_Epl_4 = 1,tau_Epl_5 = 2,
tau_Epl_6 = 3
But they need to be all different and increasing, so if you change it to:
tau_Epl_1 =-3, tau_Epl_2 =-2,
tau_Epl_3 = -1, tau_Epl_4 = 1,tau_Epl_5 = 2,
tau_Epl_6 = 3
It should work.
Also, if you define the thresholds as a list instead of a vector, the model might run faster, as follows.
ol_settings1 = list(outcomeOrdered=attitude_Epl,
V=zeta_Epl*LV,
tau=list(tau_Epl_1, tau_Epl_2, tau_Epl_3, tau_Epl_4,tau_Epl_5,tau_Epl_6),
rows=(task==1))
Best
David
Re: /Hybrid choice model_Esitmation Issues
Hi David,
It works. Thank you very much for your help.
Zabid
It works. Thank you very much for your help.
Zabid