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.

Unexpected numeric error while estimating eMDC2 model

Ask questions about errors you encouunter. Please make sure to include full details about your model specifications, and ideally your model file.
Post Reply
Pigouvian
Posts: 8
Joined: 23 Jun 2023, 19:26

Unexpected numeric error while estimating eMDC2 model

Post by Pigouvian »

Dear Prof. Hess and Dr. Palma,

I am working on estimating an eMDC2 model that incorporates an implicit budget. The goal is to analyze travel time spent on trip purposes based on individuals' socio-economic profiles and I am not considering any outside good in my model estimation.

However, I am getting the following error message: unexpected numeric constant.

Could you please check my code and fix the issue?

My code is as follows:

Code: Select all

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

### Clear memory and initialise
rm(list = ls())
library(apollo)
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName  ="eMDC_without_budget",
  modelDescr ="Extended MDC with complementarity and substitution, with unobserved budget and socio-demographics",
  indivID    ="UserID",
  outputDirectory="output"
)

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

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

### Randomly split dataset into estimation (70%) and validation (30%)
set.seed(1)
database$validation <- runif(nrow(database))>0.7
dbVal    <- database[ database$validation,] # validation sample
database <- database[!database$validation,] # estimation sample

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

### Parameters starting values c(name1=value1, name2=value2, ...)
apollo_beta = c(
                # Base utility of inside goods

                bBusiness_trip     = 0,                 
                bWork_trip         = 0,                
                bRestaurant_trip   = 0,
                bHospital_trip     = 0,
                bLeisure_trip      = 0,
                bAltruistic_trip   = 0,
                bShopping_trip     = 0,
                bStrolling         = 0,

                bBusiness_trip_Female             = 0,  
                bBusiness_Age_30_49               = 0, 
                bBusiness_Age_above_50            = 0, 
                bBusiness_trip_Mode_choice_car    = 0,
                bBusiness_trip_Mode_choice_train  = 0,
                bBusiness_trip_Car_ownership      = 0,

                bWork_trip_Female                 = 0,  
                bWork_Age_30_49                   = 0, 
                bWork_Age_above_50                = 0, 
                bWork_trip_Mode_choice_car        = 0,
                bWork_trip_Mode_choice_subway     = 0,
                bWork_trip_Mode_choice_train      = 0,

                bRestaurant_trip_Female            = 0,
                bRestaurant_Housewife_Househusband = 0,
                bRestaurant_Car_ownership          = 0,
                bRestaurant_Unemployed             = 0,
                bRestaurant_Mode_choice_car        = 0,

                bHospital_trip_Age_above_50           = 0,
                bHospital_trip_Female                 = 0,
                bHospital_trip_Housewife_Househusband = 0,

                bLeisure_trip_Female                  = 0,
                bLeisure_trip_Mode_choice_train       = 0,

                bAltruistic_trip_Female                 = 0,
                bAltruistic_Unemployed                  = 0,
                bAltruistic_Housewife_Househusband      = 0,
                bAltruistic_Part_time                   = 0,

                bShopping_trip_Female                      = 0,
                bShopping_trip_Housewife_Househusband      = 0,

                bStrolling_Housewife_Househusband    = 0,
                bStrolling_Age_above_50              = 0,
                bStrolling_Unemployed                = 0,
                bStrolling_Part_time                 = 0,


                # Satiation
                sBusiness_trip     = 0,                 
                sWork_trip         = 0,                
                sRestaurant_trip   = 0,
                sHospital_trip     = 0,
                sLeisure_trip      = 0,
                sAltruistic_trip   = 0,
                sShopping_trip     = 0,
                sStrolling         = 0,


                # Complementarity and substitution
                dBusWork           = 1, 
                dShopStrol         = 0, 
                dShopRes           = 0, 
                dShopLeis          = 0, 
                dAltHosp           = 0, 
                dAltStrol          = 0, 

                # S.d. of error term
                sigma        = 1)

### Names of fixed parameters
apollo_fixed = c('dBusWork')

# ################################################################# #
#### GROUP AND VALIDATE INPUTS                                   ####
# ################################################################# #

apollo_inputs = apollo_validateInputs()

# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION                        ####
# ################################################################# #

apollo_probabilities=function(apollo_beta, apollo_inputs, 
                              functionality="estimate"){
    
  ### Initialise
  apollo_attach(apollo_beta, apollo_inputs)
  on.exit(apollo_detach(apollo_beta, apollo_inputs))
  P = list()


 ### Prepare Inputs
  alts  = c("Business_trip", "Work_trip", "Restaurant_trip", "Hospital_trip", "Leisure_trip", "Altruistic_trip", "Shopping_trip", "Strolling")
  nAlt = length(alts)
  ones = setNames(as.list(rep(1, nAlt)), alts)
  continuousChoice = list(Business_trip    =     Business_trip,
                          Work_trip        =     Work_trip,
                          Restaurant_trip  =     Restaurant_trip,
                          Hospital_trip    =     Hospital_trip,
                          Leisure_trip     =     Leisure_trip,
                          Altruistic_trip  =     Altruistic_trip,
                          Shopping_trip    =     Shopping_trip,
                          Strolling        =     Strolling)

utilities = list(
    Business_trip      = bBusiness_trip + bBusiness_trip_Female*Female + bBusiness_Age_30_49*Age_30_49 +  bBusiness_Age_above_50*Age_above_50 +  
                         bBusiness_trip_Mode_choice_car*Mode_choice_car +  bBusiness_trip_Mode_choice_train*Mode_choice_train + bBusiness_trip_Car_ownership*Car_ownership ,

    Work_trip      = bWork_trip + bWork_trip_Female*Female + bWork_Age_30_49*Age_30_49 +  bWork_Age_above_50*Age_above_50 +  
                     bWork_trip_Mode_choice_car*Mode_choice_car +  bWork_trip_Mode_choice_train*Mode_choice_train + bWork_trip_Mode_choice_subway*Mode_choice_subway,
    
    Restaurant_trip = bRestaurant_trip + bRestaurant_Housewife_Househusband*Housewife_Househusband + bRestaurant_trip_Female*Female + bRestaurant_Car_ownership*Car_ownership + 
                      bRestaurant_Unemployed*Unemployed + bRestaurant_Mode_choice_car*Mode_choice_car,

    Hospital_trip   = bHospital_trip + bHospital_trip_Age_above_50*Age_above_50 + bHospital_trip_Female*Female + bHospital_trip_Housewife_Househusband*Housewife_Househusband,

    Leisure_trip    = bLeisure_trip + bLeisure_trip_Female*Female + bLeisure_trip_Mode_choice_train*Mode_choice_train,

    Altruistic_trip = bAltruistic_trip + bAltruistic_trip_Female*Female + bAltruistic_Unemployed*Unemployed + bAltruistic_Housewife_Househusband*Housewife_Househusband + 
                      bAltruistic_Part_time*Part_time,

    Shopping_trip   = bShopping_trip + bShopping_trip_Female*Female + bShopping_trip_Housewife_Househusband*Housewife_Househusband,

    Strolling       = bStrolling + bStrolling_Housewife_Househusband*Housewife_Househusband + bStrolling_Age_above_50*Age_above_50 + bStrolling_Unemployed*Unemployed + 
                      bStrolling_Part_time*bStrolling_Part_time
  )
 
gamma = list   (Business_trip     = sBusiness_trip,    
                Work_trip         = sWork_trip,                         
                Restaurant_trip   = sRestaurant_trip,  
                Hospital_trip     = sHospital_trip,     
                Leisure_trip      = sLeisure_trip,      
                Altruistic_trip   = sAltruistic_trip,   
                Shopping_trip     = sShopping_trip,    
                Strolling         = sStrolling)

delta <- c  (0,              0,              0,                    0,                            
             dShopStrol,     0,              0,                    0,                            
             dShopRes,       dAltStrol,      0,                    0,                             
             dShopLeis,      dAltHosp,       dBusWork              0)

delta <- matrix(delta, nrow=nAlt, ncol=nAlt, byrow=TRUE)

emdc_settings <- list(continuousChoice = continuousChoice, 
                        avail            = ones,
                        utilities        = utilities, 
                        gamma            = gamma, 
                        delta            = delta, 
                        cost             = ones,
                        sigma            = sigma)
  P[["model"]] = apollo_emdc(emdc_settings, functionality)

### Comment out as necessary
  P = apollo_panelProd(P, apollo_inputs, functionality)
  P = apollo_prepareProb(P, apollo_inputs, functionality)
  return(P)
}

# ################################################################# #
#### MODEL ESTIMATION & OUTPUT                                   ####
# ################################################################# #

model = apollo_estimate(apollo_beta, apollo_fixed, 
                        apollo_probabilities, apollo_inputs)
  
stephanehess
Site Admin
Posts: 998
Joined: 24 Apr 2020, 16:29

Re: Unexpected numeric error while estimating eMDC2 model

Post by stephanehess »

Hi

can you please show us the full output, i.e. where the error occurs

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Pigouvian
Posts: 8
Joined: 23 Jun 2023, 19:26

Re: Unexpected numeric error while estimating eMDC2 model

Post by Pigouvian »

Thank you Prof. Hess for prompt response.
Please find below the running code for the mentioned problem:

Code: Select all

apollo_initialise()
Apollo ignition sequence completed
> 
> ### Set core controls
> apollo_control = list(
+     modelName  ="eMDC_without_budget",
+     modelDescr ="Extended MDC with complementarity and substitution, with unobserved budget and socio-demographics",
+     indivID    ="UserID",
+     outputDirectory="output"
+ )
> 
> # ################################################################# #
> #### LOAD DATA AND APPLY ANY TRANSFORMATIONS                     ####
> # ################################################################# #
> 
> database = read.csv("Tokyo_data_apollo.csv")
> 
> ### Randomly split dataset into estimation (70%) and validation (30%)
> set.seed(1)
> database$validation <- runif(nrow(database))>0.7
> dbVal    <- database[ database$validation,] # validation sample
> database <- database[!database$validation,] # estimation sample
> 
> # ################################################################# #
> #### DEFINE MODEL PARAMETERS                                     ####
> # ################################################################# #
> 
> ### Parameters starting values c(name1=value1, name2=value2, ...)
> apollo_beta = c(
+     # Base utility of inside goods
+     
+     bBusiness_trip     = 0,                 
+     bWork_trip         = 0,                
+     bRestaurant_trip   = 0,
+     bHospital_trip     = 0,
+     bLeisure_trip      = 0,
+     bAltruistic_trip   = 0,
+     bShopping_trip     = 0,
+     bStrolling         = 0,
+     
+     bBusiness_trip_Female             = 0,  
+     bBusiness_Age_30_49               = 0, 
+     bBusiness_Age_above_50            = 0, 
+     bBusiness_trip_Mode_choice_car    = 0,
+     bBusiness_trip_Mode_choice_train  = 0,
+     bBusiness_trip_Car_ownership      = 0,
+     
+     bWork_trip_Female                 = 0,  
+     bWork_Age_30_49                   = 0, 
+     bWork_Age_above_50                = 0, 
+     bWork_trip_Mode_choice_car        = 0,
+     bWork_trip_Mode_choice_subway     = 0,
+     bWork_trip_Mode_choice_train      = 0,
+     
+     bRestaurant_trip_Female            = 0,
+     bRestaurant_Housewife_Househusband = 0,
+     bRestaurant_Car_ownership          = 0,
+     bRestaurant_Unemployed             = 0,
+     bRestaurant_Mode_choice_car        = 0,
+     
+     bHospital_trip_Age_above_50           = 0,
+     bHospital_trip_Female                 = 0,
+     bHospital_trip_Housewife_Househusband = 0,
+     
+     bLeisure_trip_Female                  = 0,
+     bLeisure_trip_Mode_choice_train       = 0,
+     
+     bAltruistic_trip_Female                 = 0,
+     bAltruistic_Unemployed                  = 0,
+     bAltruistic_Housewife_Househusband      = 0,
+     bAltruistic_Part_time                   = 0,
+     
+     bShopping_trip_Female                      = 0,
+     bShopping_trip_Housewife_Househusband      = 0,
+     
+     bStrolling_Housewife_Househusband    = 0,
+     bStrolling_Age_above_50              = 0,
+     bStrolling_Unemployed                = 0,
+     bStrolling_Part_time                 = 0,
+     
+     
+     # Satiation
+     sBusiness_trip     = 0,                 
+     sWork_trip         = 0,                
+     sRestaurant_trip   = 0,
+     sHospital_trip     = 0,
+     sLeisure_trip      = 0,
+     sAltruistic_trip   = 0,
+     sShopping_trip     = 0,
+     sStrolling         = 0,
+     
+     
+     # Complementarity and substitution
+     dBusWork           = 1, 
+     dShopStrol         = 0, 
+     dShopRes           = 0, 
+     dShopLeis          = 0, 
+     dAltHosp           = 0, 
+     dAltStrol          = 0, 
+     
+     # S.d. of error term
+     sigma        = 1)
> 
> ### Names of fixed parameters
> apollo_fixed = c('dBusWork')
> 
> # ################################################################# #
> #### GROUP AND VALIDATE INPUTS                                   ####
> # ################################################################# #
> 
> apollo_inputs = apollo_validateInputs()
Several observations per individual detected based on the value of UserID. Setting panelData in apollo_control
  set to TRUE.
All checks on apollo_control completed.
All checks on database completed.
> 
> # ################################################################# #
> #### DEFINE MODEL AND LIKELIHOOD FUNCTION                        ####
> # ################################################################# #
> 
> apollo_probabilities=function(apollo_beta, apollo_inputs, 
+                               functionality="estimate"){
+     
+     ### Initialise
+     apollo_attach(apollo_beta, apollo_inputs)
+     on.exit(apollo_detach(apollo_beta, apollo_inputs))
+     P = list()
+     
+     
+     ### Prepare Inputs
+     alts  = c("Business_trip", "Work_trip", "Restaurant_trip", "Hospital_trip", "Leisure_trip", "Altruistic_trip", "Shopping_trip", "Strolling")
+     nAlt = length(alts)
+     ones = setNames(as.list(rep(1, nAlt)), alts)
+     continuousChoice = list(Business_trip    =     Business_trip,
+                             Work_trip        =     Work_trip,
+                             Restaurant_trip  =     Restaurant_trip,
+                             Hospital_trip    =     Hospital_trip,
+                             Leisure_trip     =     Leisure_trip,
+                             Altruistic_trip  =     Altruistic_trip,
+                             Shopping_trip    =     Shopping_trip,
+                             Strolling        =     Strolling)
+     
+     utilities = list(
+         Business_trip      = bBusiness_trip + bBusiness_trip_Female*Female + bBusiness_Age_30_49*Age_30_49 +  bBusiness_Age_above_50*Age_above_50 +  
+             bBusiness_trip_Mode_choice_car*Mode_choice_car +  bBusiness_trip_Mode_choice_train*Mode_choice_train + bBusiness_trip_Car_ownership*Car_ownership ,
+         
+         Work_trip      = bWork_trip + bWork_trip_Female*Female + bWork_Age_30_49*Age_30_49 +  bWork_Age_above_50*Age_above_50 +  
+             bWork_trip_Mode_choice_car*Mode_choice_car +  bWork_trip_Mode_choice_train*Mode_choice_train + bWork_trip_Mode_choice_subway*Mode_choice_subway,
+         
+         Restaurant_trip = bRestaurant_trip + bRestaurant_Housewife_Househusband*Housewife_Househusband + bRestaurant_trip_Female*Female + bRestaurant_Car_ownership*Car_ownership + 
+             bRestaurant_Unemployed*Unemployed + bRestaurant_Mode_choice_car*Mode_choice_car,
+         
+         Hospital_trip   = bHospital_trip + bHospital_trip_Age_above_50*Age_above_50 + bHospital_trip_Female*Female + bHospital_trip_Housewife_Househusband*Housewife_Househusband,
+         
+         Leisure_trip    = bLeisure_trip + bLeisure_trip_Female*Female + bLeisure_trip_Mode_choice_train*Mode_choice_train,
+         
+         Altruistic_trip = bAltruistic_trip + bAltruistic_trip_Female*Female + bAltruistic_Unemployed*Unemployed + bAltruistic_Housewife_Househusband*Housewife_Househusband + 
+             bAltruistic_Part_time*Part_time,
+         
+         Shopping_trip   = bShopping_trip + bShopping_trip_Female*Female + bShopping_trip_Housewife_Househusband*Housewife_Househusband,
+         
+         Strolling       = bStrolling + bStrolling_Housewife_Househusband*Housewife_Househusband + bStrolling_Age_above_50*Age_above_50 + bStrolling_Unemployed*Unemployed + 
+             bStrolling_Part_time*bStrolling_Part_time
+     )
+     
+     gamma = list   (Business_trip     = sBusiness_trip,    
+                     Work_trip         = sWork_trip,                         
+                     Restaurant_trip   = sRestaurant_trip,  
+                     Hospital_trip     = sHospital_trip,     
+                     Leisure_trip      = sLeisure_trip,      
+                     Altruistic_trip   = sAltruistic_trip,   
+                     Shopping_trip     = sShopping_trip,    
+                     Strolling         = sStrolling)
+     
+     delta <- c  (0,              0,              0,                    0,                            
+                  dShopStrol,     0,              0,                    0,                            
+                  dShopRes,       dAltStrol,      0,                    0,                             
+                  dShopLeis,      dAltHosp,       dBusWork              0)
Error: unexpected numeric constant in:
"                 dShopRes,       dAltStrol,      0,                    0,                             
                 dShopLeis,      dAltHosp,       dBusWork              0"
>     delta <- matrix(delta, nrow=nAlt, ncol=nAlt, byrow=TRUE)
Error in matrix(delta, nrow = nAlt, ncol = nAlt, byrow = TRUE) : 
  object 'delta' not found
>     
>     emdc_settings <- list(continuousChoice = continuousChoice, 
+                           avail            = ones,
+                           utilities        = utilities, 
+                           gamma            = gamma, 
+                           delta            = delta, 
+                           cost             = ones,
+                           sigma            = sigma)
Error: object 'continuousChoice' not found
>     P[["model"]] = apollo_emdc(emdc_settings, functionality)
Error in apollo_emdc(emdc_settings, functionality) : 
  object 'emdc_settings' not found
>     
>     ### Comment out as necessary
>     P = apollo_panelProd(P, apollo_inputs, functionality)
Error in apollo_panelProd(P, apollo_inputs, functionality) : 
  object 'P' not found
>     P = apollo_prepareProb(P, apollo_inputs, functionality)
Error in apollo_prepareProb(P, apollo_inputs, functionality) : 
  object 'P' not found
>     return(P)
Error: object 'P' not found
> }
Error: unexpected '}' in "}"
> 
> # ################################################################# #
> #### MODEL ESTIMATION & OUTPUT                                   ####
> # ################################################################# #
> 
> model = apollo_estimate(apollo_beta, apollo_fixed, 
+                         apollo_probabilities, apollo_inputs)
Error in apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities,  : 
  object 'apollo_probabilities' not found
stephanehess
Site Admin
Posts: 998
Joined: 24 Apr 2020, 16:29

Re: Unexpected numeric error while estimating eMDC2 model

Post by stephanehess »

You're missing a comma after dBusWork
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Pigouvian
Posts: 8
Joined: 23 Jun 2023, 19:26

Re: Unexpected numeric error while estimating eMDC2 model

Post by Pigouvian »

Thnak you Prof. Hess for resolving this issues. The error with the associated step is resolved, however, I am now getting the following error message: Error in apollo_emdc2(emdc_settings, functionality) :
Some mandatory elements are missing from 'emdc_settings'


The running code is as follows:

Code: Select all

> # ################################################################# #
> #### LOAD LIBRARY AND DEFINE CORE SETTINGS                       ####
> # ################################################################# #
> 
> ### Clear memory and initialise
> rm(list = ls())
> library(apollo)
> apollo_initialise()
Apollo ignition sequence completed
> 
> ### Set core controls
> apollo_control = list(
+     modelName  ="eMDC_without_budget",
+     modelDescr ="Extended MDC with complementarity and substitution, with unobserved budget and socio-demographics",
+     indivID    ="UserID",
+     outputDirectory="output"
+ )
> 
> # ################################################################# #
> #### LOAD DATA AND APPLY ANY TRANSFORMATIONS                     ####
> # ################################################################# #
> 
> database = read.csv("Tokyo_data_apollo.csv")
> 
> ### Randomly split dataset into estimation (70%) and validation (30%)
> set. Seed(1)
> database$validation <- runif(nrow(database))>0.7
> dbVal    <- database[ database$validation,] # validation sample
> database <- database[!database$validation,] # estimation sample
> 
> # ################################################################# #
> #### DEFINE MODEL PARAMETERS                                     ####
> # ################################################################# #
> 
> ### Parameters starting values c(name1=value1, name2=value2, ...)
> apollo_beta = c(
+     # Base utility of inside goods
+     
+     bBusiness_trip     = 0,                 
+     bWork_trip         = 0,                
+     bRestaurant_trip   = 0,
+     bHospital_trip     = 0,
+     bLeisure_trip      = 0,
+     bAltruistic_trip   = 0,
+     bShopping_trip     = 0,
+     bStrolling         = 0,
+     
+     bBusiness_trip_Female             = 0,  
+     bBusiness_Age_30_49               = 0, 
+     bBusiness_Age_above_50            = 0, 
+     bBusiness_trip_Mode_choice_car    = 0,
+     bBusiness_trip_Mode_choice_train  = 0,
+     bBusiness_trip_Car_ownership      = 0,
+     
+     bWork_trip_Female                 = 0,  
+     bWork_Age_30_49                   = 0, 
+     bWork_Age_above_50                = 0, 
+     bWork_trip_Mode_choice_car        = 0,
+     bWork_trip_Mode_choice_subway     = 0,
+     bWork_trip_Mode_choice_train      = 0,
+     
+     bRestaurant_trip_Female            = 0,
+     bRestaurant_Housewife_Househusband = 0,
+     bRestaurant_Car_ownership          = 0,
+     bRestaurant_Unemployed             = 0,
+     bRestaurant_Mode_choice_car        = 0,
+     
+     bHospital_trip_Age_above_50           = 0,
+     bHospital_trip_Female                 = 0,
+     bHospital_trip_Housewife_Househusband = 0,
+     
+     bLeisure_trip_Female                  = 0,
+     bLeisure_trip_Mode_choice_train       = 0,
+     
+     bAltruistic_trip_Female                 = 0,
+     bAltruistic_Unemployed                  = 0,
+     bAltruistic_Housewife_Househusband      = 0,
+     bAltruistic_Part_time                   = 0,
+     
+     bShopping_trip_Female                      = 0,
+     bShopping_trip_Housewife_Househusband      = 0,
+     
+     bStrolling_Housewife_Househusband    = 0,
+     bStrolling_Age_above_50              = 0,
+     bStrolling_Unemployed                = 0,
+     bStrolling_Part_time                 = 0,
+     
+     
+     # Satiation
+     sBusiness_trip     = 0,                 
+     sWork_trip         = 0,                
+     sRestaurant_trip   = 0,
+     sHospital_trip     = 0,
+     sLeisure_trip      = 0,
+     sAltruistic_trip   = 0,
+     sShopping_trip     = 0,
+     sStrolling         = 0,
+     
+     
+     # Complementarity and substitution
+     dBusWork           = 1, 
+     dShopStrol         = 0, 
+     dShopRes           = 0, 
+     dShopLeis          = 0, 
+     dAltHosp           = 0, 
+     dAltStrol          = 0, 
+     
+     # S.d. of error term
+     sigma        = 1)
> 
> ### Names of fixed parameters
> apollo_fixed = c('dBusWork')
> 
> # ################################################################# #
> #### GROUP AND VALIDATE INPUTS                                   ####
> # ################################################################# #
> 
> apollo_inputs = apollo_validateInputs()
Several observations per individual detected based on the value of UserID. Setting panelData in apollo_control
  set to TRUE.
All checks on apollo_control completed.
All checks on database completed.
> 
> # ################################################################# #
> #### DEFINE MODEL AND LIKELIHOOD FUNCTION                        ####
> # ################################################################# #
> 
> apollo_probabilities=function(apollo_beta, apollo_inputs, 
+                               functionality="estimate"){
+     
+     ### Initialise
+     apollo_attach(apollo_beta, apollo_inputs)
+     on. Exit(apollo_detach(apollo_beta, apollo_inputs))
+     P = list()
+     
+     
+     ### Prepare Inputs
+     alts  = c("Business_trip", "Work_trip", "Restaurant_trip", "Hospital_trip", "Leisure_trip", "Altruistic_trip", "Shopping_trip", "Strolling")
+     nAlt = length(alts)
+     ones = setNames(as.list(rep(1, nAlt)), alts)
+     continuousChoice = list(Business_trip    =     Business_trip,
+                             Work_trip        =     Work_trip,
+                             Restaurant_trip  =     Restaurant_trip,
+                             Hospital_trip    =     Hospital_trip,
+                             Leisure_trip     =     Leisure_trip,
+                             Altruistic_trip  =     Altruistic_trip,
+                             Shopping_trip    =     Shopping_trip,
+                             Strolling        =     Strolling)
+     
+     utilities = list(
+         Business_trip      = bBusiness_trip + bBusiness_trip_Female*Female + bBusiness_Age_30_49*Age_30_49 +  bBusiness_Age_above_50*Age_above_50 +  
+             bBusiness_trip_Mode_choice_car*Mode_choice_car +  bBusiness_trip_Mode_choice_train*Mode_choice_train + bBusiness_trip_Car_ownership*Car_ownership ,
+         
+         Work_trip      = bWork_trip + bWork_trip_Female*Female + bWork_Age_30_49*Age_30_49 +  bWork_Age_above_50*Age_above_50 +  
+             bWork_trip_Mode_choice_car*Mode_choice_car +  bWork_trip_Mode_choice_train*Mode_choice_train + bWork_trip_Mode_choice_subway*Mode_choice_subway,
+         
+         Restaurant_trip = bRestaurant_trip + bRestaurant_Housewife_Househusband*Housewife_Househusband + bRestaurant_trip_Female*Female + bRestaurant_Car_ownership*Car_ownership + 
+             bRestaurant_Unemployed*Unemployed + bRestaurant_Mode_choice_car*Mode_choice_car,
+         
+         Hospital_trip   = bHospital_trip + bHospital_trip_Age_above_50*Age_above_50 + bHospital_trip_Female*Female + bHospital_trip_Housewife_Househusband*Housewife_Househusband,
+         
+         Leisure_trip    = bLeisure_trip + bLeisure_trip_Female*Female + bLeisure_trip_Mode_choice_train*Mode_choice_train,
+         
+         Altruistic_trip = bAltruistic_trip + bAltruistic_trip_Female*Female + bAltruistic_Unemployed*Unemployed + bAltruistic_Housewife_Househusband*Housewife_Househusband + 
+             bAltruistic_Part_time*Part_time,
+         
+         Shopping_trip   = bShopping_trip + bShopping_trip_Female*Female + bShopping_trip_Housewife_Househusband*Housewife_Househusband,
+         
+         Strolling       = bStrolling + bStrolling_Housewife_Househusband*Housewife_Househusband + bStrolling_Age_above_50*Age_above_50 + bStrolling_Unemployed*Unemployed + 
+             bStrolling_Part_time*bStrolling_Part_time
+     )
+     
+     gamma = list   (Business_trip     = sBusiness_trip,    
+                     Work_trip         = sWork_trip,                         
+                     Restaurant_trip   = sRestaurant_trip,  
+                     Hospital_trip     = sHospital_trip,     
+                     Leisure_trip      = sLeisure_trip,      
+                     Altruistic_trip   = sAltruistic_trip,   
+                     Shopping_trip     = sShopping_trip,    
+                     Strolling         = sStrolling)
+     
+     delta <- c  (0,              0,              0,                    0,                            
+                  dShopStrol,     0,              0,                    0,                            
+                  dShopRes,       dAltStrol,      0,                    0,                             
+                  dShopLeis,      dAltHosp,       dBusWork,             0)
+     delta <- matrix(delta, nrow=nAlt, ncol=nAlt, byrow=TRUE)
+     
+     emdc_settings <- list(continuousChoice = continuousChoice, 
+                           avail            = ones,
+                           utilities        = utilities,
+                           gamma            = gamma, 
+                           delta            = delta, 
+                           cost             = ones,
+                           sigma            = sigma)
+     P[["model"]] = apollo_emdc(emdc_settings, functionality)
+     
+     ### Comment out as necessary
+     P = apollo_panelProd(P, apollo_inputs, functionality)
+     P = apollo_prepareProb(P, apollo_inputs, functionality)
+     return(P)
+ }
> 
> # ################################################################# #
> #### MODEL ESTIMATION & OUTPUT                                   ####
> # ################################################################# #
> 
> model = apollo_estimate(apollo_beta, apollo_fixed, 
+                         apollo_probabilities, apollo_inputs)
Preparing user-defined functions.
WARNING: The pre-processing of 'apollo_probabilities' failed in initial testing. Your model may still run, but this
  indicates a potential problem. Please contact the developers for assistance! 

  Current process will resume in 5 seconds unless interrupted by the user.....


Testing likelihood function...
Error in apollo_emdc2(emdc_settings, functionality) : 
  Some mandatory elements are missing from 'emdc_settings'.
Is it because I have not considered Outside Utility? I did not include this element since in my research I do not have any outside good.
dpalma
Posts: 190
Joined: 24 Apr 2020, 17:54

Re: Unexpected numeric error while estimating eMDC2 model

Post by dpalma »

Hi,

The eMDC model always assumes an outside good, it is part of the model. However, one of the interesting characteristics of the model is that it allows you to leave the consumed amount of the outside good (or equivalently the budget) undefined. Therefore, you still need to define the utility of the outside good (utilityOutside in apollo).

Not having to define the budget is useful when you don't know the budget (e.g. when modelling monetary expenditure you are often not confident on the real discretionary income of people), but it is not so useful when modelling time use, as in that case you clearly know the budget (e.g. 24 hours a day, or 168 hours a week). If you are modelling time use, I would recommend you use the model with the budget. For that, you would have to provide the budget to the model, and remove one of the alternatives, so that it becomes the outside good (outside good consumption = budget - sum(consumption in all other alternatives)).

You can see a longer discussion on Palma & Hess (2022) Extending the Multiple Discrete Continuous (MDC) modelling framework to consider complementarity, substitution, and an unobserved budget. Transportation Research 161B.

Best wishes
David
Post Reply