Page 1 of 1

Error in automating the MDCEV model specification

Posted: 18 Apr 2024, 02:35
by isiddhartha
I am trying to automate the MDCEV model specification. I used the apollo MDCEV example (no outside good). I am getting an error (show below). Can someone please help me fix it.

Thanks
Sid

Error in apollo_validate(mdcev_settings, modelType, functionality, apollo_inputs) :
SPECIFICATION ISSUE - Labels in "alternatives" for model component "model" do not match those in "utilities"!


# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################################################# #

### Clear memory
rm(list = ls())

### Load Apollo library
library(apollo)
library(tidyverse)

### Initialise code
apollo_initialise()

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

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

### Loading data from package
### if data is to be loaded from a file (e.g. called data.csv),
### the code would be: database = read.csv("data.csv",header=TRUE)

### for data dictionary, use ?apollo_timeUseData

database <- apollo_timeUseData %>%
mutate(dropOff = t_a01/60,
work = t_a02/60,
school = t_a03/60,
shopping = t_a04/60,
privBusiness = t_a05/60,
petrol = t_a06/60,
leisure = t_a07/60,
vacation = t_a08/60,
exercise = t_a09/60,
home = t_a10/60,
travel = t_a11/60,
other = t_a12/60)


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

alternatives <- colnames(database)[21:32]

gamma_prefix = "gamma_"
delta_prefix = "delta_"

# Create vectors for gamma and delta values
gamma_values = setNames(rep(1, length(alternatives)), paste0(gamma_prefix, alternatives))
delta_values = setNames(rep(0, length(alternatives)), paste0(delta_prefix, alternatives))

# Combine all into one vector
apollo_beta = c(alpha_base = 0,
gamma_values,
delta_values,
sig = 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("delta_home", "sig")

# ################################################################# #
#### 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 dynamically
alternatives = colnames(apollo_inputs$database)[21:32]

### Define availability dynamically
avail = lapply(alternatives, function(x) 1)
names(avail) = alternatives

### Define continuous consumption for individual alternatives dynamically
continuousChoice = sapply(alternatives, function(alt) {
eval(parse(text = paste0(alt)))
}, simplify = FALSE)

### Define utilities for individual alternatives dynamically
V = lapply(alternatives, function(alt)
{eval(parse(text = paste0("delta_", alt)))})
names(V) = alternatives

### Define alpha parameters dynamically
alpha = lapply(alternatives, function(alt) {1 / (1 + exp(-alpha_base))})
names(alpha) = alternatives

### Define gamma parameters dynamically
gamma = lapply(alternatives, function(alt) {eval(parse(text = paste0("gamma_", alt)))})
names(gamma) = alternatives

### Define costs for individual alternatives dynamically
cost = lapply(alternatives, function(alt) 1)
names(cost) = alternatives

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

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

# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name) ----
# ----------------------------------------------------------------- #

apollo_saveOutput(model)

# ################################################################# #
##### POST-PROCESSING ####
# ################################################################# #

### Print outputs of additional diagnostics to new output file (remember to close file writing when complete)
apollo_sink()

# ----------------------------------------------------------------- #
#---- MODEL PREDICTIONS ----
# ----------------------------------------------------------------- #

### Use the estimated model to make predictions
predictions_base = apollo_prediction(model, apollo_probabilities, apollo_inputs)

# ----------------------------------------------------------------- #
#---- switch off writing to file ----
# ----------------------------------------------------------------- #

apollo_sink()

Re: Error in automating the MDCEV model specification

Posted: 22 Apr 2024, 13:17
by stephanehess
Hi

I can try to debug this for you if you share the model and data file with me outside the forum

Thanks

Stephane