Multinomial logit model has the same code as binary logit model. We will see two additional function in apollo_probabilities() in step 5.
apollo_panelProd() which multiplies the probabilities choice observations for the same individual. We have been commenting this command (using #) because we only have one observation per individual.### Take product across observation for same individual
P = apollo_panelProd(P, apollo_inputs, functionality)
apollo_weighing() prior to apollo_prepareProb(). The command is shown below. We also need to identify the weights column in apollo_control. It can be seen in step 1-2.### accounts for the weights
P = apollo_weighting(P, apollo_inputs, functionality)
The apollo_probabilities() always ends with the same two commands. First is apollo_prepareProb() which prepares the output of the function, followed by command return(P)
### Prepare and return outputs of function
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
Note - There are many examples, along with few datasets, that can be downloaded from the Apollo choice modelling website. The chapter 4 in the Apollo manual explains the example no. 3 in detail. However, I feel that the details in chapter 4 can be overwhelming for those who are new to discrete choice modelling.
We have provided a sample code for binary logit model for an assignment question - 1b
# --------------------------------------------------------------------------------
### Step 1 - Initialise the code
# clean workspace
rm(list = ls())
# (OPTIONAL) set working directory
setwd("C:/Users/gulhare.s/Dropbox (UFL)/PhD/Discrete choice modeling/Article 5 - Multinomial Logit Model")
# load libraries
library(tidyverse) # for data science
library(haven) # to read SPSS files
library(apollo) # for mode choice analysis
# mandatory step
apollo_initialise()
#### Step 1-2 - Set Apollo controls
apollo_control = list(
modelName = "Q_1b",
modelDescr ="Model 1a",
indivID ="CASE",
weights = "WEIGHT"
)
# --------------------------------------------------------------------------------
### Step 2 - Data
# read SPSS file
tbl_spss <- read_sav("Data_A2.sav")
# read labels and assign it to tbl_labelled (OPTIONAL)
tbl_labelled <- as_factor(tbl_spss)
# ----------------------------------------------------------------------------------
#### Step 2-2 - Clean and transform data
# remove labels/attributes from tibble
tbl_spss[] <- lapply(tbl_spss, function(x) {attributes(x) <- NULL;x})
# convert tibble to dataframe, always use variable name database
database <- as.data.frame(tbl_spss)
# --------------------------------------------------------------------------------
### Step 3 - Define model parameters
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(asc_DA = 0,
asc_SR = 0,
asc_TR = 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_TR")
# --------------------------------------------------------------------------------
### Step 4 - Validate data
apollo_inputs = apollo_validateInputs()
# --------------------------------------------------------------------------------
### Step 5 - Define apollo probabilities
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))
### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[['DA']] = asc_DA
V[['SR']] = asc_SR
V[['TR']] = asc_TR
### Create list of probabilities P
P = list()
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(DA = 1, SR = 2, TR = 3),
avail = 1,
choiceVar = CHOICE,
V = 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)
### weigh the observations
P = apollo_weighting(P, apollo_inputs, functionality)
### Prepare and return outputs of function
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}
# --------------------------------------------------------------------------------
### Step 6 - Estimate model
model = apollo_estimate(apollo_beta,
apollo_fixed,
apollo_probabilities,
apollo_inputs)
# --------------------------------------------------------------------------------
### Step 7 - Model output
# print model output
apollo_modelOutput(model)
# save output
apollo_saveOutput(model)