Hi,
I have RP data with a large number of alternatives. I have therefore used the interative coding which is mentioned in the manual, chapter 11. Unfortunately, I get an error which I cannot resolve and therefore I ask the forum for help.
My iterative code looks like this (for simplicity I here only use three alternatives and only estimate the distance parameter):
J<-3
V = list()
avail=list()
for(j in 1:J){
V[[paste0("alt",j)]] = b_distance*get(paste0("NEAR_DIST_",j))
avail[[paste0("alt",j)]] = get(paste0("av_",1:J)) }
mnl_settings = list (
alternatives = setNames ( 40:J , names(V) ) ,
avail=avail,
choiceVar = choice ,
V = V )
When running the line 'model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)', I get this error message:
Testing likelihood functionError in apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, :
Parameter b_distance does not influence the log-likelihood of your model!
However, if I 'hard code' the utility lines (see below), the models runs and converges perfectly. Any idea what's wrong?
Best,
Thomas
V[['alt1']] = b_distance * NEAR_DIST_1
V[['alt2']] = b_distance * NEAR_DIST_2
V[['alt3']] = b_distance * NEAR_DIST_3
mnl_settings = list(
alternatives = c(alt1=1,alt2=2,alt3=3),
avail = list(alt1=av_1,alt2=av_2,alt3=av_3),
choiceVar = choice,
V = V)
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.
Error when using iterative coding
-
- Site Admin
- Posts: 1042
- Joined: 24 Apr 2020, 16:29
Re: Error when using iterative coding
Thomas
the line
doesn't look right. You only have 3 alternatives, but would be setting an index from 40 down to 3
Stephane
the line
Code: Select all
alternatives = setNames ( 40:J , names(V) ) ,
Stephane
Re: Error when using iterative coding
Hi Stephane,
Sorry, that's a leftover from testing whether the problem was related to first 40 alternatives. It should naturally be set to 1. It does, however, not influence the error message, which still says that b_distance is not influencing the log-likelihood.
Cheers
/Thomas
Sorry, that's a leftover from testing whether the problem was related to first 40 alternatives. It should naturally be set to 1. It does, however, not influence the error message, which still says that b_distance is not influencing the log-likelihood.
Cheers
/Thomas
Re: Error when using iterative coding
Hi Thomas,
It is hard to say for sure what is the issue, but I believe there is a problem with your definition of availabilities inside the loop. I would change it to the following:
Please try that and let us know how it goes.
Cheers
David
It is hard to say for sure what is the issue, but I believe there is a problem with your definition of availabilities inside the loop. I would change it to the following:
Code: Select all
J = 3
V = list()
avail = list()
for(j in 1:J){
V[[paste0("alt",j)]] = b_distance*get(paste0("NEAR_DIST_",j))
avail[[paste0("alt",j)]] = get(paste0("av_",j)) # this line changed
}
mnl_settings = list (
alternatives = setNames ( 1:J , names(V) ),
avail = avail,
choiceVar = choice,
V = V )
Cheers
David
Re: Error when using iterative coding
Hi David/Stephane,
Sorry for the long delay in response. Unfortunately, your suggestion is not solving the issue. I have tried the loop code on a number of other data sets but still get the same error message. I have then tried it at one of your provided example data and still it doesn't work for me. Please see below a very simple model on the drug choice data where I can estimate a model if I write the utility lines for all 4 alternatives (included in the code but as comments) but not if I use the loop. The error message I get is:
Error in apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, :
Parameter b_risk does not influence the log-likelihood of your model!
Best
/Thomas
rm(list = ls())
### Load Apollo library
library(apollo)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "Test loop for alternatives",
modelDescr = "Simple model on drug choice data, ",
indivID = "ID"
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
database = read.csv("apollo_drugChoiceData.csv",header=TRUE)
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(b_risk = 0,
b_price = 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()
# V[['alt1']] = b_risk*side_effects_1 + b_price*price_1
# V[['alt2']] = b_risk*side_effects_2 + b_price*price_2
# V[['alt3']] = b_risk*side_effects_3 + b_price*price_3
# V[['alt4']] = b_risk*side_effects_4 + b_price*price_4
#
V = list()
for(j in 1:4){
V[[paste0("alt",j)]] = (b_risk*get(paste0("side_effects_",j))+b_price*get(paste0("price_",j)))}
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(alt1=1, alt2=2, alt3=3, alt4=4),
avail = list(alt1=1, alt2=1, alt3=1, alt4=1),
choiceVar = best,
V = 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)
### 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)
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #
apollo_modelOutput(model)
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name) ----
# ----------------------------------------------------------------- #
apollo_saveOutput(model)
Sorry for the long delay in response. Unfortunately, your suggestion is not solving the issue. I have tried the loop code on a number of other data sets but still get the same error message. I have then tried it at one of your provided example data and still it doesn't work for me. Please see below a very simple model on the drug choice data where I can estimate a model if I write the utility lines for all 4 alternatives (included in the code but as comments) but not if I use the loop. The error message I get is:
Error in apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, :
Parameter b_risk does not influence the log-likelihood of your model!
Best
/Thomas
rm(list = ls())
### Load Apollo library
library(apollo)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "Test loop for alternatives",
modelDescr = "Simple model on drug choice data, ",
indivID = "ID"
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
database = read.csv("apollo_drugChoiceData.csv",header=TRUE)
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(b_risk = 0,
b_price = 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()
# V[['alt1']] = b_risk*side_effects_1 + b_price*price_1
# V[['alt2']] = b_risk*side_effects_2 + b_price*price_2
# V[['alt3']] = b_risk*side_effects_3 + b_price*price_3
# V[['alt4']] = b_risk*side_effects_4 + b_price*price_4
#
V = list()
for(j in 1:4){
V[[paste0("alt",j)]] = (b_risk*get(paste0("side_effects_",j))+b_price*get(paste0("price_",j)))}
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(alt1=1, alt2=2, alt3=3, alt4=4),
avail = list(alt1=1, alt2=1, alt3=1, alt4=1),
choiceVar = best,
V = 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)
### 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)
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #
apollo_modelOutput(model)
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name) ----
# ----------------------------------------------------------------- #
apollo_saveOutput(model)
Re: Error when using iterative coding
Hi Thomas,
You have run into a bug. Since v0.2.0 Apollo speeds up estimation using analytical derivatives (gradients) for MNL and other models. However, due to the way they are implemented, analytical derivatives are incompatible with loops inside apollo_probabilities. We hope to solve this issue in the next release, but for now, I would recommend turning off the analytical derivatives by setting apollo_control$analyticGrad = FALSE.If you do that, the example you posted works fine. The relevant piece of code is copied below:
Best wishes
David
You have run into a bug. Since v0.2.0 Apollo speeds up estimation using analytical derivatives (gradients) for MNL and other models. However, due to the way they are implemented, analytical derivatives are incompatible with loops inside apollo_probabilities. We hope to solve this issue in the next release, but for now, I would recommend turning off the analytical derivatives by setting apollo_control$analyticGrad = FALSE.If you do that, the example you posted works fine. The relevant piece of code is copied below:
Code: Select all
### Set core controls
apollo_control = list(
modelName = "Test loop for alternatives",
modelDescr = "Simple model on drug choice data, ",
indivID = "ID",
analyticGrad = FALSE # this line is new
)
David
Re: Error when using iterative coding
Huge thanks David - when turning of the derivates it now also works on my own data.
Best
/Thomas
Best
/Thomas