I am facing a similar issue with another set of data. I am starting with the simpler MNL model on SP data. In my data, I have two-level choice sets: when an individual choose to travel with public transport in upper level SCEs, he/she will get another set of SCEs for access/egress to the public transport. Looking to this, I have fixed two asc: one for upper level choice, another for lower (access/egress) choice.
In the results, NaNs have been produced for model fit statistics which are shown below.
Also, when I estimate the same model using Maximum Simulated Liklihood instead of HB, I get the message that "Negative definite Hessian with maximum eigenvalue: -0.579" Can you please suggest on this as well?
Code: Select all
### Clear memory
rm(list = ls())
### Load Apollo library
library(apollo)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName = "MNL_SP",
modelDescr = "Pilot MNL model on mode choice SP data with HB",
indivID = "ID",
outputDirectory = "output",
HB = T
)
# ################################################################# #
#### 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("Analysis.csv", header = T)
### for data dictionary, use ?apollo_modeChoiceData
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(asc_car = 0,
asc_pt = -0.28,
asc_ecar = -0.4,
asc_ebike = -0.38,
asc_escoot = 4.38,
asc_aebike = -1.7,
asc_aescoot = -4.30,
asc_awalk = 0,
b_car_ivtt = -0.1,
b_car_egt = -0.05,
b_car_tc = -0.21,
b_car_pc = -0.25,
b_pt_ivtt = -0.11,
b_pt_act = -0.03,
b_pt_egt = -0.1,
b_pt_wt = -0.1,
b_pt_tc = 0,
b_ecar_ivtt = -0.11,
b_ecar_act = -0.1,
b_ecar_egt = -0.14,
b_ecar_tc = -0.23,
b_ecar_av = 0,
b_ecar_wt = -0.02,
b_ebike_ivtt = -0.05,
b_ebike_act = 0,
b_ebike_egt = -0.06,
b_ebike_tc = -0.52,
b_ebike_av = -0.01,
b_ebike_wt = -0.38,
b_escoot_ivtt = -0.15,
b_escoot_act = -0.21,
b_escoot_egt = -0.27,
b_escoot_tc = -0.13,
b_escoot_av = -0.04,
b_escoot_wt = -0.59,
b_aebike_ivtt = -0.02,
b_aebike_wk = -0.01,
b_aebike_tc = -0.43,
b_aebike_av = 0,
b_aebike_wt = -0.15,
b_aescoot_ivtt = -0.24,
b_aescoot_wk = 0,
b_aescoot_tc = -0.3,
b_aescoot_av = 0,
b_aescoot_wt = 0,
b_awalk = -0.06)
# b_age = 0,
# b_female = 0,
# b_hhinc = 0)
apollo_HB=list(hbDist = c(asc_car = "NR",
asc_pt = "N",
asc_ecar = "N",
asc_ebike = "N",
asc_escoot = "N",
asc_aebike = "N",
asc_aescoot = "N",
asc_awalk = "NR",
b_car_ivtt = "LN-",
b_car_egt = "N",
b_car_tc = "LN-",
b_car_pc = "LN-",
b_pt_ivtt = "LN-",
b_pt_act = "LN-",
b_pt_egt = "LN-",
b_pt_wt = "LN-",
b_pt_tc = "LN-",
b_ecar_ivtt = "LN-",
b_ecar_act = "LN-",
b_ecar_egt = "LN-",
b_ecar_tc = "LN-",
b_ecar_av = "N",
b_ecar_wt = "LN-",
b_ebike_ivtt = "LN-",
b_ebike_act = "LN-",
b_ebike_egt = "LN-",
b_ebike_tc = "LN-",
b_ebike_av = "N",
b_ebike_wt = "LN-",
b_escoot_ivtt = "LN-",
b_escoot_act = "LN-",
b_escoot_egt = "LN-",
b_escoot_tc = "LN-",
b_escoot_av = "N",
b_escoot_wt = "LN-",
b_aebike_ivtt = "LN-",
b_aebike_wk = "LN-",
b_aebike_tc = "LN-",
b_aebike_av = "N",
b_aebike_wt = "LN-",
b_aescoot_ivtt = "LN-",
b_aescoot_wk = "LN-",
b_aescoot_tc = "LN-",
b_aescoot_av = "N",
b_aescoot_wt = "LN-",
b_awalk = "LN-"),
gNCREP = 50000, # burn-in iterations
gNEREP = 20000, # post burn-in iterations
gINFOSKIP = 500
)
### 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", "asc_awalk")
# ################################################################# #
#### 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[["car"]] = asc_car + b_car_ivtt*car_ivtt + b_car_egt*car_egt + b_car_tc*car_tc + b_car_pc*car_pc #+ b_age*age + b_female*female + b_hhinc*hhinc
V[["pt"]] = asc_pt + b_pt_ivtt*pt_ivtt + b_pt_act*pt_act + b_pt_egt*pt_egt + b_pt_wt*pt_wt + b_pt_tc*pt_tc #+ b_age*age + b_female*female + b_hhinc*hhinc
V[["ecar"]] = asc_ecar + b_ecar_ivtt*ecar_ivtt + b_ecar_act*ecar_act + b_ecar_egt*ecar_egt + b_ecar_tc*ecar_tc + b_ecar_av*ecar_av + b_ecar_wt*ecar_wt #+ b_age*age + b_female*female + b_hhinc*hhinc
V[["ebike"]] = asc_ebike + b_ebike_ivtt*ebike_ivtt + b_ebike_act*ebike_act + b_ebike_egt*ebike_egt + b_ebike_tc*ebike_tc + b_ebike_av*ebike_av + b_ebike_wt*ebike_wt #+ b_age*age + b_female*female + b_hhinc*hhinc
V[["escoot"]] = asc_escoot + b_escoot_ivtt*escoot_ivtt + b_escoot_act*escoot_act + b_escoot_egt*escoot_egt + b_escoot_tc*escoot_tc + b_escoot_av*escoot_av + b_escoot_wt*escoot_wt #+ b_age*age + b_female*female + b_hhinc*hhinc
V[["aebike"]] = asc_aebike + b_aebike_ivtt*aebike_ivtt + b_aebike_wk*aebike_wk + b_aebike_tc*aebike_tc + b_aebike_av*aebike_av + b_aebike_wt*aebike_wt #+ b_age*age + b_female*female + b_hhinc*hhinc
V[["aescoot"]] = asc_aescoot + b_aescoot_ivtt*aescoot_ivtt + b_aescoot_wk*aescoot_wk + b_aescoot_tc*aescoot_tc + b_aescoot_av*aescoot_av + b_aescoot_wt*aescoot_wt #+ b_age*age + b_female*female + b_hhinc*hhinc
V[["awalk"]] = asc_awalk + b_awalk*awalk #+ b_age*age + b_female*female + b_hhinc*hhinc
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(car=1, pt=2, ecar=3, ebike=4, escoot=5, aebike=6, aescoot=7, awalk=8),
avail = list(car=av_car, pt=av_pt, ecar=av_ecar, ebike=av_ebike, escoot=av_escoot, aebike=av_aebike, aescoot=av_aescoot, awalk=av_awalk),
choiceVar = choice,
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 ####
# ################################################################# #
modelB = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs, estimate_settings = list(estimationRoutine="BFGS"))
# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #
apollo_modelOutput(modelB)
# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name) ----
# ----------------------------------------------------------------- #
apollo_saveOutput(model)