Important: Read this before posting to this forum

  1. 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.
  2. 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
  3. Before asking a question on the forum, users are kindly requested to follow these steps:
    1. Check that the same issue has not already been addressed in the forum - there is a search tool.
    2. 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
    3. Check the frequently asked questions section on the Apollo website, which discusses some common issues/failures. Please see http://www.apollochoicemodelling.com/faq.html
    4. Make sure that R is using the latest official release of Apollo.
  4. If the above steps do not resolve the issue, then users should follow these steps when posting a question:
    1. provide full details on the issue, including the entire code and output, including any error messages
    2. 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.

Errors when estimating the RPL in WTP space

Ask questions about errors you encouunter. Please make sure to include full details about your model specifications, and ideally your model file.
Post Reply
GYTU
Posts: 3
Joined: 12 Jun 2021, 07:23

Errors when estimating the RPL in WTP space

Post by GYTU »

Dear developers of the Apollo package,

Thanks a lot for having developed such as great package.

I'm trying to run a RPL in WTP space following the example_16.

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 ="Apollo_choice_experiment ",
modelDescr ="Mixed logit model in WTP space",
indivID ="id1",
mixing = TRUE,
nCores = 1
)

# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #


database = read.csv("C:/Users/gt340/Documents/”Apollo_data.csv",header=TRUE)


# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(
mu_log_b_price =-2,
sigma_log_b_price_inter =0,
mu_b_tenure =-1,
sigma_log_b_tenure_inter =0,
mu_b_savper =0.5,
sigma_log_b_savper_inter =0,
mu_b_time =1,
sigma_log_b_time_inter =0
)

apollo_fixed = c()
# ################################################################# #
#### DEFINE RANDOM COMPONENTS ####
# ################################################################# #

### Set parameters for generating draws




apollo_draws = list(
interDrawsType = "halton",
interNDraws = 100,
interUnifDraws = c(),
interNormDraws = c("draws_price_inter" , "draws_tenure_inter" , "draws_savper_inter","draws_time_inter"),
intraDrawsType = "mlhs",
intraNDraws = 0,
intraUnifDraws = c(),
intraNormDraws = c()
)


### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
randcoeff = list()

randcoeff[["b_price"]] = -exp( mu_log_b_price
+ sigma_log_b_price_inter * draws_price_inter )

randcoeff[["b_tenure"]] = mu_b_tenure
+ sigma_log_b_tenure_inter* draws_tenure_inter

randcoeff[["b_savper"]] = mu_b_savper
+ sigma_log_b_savper_inter * draws_savper_inter
randcoeff[["b_time"]] = mu_b_time
+ sigma_log_b_time_inter* draws_time_inter
return(randcoeff)
}


# ################################################################# #
#### GROUP AND VALIDATE INPUTS ####
# ################################################################# #

apollo_inputs = apollo_validateInputs()

apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){


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']] = b_price*(b_tenure*tenure1 + price1 + b_savper*savper1 + b_time*time1)
V[['alt2']] = b_price*(b_tenure*tenure2 + price2 + b_savper*savper2 + b_time*time2)

### Define settings for MNL model component
mnl_settings = list(
alternatives = c(alt1=1, alt2=2),
avail = list(alt1=1, alt2=1),
choiceVar = choice,
V = V
)
### Compute probabilities using MNL model
P[['model']] = apollo_mnl(mnl_settings, functionality)

### Average across intra-individual draws
### P = apollo_avgIntraDraws(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)
}
# ################################################################# #
#### MODEL ESTIMATION ####
# ################################################################# #

### Optional speedTest
#speedTest_settings=list(
# nDrawsTry = c(50, 75, 100),
# nCoresTry = 1:3,
# nRep = 10
#)

#apollo_speedTest(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs, speedTest_settings)
model = apollo_estimate(apollo_beta, apollo_fixed,
apollo_probabilities, apollo_inputs,
estimate_settings=list(hessianRoutine="maxLik"))


It started the estimation:

Code: Select all

 Starting main estimation
Initial function value: -Inf 
Initial gradient value:
            mu_log_b_price    sigma_log_b_price_inter                mu_b_tenure 
                       NaN                        NaN                        NaN 
    sigma_log_b_tenure_inter      mu_b_savper     sigma_log_b_savper_inter 
                       NaN                        NaN                        NaN 
             mu_b_time        sigma_log_b_time_inter 
                   NaN                        NaN 
Error in maxOptim(fn = fn, grad = grad, hess = hess, start = start, method = "BFGS",  : 
  NA in the initial gradient
 
Then, I have received an error message:
Error in maxOptim(fn = fn, grad = grad, hess = hess, start = start, method = "BFGS", :
NA in the initial gradient


Thank you in advance for you reply!

BTW, I tried to attach my data in the post, but I have received a message when I try to do it:"Invalid file extension: Apollo_data.csv". If it's necessary, I will send you my data.
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Errors when estimating the RPL in WTP space

Post by stephanehess »

Hi

this is a starting value issue, leading to a infinite initial LL.

Can you try with

apollo_beta = c(
mu_log_b_price =-2,
sigma_log_b_price_inter =0,
mu_b_tenure =0,
sigma_log_b_tenure_inter =0,
mu_b_savper =0,
sigma_log_b_savper_inter =0,
mu_b_time =0,
sigma_log_b_time_inter =0
)
--------------------------------
Stephane Hess
www.stephanehess.me.uk
GYTU
Posts: 3
Joined: 12 Jun 2021, 07:23

Re: Errors when estimating the RPL in WTP space

Post by GYTU »

Dear Prof.Hess,

Many thanks for your apply. I have changed the starting values to zero for all non-price parameters. It still can't converge.

I have tried with the mixlogitwtp package in Stata. The model converged with 300 halton draws. But when I try to run the model with higher numbers of halton draws, it failed to converge.

I have also tried to set the starting values according to the results that I have obtained with the mixlogitwtp package. It didn't work.
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Errors when estimating the RPL in WTP space

Post by stephanehess »

Hi

when you say it can't converge, are you getting a finite starting LL though now? If not, you can also decrease the starting value of mu_log_b_price to make it more negative so the exponential of it is closer to zero.

When you say that mixlogitwtp fails with a larger number of draws, then this could suggest another issue with your model. More draws is always more accurate, and if a model fails with more draws, then this can show an issue that was not apparent with a lower number of draws due to simulation error

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
GYTU
Posts: 3
Joined: 12 Jun 2021, 07:23

Re: Errors when estimating the RPL in WTP space

Post by GYTU »

Thank you so much for your reply. I understand better the importance of using high numbers of halton draws and I would like to know more about it: Can I use lower number of draws if I have only 4 random parameters?

I'm still trying with new starting values and different data specifications (e.g., dividing variables by 100). Since it doesn't converge, I would like to ignore the correlation between attributes to see what the WTP parameters look like (there will be 10 parameters less, hopefully it will converge). How to change the above code to do this?
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Errors when estimating the RPL in WTP space

Post by stephanehess »

Hi

your model is not allowing for correlation between the parameters, you are only estimating the means and standard deviations.

Again, when you say the model does not converge, do you mean that it fails at the starting values, or is it iterating now. I'm happy to help you, but can you please be specific and always show the input and output?

Regarding the number of draws, there is no correct answer on how many draws to use. More is always better

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Post Reply