Page 1 of 1

Error during MDCEV prediction

Posted: 21 Apr 2021, 18:17
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)

Re: Error during MDCEV prediction

Posted: 22 Apr 2021, 08:17
by stephanehess
Hi

what version of Apollo are you using?

Stephane

Re: Error during MDCEV prediction

Posted: 22 Apr 2021, 08:27
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

Re: Error during MDCEV prediction

Posted: 22 Apr 2021, 08:31
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

Re: Error during MDCEV prediction

Posted: 22 Apr 2021, 09:01
by PaulW
I sent you the data thank you very much

Re: Error during MDCEV prediction

Posted: 22 Apr 2021, 17:32
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

Re: Error during MDCEV prediction

Posted: 23 Apr 2021, 09:09
by PaulW
Thank you very much !