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