Page 1 of 1

Scenario attributes estimation

Posted: 10 Jul 2024, 08:21
by Yikang Wu
Dear Stephane,

I am building simple MNL models based on my pilot data for getting informative priors of efficient experimental design. And my mode choice survey of three alternatives includes two scenario attributes, weather and travel distance, which are categorical and continues attributes respectively.

As only difference matters, I only put these scenario attributes as main effects in the utility function of two alternatives, and try to get the relative priors to the remaining alternative. However, regarding the categorical attribute, weather with 5 levels, I have to fix one level as zero to estimate the model. Then I get priors for weather of two alternatives, for example, (0,-0.1,-0.2,-0.3,-0.4) and (0,-0.05,-0.1,-0.13,-0.2).

For this, I am confused about how to interpret the zero priors as relative effect to the third alternative. If I interpret the last level of weather have -0.4 effect on choosing the first alternative compared to the third one, the first level of weather will have no effect on choice.

Or what is your suggestion on modelling scenario attributes?

Here is my code, please notice that b_td is for travel distance (continuous) and b_w is for weather (categorical with 5 levels).

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       = "MNL_ModeChoice_Car",
  modelDescr      = "Simple MNL model on mode choice with car as status-quo",
  indivID         = "RID",
  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 <- pmm_ptsmm_car_2024_06_20
### for data dictionary, use ?apollo_modeChoiceData

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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(asc_pmm          = 0,
              asc_ptsmm         = 0,
              asc_car          = 0,
              b_td_pmm    = 0,
              b_td_ptsmm    = 0,
              #b_td_car    = 0,
              b_w1_pmm   = 0,
              b_w2_pmm   = 0,
              b_w3_pmm   = 0,
              b_w4_pmm   = 0,
              b_w5_pmm   = 0,
              b_w1_ptsmm   = 0,
              b_w2_ptsmm   = 0,
              b_w3_ptsmm   = 0,
              b_w4_ptsmm   = 0,
              b_w5_ptsmm   = 0,
              #b_w1_car   = 0,
              #b_w2_car   = 0,
              #b_w3_car   = 0,
              #b_w4_car   = 0,
              #b_w5_car   = 0,
              b_ivtt_ptsmm = 0,
              b_ivtt_car = 0,
              b_mmtt_pmm   = 0,
              b_mmtt_ptsmm = 0,
              b_wt_ptsmm    = 0,
              b_walk_car    = 0,
              b_rc_pmm        = 0,
              b_rc_ptsmm     = 0,
              b_rc_car     = 0,
              b_pc_car     = 0,
              b_cl1_ptsmm     = 0,
              b_cl2_ptsmm     = 0,
              b_cl3_ptsmm     = 0,
              b_cl1_car     = 0,
              b_cl2_car     = 0,
              b_cl3_car     = 0,
              b_bw1_pmm     = 0,
              b_bw2_pmm     = 0,
              b_bw3_pmm     = 0,
              b_bw4_pmm     = 0,
              b_bw5_pmm     = 0,
              b_bw1_ptsmm    = 0,
              b_bw2_ptsmm    = 0,
              b_bw3_ptsmm    = 0,
              b_bw4_ptsmm    = 0,
              b_bw5_ptsmm    = 0)

### 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("asc_car","b_w1_pmm","b_w1_ptsmm","b_w1_car","b_cl1_ptsmm","b_cl1_car","b_bw1_pmm","b_bw1_ptsmm")
apollo_fixed = c("asc_car","b_w1_pmm","b_w1_ptsmm","b_cl1_ptsmm","b_cl1_car","b_bw1_pmm","b_bw1_ptsmm")

# ################################################################# #
#### 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()
  
  ### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
  V = list()
  
  V[["pmm"]]  = asc_pmm  + b_td_pmm  * a1_x1_value  + b_w1_pmm  * ( a1_x2 == 1 )  + b_w2_pmm  * ( a1_x2 == 2 )  + 
    b_w3_pmm  * ( a1_x2 == 3 )  + b_w4_pmm  * ( a1_x2 == 4 )  + b_w5_pmm  * ( a1_x2 == 5 )  + 
    b_mmtt_pmm * a1_x5_value  + b_rc_pmm * a1_x9_value  + b_bw1_pmm * ( a1_x12 == 1)  + b_bw2_pmm * ( a1_x12 == 2)  + 
    b_bw3_pmm * ( a1_x12 == 3)  + b_bw4_pmm * ( a1_x12 == 4)  + b_bw5_pmm * ( a1_x12 == 5)
  
  V[["ptsmm"]]  = asc_ptsmm  + b_td_ptsmm  * a1_x1_value  + b_w1_ptsmm  * ( a1_x2 == 1 )  + 
    b_w2_ptsmm  * ( a1_x2 == 2 )  + b_w3_ptsmm  * ( a1_x2 == 3 )  + b_w4_ptsmm  * ( a1_x2 == 4 )  + 
    b_w5_ptsmm  * ( a1_x2 == 5 ) + b_ivtt_ptsmm  * a2_x4_value  + 
    b_mmtt_ptsmm * a2_x5_value  + b_wt_ptsmm * a2_x6_value  + b_rc_ptsmm * a2_x9_value  + 
    b_cl1_ptsmm * ( a2_x11 == 1) + b_cl2_ptsmm * ( a2_x11 == 2) + b_cl3_ptsmm * ( a2_x11 == 3)  + 
    b_bw1_ptsmm * ( a2_x12 == 1)  + b_bw2_ptsmm * ( a2_x12 == 2)  + 
    b_bw3_ptsmm * ( a2_x12 == 3)  + b_bw4_ptsmm * ( a2_x12 == 4)  + b_bw5_ptsmm * ( a2_x12 == 5)
  
  V[["car"]]  = asc_car  +  b_ivtt_car * a3_x4_value +
    b_walk_car * a3_x7_value  + b_rc_car * a3_x9_value  + b_pc_car * a3_x10_value  + b_cl1_car * (a3_x11 == 1)  + 
    b_cl2_car * (a3_x11 == 2)  + b_cl3_car * (a3_x11 == 3)
  
  ### Define settings for MNL model component
  mnl_settings = list(
    alternatives  = c(pmm=1, ptsmm=2, car=3), 
    #avail         = list(car=av_car, bus=av_bus, air=av_air, rail=av_rail), 
    choiceVar     = pref1,
    utilities     = V
  )
  
  ### Compute probabilities using MNL model
  P[["model"]] = apollo_mnl(mnl_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)


Thank you so much for your time and I appreciate any of your suggestions.

Best,
Yikang

Re: Scenario attributes estimation

Posted: 12 Aug 2024, 13:50
by stephanehess
Hi Yikang

apologies for the slow reply

this depends on what you want your model to do. Are your scenario variables likely to influence the base split between labelled alternatives? Then interacting them with the constants (like you did) makes sense. If they are likely to influence utilities for specific attributes, then you need to interact them with the betas (in all utilities)

In terms of interpretation, your baseline constants will give you the differences in utility vs your base alternative, i.e. car, at the base level for the scenario attributes. And the interactions will tell you how those changes at other level of the scenario variables.

Stephane