Page 1 of 1
Error when using iterative coding
Posted: 11 Nov 2020, 21:23
by Thomas
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)
Re: Error when using iterative coding
Posted: 12 Nov 2020, 17:20
by stephanehess
Thomas
the line
Code: Select all
alternatives = setNames ( 40:J , names(V) ) ,
doesn't look right. You only have 3 alternatives, but would be setting an index from 40 down to 3
Stephane
Re: Error when using iterative coding
Posted: 13 Nov 2020, 08:27
by Thomas
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
Re: Error when using iterative coding
Posted: 17 Nov 2020, 12:52
by dpalma
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:
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 )
Please try that and let us know how it goes.
Cheers
David
Re: Error when using iterative coding
Posted: 21 Jan 2021, 10:32
by Thomas
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)
Re: Error when using iterative coding
Posted: 21 Jan 2021, 16:44
by dpalma
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:
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
)
Best wishes
David
Re: Error when using iterative coding
Posted: 21 Jan 2021, 17:07
by Thomas
Huge thanks David - when turning of the derivates it now also works on my own data.
Best
/Thomas