Page 1 of 1

Error in estimating MDCEV model with RP&SP data

Posted: 17 Feb 2023, 18:08
by Aditya249
Dear Sir,

For my research work, I need to develop models with pooled RP&SP data. Considering this, I wrote a code for the MDCEV model with RP & SP data.

To practice and learn the codes, I have utilized the data from the MDCEV model (without outside good) and RP SP MNL model from Apollo examples.
Actually, I have added four more attributes (RP, SP, RP Journey, and SP task) to the MDCEV data.

However, I am getting the following error during model estimation: Error in apollo_preprocess(mdcev_settings, modelType, functionality, apollo_inputs) :
"continuousChoice" for model component "MDCEV" should be a list of numerical vectors, each with as many rows as observations.
.

I would request you to please suggest on how can I fix this code. I am using apollo version 0.2.8.

My code is as follows:

Code: Select all

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

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

### Load Apollo library
library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName       = "MDCEV_RP_SP",
  modelDescr      = "MDCEV model using RP and SP data",
  indivID         = "indivID", 
  outputDirectory = "output"
)


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

database = read.csv("MDCEV_RP_SP_Dummy.csv")


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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(alpha_base         = 0,
                gamma_dropOff      = 1,
                gamma_work         = 1,
                gamma_school       = 1,
                gamma_shopping     = 1,
                gamma_privBusiness = 1,
                gamma_petrol       = 1,
                gamma_leisure      = 1,
                gamma_vacation     = 1,
                gamma_exercise     = 1,
                gamma_home         = 1,
                gamma_travel       = 1,
                gamma_other        = 1,
                delta_dropOff      = 0,
                delta_work         = 0,
                delta_school       = 0,
                delta_shopping     = 0,
                delta_privBusiness = 0,
                delta_petrol       = 0,
                delta_leisure      = 0,
                delta_vacation     = 0,
                delta_exercise     = 0,
                delta_home         = 0,
                delta_travel       = 0,
                delta_other        = 0,
                delta_female       = 0,
                delta_age_above_30 = 0,
                mu_RP              = 1,
                mu_SP              = 1,
                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", "mu_RP")

# ################################################################# #
#### 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("dropOff", 
                   "work", 
                   "school", 
                   "shopping", 
                   "privBusiness", 
                   "petrol", 
                   "leisure", 
                   "vacation", 
                   "exercise", 
                   "home", 
                   "travel", 
                   "other")

 ### Define availabilities
  avail = list(dropOff      = 1, 
               work         = 1,
               school       = 1,
               shopping     = 1,
               privBusiness = 1,
               petrol       = 1, 
               leisure      = 1,
               vacation     = 1,
               exercise     = 1,
               home         = 1,
               travel       = 1,
               other        = 1)

### Define continuous consumption for individual alternatives
  continuousChoice = list(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 utilities for individual alternatives
  V = list()
  V[["dropOff" ]]     = delta_dropOff + delta_female*female
  V[["work"    ]]     = delta_work    + delta_female*female + delta_age_above_30*age
  V[["school"  ]]     = delta_school  
  V[["shopping"]]     = delta_shopping + delta_female*female
  V[["privBusiness"]] = delta_privBusiness + delta_female*female
  V[["petrol"  ]]     = delta_petrol  
  V[["leisure" ]]     = delta_leisure + delta_age_above_30*age
  V[["vacation"]]     = delta_vacation
  V[["exercise"]]     = delta_exercise  + delta_female*female + delta_age_above_30*age
  V[["home"    ]]     = delta_home    
  V[["travel"  ]]     = delta_travel  
  V[["other"   ]]     = delta_other


 ### Define alpha parameters
  alpha = list(dropOff  = 1 /(1 + exp(-alpha_base)), 
               work     = 1 /(1 + exp(-alpha_base)), 
               school   = 1 /(1 + exp(-alpha_base)), 
               shopping = 1 /(1 + exp(-alpha_base)), 
               privBusiness = 1 /(1 + exp(-alpha_base)),
               petrol   = 1 /(1 + exp(-alpha_base)),
               leisure  = 1 /(1 + exp(-alpha_base)), 
               vacation = 1 /(1 + exp(-alpha_base)), 
               exercise = 1 /(1 + exp(-alpha_base)), 
               home     = 1 /(1 + exp(-alpha_base)), 
               travel   = 1 /(1 + exp(-alpha_base)),
               other    = 1 /(1 + exp(-alpha_base)))

  ### Define gamma parameters
  gamma = list(dropOff      = gamma_dropOff, 
               work         = gamma_work,
               school       = gamma_school,
               shopping     = gamma_shopping,
               privBusiness = gamma_privBusiness,
               petrol       = gamma_petrol, 
               leisure      = gamma_leisure,
               vacation     = gamma_vacation,
               exercise     = gamma_exercise,
               home         = gamma_home,
               travel       = gamma_travel,
               other        = gamma_other)
  
  ### Define costs for individual alternatives
  cost = list(dropOff      = 1, 
              work         = 1,
              school       = 1,
              shopping     = 1,
              privBusiness = 1,
              petrol       = 1, 
              leisure      = 1,
              vacation     = 1,
              exercise     = 1,
              home         = 1,
              travel       = 1,
              other        = 1)



  ### Compute probabilities for the RP part of the data using MDCEV model
  mdcev_settings_RP = list(
                           alternatives      = alternatives,
                           avail             = avail,
                           continuousChoice  = continuousChoice,
                           alpha             = alpha,
                           gamma             = gamma, 
                           sigma             = sig, 
                           cost              = cost,
                           budget            = 24,
                           utilities         = list(dropOff  = mu_RP*V[["dropOff"]],
                                              work           = mu_RP*V[["work"]],
                                              school         = mu_RP*V[["school"]],
                                              privBusiness   = mu_RP*V[["privBusiness"]],
                                              petrol         = mu_RP*V[["petrol"]],
                                              leisure        = mu_RP*V[["leisure"]],
                                              vacation       = mu_RP*V[["vacation"]],
                                              exercise       = mu_RP*V[["exercise"]],
                                              home           = mu_RP*V[["home"]],
                                              travel         = mu_RP*V[["travel"]],
                                              other          = mu_RP*V[["other"]]),
                         rows          = (RP==1)
)

  P[["RP"]] = apollo_mdcev(mdcev_settings_RP, functionality)


  ### Compute probabilities for the SP part of the data using MDCEV model
  mdcev_settings_SP = list(
                           alternatives      = alternatives,
                           avail             = avail,
                           continuousChoice  = continuousChoice,
                           alpha             = alpha,
                           gamma             = gamma, 
                           sigma             = sig, 
                           cost              = cost,
                           budget            = 24,
                           utilities         = list(dropOff  = mu_SP*V[["dropOff"]],
                                              work           = mu_SP*V[["work"]],
                                              school         = mu_SP*V[["school"]],
                                              privBusiness   = mu_SP*V[["privBusiness"]],
                                              petrol         = mu_SP*V[["petrol"]],
                                              leisure        = mu_SP*V[["leisure"]],
                                              vacation       = mu_SP*V[["vacation"]],
                                              exercise       = mu_SP*V[["exercise"]],
                                              home           = mu_SP*V[["home"]],
                                              travel         = mu_SP*V[["travel"]],
                                              other          = mu_SP*V[["other"]]),
                         rows          = (SP==1)

)
  
 P[["SP"]] = apollo_mdcev(mdcev_settings_RP, functionality)   



 ### Combined model
  P = apollo_combineModels(P, apollo_inputs, 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)

Re: Error in estimating MDCEV model with RP&SP data

Posted: 06 Mar 2023, 09:12
by stephanehess
Please share the code and data with me offline via e-mail and I'll look into it: stephane.hess@gmail.com

Re: Error in estimating MDCEV model with RP&SP data

Posted: 06 Mar 2023, 16:39
by Aditya249
Thank You Prof. Hess.
I have shared the data and code at the mentioned email.

Re: Error in estimating MDCEV model with RP&SP data

Posted: 08 Mar 2023, 10:21
by stephanehess
Hi

the error message you got was not helpful (and we will fix that), but there are two issues in your code that led to it

First, you are missing shopping from the utilities inside mdcev_settings_RP and mdcev_settings_SP
Second, you were using mdcev_settings_RP for SP too

Stephane

Re: Error in estimating MDCEV model with RP&SP data

Posted: 08 Mar 2023, 11:05
by Aditya249
Thank you Prof. Hess for solving the issue. The code is working fine now. 
Thank you for developing this amazing and powerful package 'Apollo'.