Page 1 of 1

Exploded logit: only one alternative is available for each observation

Posted: 10 Oct 2024, 09:27
by Yikang Wu
Dear Stephan,

I have encountered the same error that

Testing likelihood function...
INFORMATION: Setting "avail" is missing, so full availability is assumed.
Error in apollo_validate(el_settings, modelType, functionality, apollo_inputs) :
INPUT ISSUE - Only one alternative is available for each observation for model component "model!

I am using read.csv and trying to build the exploded logit model. However, there was no problem when I built the MNL model.

And this is my code:

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       = "EL_Strategy",
  modelDescr      = "Exploded logit model on all data of strategy choice for car owners",
  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 = read.csv("/Users/josephwu/Library/CloudStorage/OneDrive-TheUniversityofQueensland/Brisbane micromobility survey/EMM SP study/Data/all_data/models/Strategy_all/exploded_logit/csv_strategy_with_car_demo.csv",header=TRUE)
### for data dictionary, use ?apollo_modeChoiceData

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

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(asc_direct          = 0,
              asc_replace         = 0,
              asc_optout          = 0,
              b_type_direct_eb    = 0,
              b_type_direct_es    = 0,
              b_type_replace_eb   = 0,
              b_type_replace_es   = 0,
              b_fp_direct_1         = 0,
              b_fp_direct_2         = 0,
              b_fp_direct_3         = 0,
              b_fp_direct_4         = 0,
              b_fp_direct_5         = 0,
              b_fp_replace_1        = 0,
              b_fp_replace_2        = 0,
              b_fp_replace_3        = 0,
              b_fp_replace_4        = 0,
              b_fp_replace_5        = 0,
              b_rate_direct_1     = 0,
              b_rate_direct_2     = 0,
              b_rate_direct_3     = 0,
              b_rate_replace_1    = 0,
              b_rate_replace_2    = 0,
              b_rate_replace_3    = 0,
              b_rr_direct_1         = 0,
              b_rr_direct_2         = 0,
              b_rr_direct_3         = 0,
              b_rr_replace_1        = 0,
              b_rr_replace_2        = 0,
              b_rr_replace_3        = 0,
              b_am_yes_direct     = 0,
              b_am_no_direct      = 0,
              b_am_yes_replace    = 0,
              b_am_no_replace     = 0,
              scale_1             = 1,
              scale_2             = 1,
              scale_3             = 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("asc_optout","b_type_direct_es","b_type_replace_es","b_fp_direct_1","b_rate_direct_1","b_fp_replace_1",
                 "b_rate_replace_1","b_am_no_direct","b_am_no_replace","b_rr_direct_1","b_rr_replace_1","scale_1")

# ################################################################# #
#### 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[["direct"]]  = asc_direct  + b_type_direct_eb  * ( a1_x1 == 1 )  + b_type_direct_es  * ( a1_x1 == 2 )  + b_fp_direct_1 * (a1_x2 == 1 ) + b_fp_direct_1 * (a1_x2 == 1 ) + 
  #   b_fp_direct_2 * (a1_x2 == 2 ) + b_fp_direct_3 * (a1_x2 == 3 ) + b_fp_direct_4 * (a1_x2 == 4 ) + b_fp_direct_5 * (a1_x2 == 5 ) +
  #   b_rate_direct_1 * ( a1_x3 == 1)  + b_rate_direct_2 * ( a1_x3 == 2)  + 
  #   b_rate_direct_3 * ( a1_x3 == 3)  + b_rr_direct * a1_x4_value  + b_am_yes_direct * ( a1_x5 == 1)  + b_am_no_direct * ( a1_x5 == 2)
  V[["direct"]]  = asc_direct  + b_type_direct_eb  * ( a1_x1 == 1 )  + b_type_direct_es  * ( a1_x1 == 2 )  + b_fp_direct_1 * (a1_x2 == 1 ) + 
    b_fp_direct_2 * (a1_x2 == 2 ) + b_fp_direct_3 * (a1_x2 == 3 ) + b_fp_direct_4 * (a1_x2 == 4 ) + b_fp_direct_5 * (a1_x2 == 5 ) +
    b_rate_direct_1 * ( a1_x3 == 1)  + b_rate_direct_2 * ( a1_x3 == 2)  + 
    b_rate_direct_3 * ( a1_x3 == 3)  + b_rr_direct_1 * ( a1_x4 == 1 ) + b_rr_direct_2 * ( a1_x4 == 2 ) + b_rr_direct_3 * ( a1_x4 == 3 ) + b_am_yes_direct * ( a1_x5 == 1)  + b_am_no_direct * ( a1_x5 == 2)
  
  # V[["replace"]]  = asc_replace + b_type_replace_eb  * ( a1_x1 == 1 )  + b_type_replace_es  * ( a1_x1 == 2 )  + b_fp_replace_1 * (a1_x2 == 1 ) + b_fp_replace_1 * (a1_x2 == 1 ) + 
  #   b_fp_replace_2 * (a1_x2 == 2 ) + b_fp_replace_3 * (a1_x2 == 3 ) + b_fp_replace_4 * (a1_x2 == 4 ) + b_fp_replace_5 * (a1_x2 == 5 )  + b_rate_replace_1 * ( a2_x3 == 1)  + 
  #   b_rate_replace_2 * ( a2_x3 == 2)  + 
  #   b_rate_replace_3 * ( a2_x3 == 3)  + b_rr_replace * a2_x4_value  + b_am_yes_replace * ( a2_x5 == 1)  + b_am_no_replace * ( a2_x5 == 2)
  V[["replace"]]  = asc_replace + b_type_replace_eb  * ( a1_x1 == 1 )  + b_type_replace_es  * ( a1_x1 == 2 ) + b_fp_replace_1 * (a1_x2 == 1 ) + 
    b_fp_replace_2 * (a1_x2 == 2 ) + b_fp_replace_3 * (a1_x2 == 3 ) + b_fp_replace_4 * (a1_x2 == 4 ) + b_fp_replace_5 * (a1_x2 == 5 )  + b_rate_replace_1 * ( a2_x3 == 1)  + 
    b_rate_replace_2 * ( a2_x3 == 2)  + 
    b_rate_replace_3 * ( a2_x3 == 3)  + b_rr_replace_1 * ( a2_x4 == 1 )  + b_rr_replace_2 * ( a2_x4 == 2 )+ b_rr_replace_3 * ( a2_x4 == 3 )  + b_am_yes_replace * ( a2_x5 == 1)  + b_am_no_replace * ( a2_x5 == 2)
  
  V[["optout"]]  = asc_optout  
  
  ### Define settings for MNL model component
  el_settings = list(
    alternatives  = c(direct=1, replace=2, optout=3), 
    #avail        = list(direct=1, replace=1, optout=1),
    choiceVars     = list(pref1, pref3, third_pref),
    utilities     = V,
    scales       = list(scale_1,scale_2,scale_3)
  )
  
  ### Compute exploded logit probabilities
  P[["model"]]=apollo_el(el_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 for your time!

Best,
Yikang

Re: Signs of coefficients are complete opposite

Posted: 10 Oct 2024, 10:29
by stephanehess
Hi

you have 3 alternatives, so you can only have 2 stages in the EL model

Stephane