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 in automating the MDCEV model specification

Ask questions about errors you encouunter. Please make sure to include full details about your model specifications, and ideally your model file.
Post Reply
isiddhartha
Posts: 2
Joined: 19 Jan 2021, 22:51

Error in automating the MDCEV model specification

Post 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()
stephanehess
Site Admin
Posts: 1002
Joined: 24 Apr 2020, 16:29

Re: Error in automating the MDCEV model specification

Post 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
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Post Reply