Page 1 of 1

MDCEV gamma parameterisation

Posted: 10 Dec 2023, 19:10
by Anna_R
Hi!

I am trying to estimate a MDCEV model in which the gamma term is parameterised. However, I get the following warning:

Code: Select all

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! 
If I don't interrupt the process, I get this error:

Code: Select all

Error in apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities,  : 
  CALCULATION ISSUE - Log-likelihood calculation fails at values close to the starting values!
I have been able to replicate the issue using the mdcev example (with an outside good). The issue appears when parameterising gamma for alternatives that are not always available. Specifically, log-likelihood calculation fails at starting values for individuals for whom the alternative is not available.

For the following example, I tried parameterising gamma_work and added individual availabilities for each observation right after loading the database.

Code: Select all

# ################################################################# #
#### 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_with_outside_good",
  modelDescr ="MDCEV model on time use data, alpha-gamma profile with outside good and socio-demographics",
  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)
database = apollo_timeUseData
database <- database %>%
  group_by(indivID) %>%
  mutate(w_sum = sum(t_a02),
         w_avail = ifelse(w_sum == 0, sample(x = 0:1, size = 1), 1)) %>%
  ungroup()
### for data dictionary, use ?apollo_timeUseData

### Create consumption variables for combined activities
database$t_outside = rowSums(database[,c("t_a01", "t_a06", "t_a10", "t_a11", "t_a12")]) # outside good: time spent at home and travelling
database$t_leisure = rowSums(database[,c("t_a07", "t_a08", "t_a09")])

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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(alpha_base         = 0,
                gamma_work         = 1,
                gamma_work_FT      = 1,
                gamma_school       = 1,
                gamma_shopping     = 1,
                gamma_private      = 1,
                gamma_leisure      = 1,
                delta_work         = 0,
                delta_school       = 0,
                delta_shopping     = 0,
                delta_private      = 0,
                delta_leisure      = 0,
                delta_work_FT      = 0,
                delta_work_wknd    = 0,
                delta_school_young = 0,
                delta_leisure_wknd = 0,
                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("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
  alternatives  = c("outside",
                    "work",
                    "school",
                    "shopping",
                    "private", 
                    "leisure")
  
  ### Define availabilities
  avail = list(outside  = 1, 
               work     = w_avail, 
               school   = 1, 
               shopping = 1, 
               private  = 1,
               leisure  = 1)
  
  ### Define continuous consumption for individual alternatives
  continuousChoice = list(outside  = t_outside/60,
                          work     = t_a02/60,
                          school   = t_a03/60,
                          shopping = t_a04/60,
                          private  = t_a05/60,
                          leisure  = t_leisure/60)
  
  ### Define utilities for individual alternatives
  V = list()
  V[["outside"]]  = 0
  V[["work"]]     = delta_work     + delta_work_FT * occ_full_time + delta_work_wknd * weekend
  V[["school"]]   = delta_school   + delta_school_young * (age<=30)
  V[["shopping"]] = delta_shopping
  V[["private"]]  = delta_private
  V[["leisure"]]  = delta_leisure  + delta_leisure_wknd*weekend
  
  ### Define alpha parameters
  alpha = list(outside  = 1 /(1 + exp(-alpha_base)), 
               work     = 1 /(1 + exp(-alpha_base)), 
               school   = 1 /(1 + exp(-alpha_base)), 
               shopping = 1 /(1 + exp(-alpha_base)), 
               private  = 1 /(1 + exp(-alpha_base)),
               leisure  = 1 /(1 + exp(-alpha_base)))
  
  ### Define gamma parameters
  gamma = list(work     = gamma_work + gamma_work_FT * occ_full_time,    
               school   = gamma_school,
               shopping = gamma_shopping,
               private  = gamma_private,
               leisure  = gamma_leisure)
  
  ### Define costs for individual alternatives
  cost = list(outside  = 1, 
              work     = 1, 
              school   = 1, 
              shopping = 1, 
              private  = 1,
              leisure  = 1)
  
  ### 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)

Re: MDCEV gamma parameterisation

Posted: 06 Feb 2024, 20:46
by dpalma
Hi,

Sorry for the delayed response. Both Stephane and I were travelling and didn't get the chance to look at the forum as often as we are used to.

I am not sure if it is the same in your case, but in the example you built, the problem is that the budget is incorrect for those observations where work is not available. In the code, all observations have a budget of 24 hours, but if you make work unavailable when working time is not zero, then consumption will not add up to 24 hours for those individuals with work unavailable. This brakes the model. If you make work available for everyone, you can parametrise the gammas without an issue.

Best
David