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.

Error during MDCEV prediction

Report bugs or highlight issues with Apollo functions. At a minimum, please include the part of the output where you believe the bug is manifested. Ideally, please share your model file.
Post Reply
PaulW
Posts: 5
Joined: 15 Mar 2021, 14:38

Error during MDCEV prediction

Post by PaulW »

Dear Apollo team,

I'm encountering the following error while using apollo_prediction for my MDCEV model:

Error in if (M > sum(phk > lambda) || M > sum(phk > 0)) { :
missing value where TRUE/FALSE needed

or in version 0.1.0:

Error in if ((sum(lambda < V_trans[i, 2:NALT])) < (M)) { :
missing value where TRUE/FALSE needed

The code itself is almost identical to that of Apollo example 11 but on a different dataset having 611 observations. The error seems to occur in 'forecasting_subroutine'. Do you have any idea if/how I can fix this ?

Code:

### Load Apollo library
library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
modelName ="First test",
modelDescr ="MDCEV model on time use data, alpha-gamma profile, outside good and constants only in utilities",
indivID ="indivID"
)

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

database = data_ALL
database$outside = database$other + 0.25

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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(alpha_base = 0,
gamma_tv = 1,
gamma_streaming = 1,
gamma_dvd = 1,
gamma_avod = 1,
gamma_gaming = 1,
delta_tv = 0,
delta_streaming = 0,
delta_dvd = 0,
delta_avod = 0,
delta_gaming = 0,
sigma = 1)

### 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("sigma")

# ################################################################# #
#### 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()

### Define individual alternatives
alternatives = c("outside",
"tv",
"streaming",
"dvd",
"avod",
"gaming")

### Define availabilities
avail = list(outside = 1,
tv = 1,
streaming = 1,
dvd = 1,
avod = 1,
gaming = 1)

### Define continuous consumption for individual alternatives
continuousChoice = list(outside = outside,
tv = tv,
streaming = streaming,
dvd = dvd,
avod = avod,
gaming = gaming)

### Define utilities for individual alternatives
V = list()
V[["outside"]] = 0
V[["tv"]] = delta_tv
V[["streaming"]] = delta_streaming
V[["dvd"]] = delta_dvd
V[["avod"]] = delta_avod
V[["gaming"]] = delta_gaming


### Define alpha parameters
alpha = list(outside = 1 /(1 + exp(-alpha_base)),
tv = 1 /(1 + exp(-alpha_base)),
streaming = 1 /(1 + exp(-alpha_base)),
dvd = 1 /(1 + exp(-alpha_base)),
avod = 1 /(1 + exp(-alpha_base)),
gaming = 1 /(1 + exp(-alpha_base)))

### Define gamma parameters
gamma = list(tv = gamma_tv,
streaming = gamma_streaming,
dvd = gamma_dvd,
avod = gamma_avod,
gaming = gamma_gaming)

### Define costs for individual alternatives
cost = list(outside = 1,
tv = 1,
streaming = 1,
dvd = 1,
avod = 1,
gaming = 1)

### Define budget
budget = 28.25

### Define settings for MDCEV model
mdcev_settings <- list(alternatives = alternatives,
avail = avail,
continuousChoice = continuousChoice,
V = V,
alpha = alpha,
gamma = gamma,
sigma = sigma,
cost = cost,
budget = budget)

### Compute probabilities using MDCEV model
P[["model"]] = apollo_mdcev(mdcev_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)

predictions_base = apollo_prediction(model, apollo_probabilities, apollo_inputs)
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Error during MDCEV prediction

Post by stephanehess »

Hi

what version of Apollo are you using?

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
PaulW
Posts: 5
Joined: 15 Mar 2021, 14:38

Re: Error during MDCEV prediction

Post by PaulW »

I tested it in version 0.1.0 and 0.2.4

version 0.1.0:
Error in if ((sum(lambda < V_trans[i, 2:NALT])) < (M)) { :
missing value where TRUE/FALSE needed

version 0.2.4:
Error in if (M > sum(phk > lambda) || M > sum(phk > 0)) { :
missing value where TRUE/FALSE needed

Kind regards,
Paul
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Error during MDCEV prediction

Post by stephanehess »

Paul

thanks. We will look into this. If it would be possible for you, it would be helpful if you could share your code and data with us outside the forum

Thanks

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
PaulW
Posts: 5
Joined: 15 Mar 2021, 14:38

Re: Error during MDCEV prediction

Post by PaulW »

I sent you the data thank you very much
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Error during MDCEV prediction

Post by stephanehess »

Paul

you found a bug, thanks.

The issue was that your budget was a scalar, rather than a vector, which is of course fine in estimation, but we hadn't catered for that in prediction. We have fixed this in v.0.2.5 which you can download from www.apollochoicemodelling.com

Alternatively, you can also change your line

budget = 28.25

to

budget = rep(28.25,nrow(apollo_inputs$database))

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
PaulW
Posts: 5
Joined: 15 Mar 2021, 14:38

Re: Error during MDCEV prediction

Post by PaulW »

Thank you very much !
Post Reply