The code is broken down into series of steps, it very slightly deviates from the manual in step 2 because we will use tidyverse package to read, analyze and preprocess data since it is more flexible and very useful tool for future.
Step 1 - Initialisation
Step 2 - Data
Step 3 - Define model parameters
Step 4 - Validate Inputs
Step 5 - Define apollo probabilities
Step 6 - Estimate model
Step 7 - Print and save output
# --------------------------------------------------------------------------------
### 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/Assignments/Assignment 3")
# 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 = "Model_OL",
modelDescr ="Model ordered logit",
indivID ="CASE"
)
# --------------------------------------------------------------------------------
### Step 2 - Data
# read SPSS file
tbl <- read_csv("Assign3.csv") %>%
mutate(stops = case_when(
WHCSTOPS == 0 ~ "never",
WHCSTOPS == 1 ~ "one",
WHCSTOPS == 2 ~ "two",
WHCSTOPS == 3 ~ "three",
WHCSTOPS == 4 ~ "four"),
stops_factor = factor(stops, ordered = TRUE,
levels = c("never", "one", "two", "three", "four")))
# convert tibble to dataframe, always use variable name database
database <- as.data.frame(tbl)
# --------------------------------------------------------------------------------
### Step 3 - Define model parameters
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(b_dist = 0,
tau_1 = 1,
tau_2 = 2,
tau_3 = 3,
tau_4 = 4)
### 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()
# --------------------------------------------------------------------------------
### 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))
### Utility equation
V = b_dist * DISTANCE
### Create list of probabilities P
P = list()
### Define settings for MNL model component
ol_settings = list(
outcomeOrdered = WHCSTOPS,
V = V,
tau = c(tau_1, tau_2, tau_3, tau_4),
coding = c(0, 1, 2, 3, 4))
### Compute probabilities using MNL model,
P[['model']] = apollo_ol(ol_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)
}
## Optional: calculate LL before model estimation
apollo_llCalc(apollo_beta, apollo_probabilities, apollo_inputs)
# --------------------------------------------------------------------------------
### 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)