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. We check the forum at least twice a week. It may thus take a couple of days for your post to appear and before we reply. There is no need to submit the post multiple times.

Signs of coefficients are complete opposite

Ask questions about model specifications. Ideally include a mathematical explanation of your proposed model.
Post Reply
Yikang Wu
Posts: 2
Joined: 20 May 2024, 02:31

Exploded logit: only one alternative is available for each observation

Post 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
stephanehess
Site Admin
Posts: 1367
Joined: 24 Apr 2020, 16:29

Re: Signs of coefficients are complete opposite

Post by stephanehess »

Hi

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

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Post Reply