I was trying to develop a new model framework to investigate commuters’ behavior and response toward the presence of Light Rapid Transit with two levels of nested logit model framework. The nested logit model framework is attached in this post where the upper level represents commuters mode choice (LRT and non-LRT) while the lower level indicates the travel characteristics (simple trip-chain and complex trip-chain).
However, the error came up when testing the influence of parameters was conducted as follows:
"Error in apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, :
Parameter b_gender does not influence the log-likelihood of your model!"
When I tried to waived that particular parameter (i.e. b_gender), the same error message still appeared but with other parameters (i.e. b_age1, b_age2, etc.).
My modified code is as follows:
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 ="Apollo_LRT_NL_modeAboveTripChain_02",
modelDescr ="Two-level NL model with SP data where mode choice is above the trip chain typology",
indivID ="ID"
)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
database = read.csv("apollo_statedPreferenceData_NL.csv",header=TRUE)
### Use only SP data
# database = subset(database,database$SP==1)
### Create new variable with average income
# database$mean_income = mean(database$income)
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(asc_lrt_stc = 0,
asc_lrt_ctc = 0,
asc_nonlrt_stc = 0,
asc_nonlrt_ctc = 0,
#asc_bus_shift_female = 0,
#asc_air_shift_female = 0,
#asc_rail_shift_female = 0,
#b_tt_shift_business = 0,
b_stc = 0,
b_ctc = 0,
b_lrt_cost1 = 0,
b_lrt_cost2 = 0,
b_lrt_cost3 = 0,
b_lrt_time1 = 0,
b_lrt_time2 = 0,
b_lrt_waiting1 = 0,
b_lrt_waiting2 = 0,
b_lrt_transfer = 0,
b_nonlrt_cost1 = 0,
b_nonlrt_cost2 = 0,
b_nonlrt_cost3 = 0,
b_nonlrt_time1 = 0,
b_nonlrt_time2 = 0,
b_nonlrt_waiting1 = 0,
b_nonlrt_waiting2 = 0,
b_nonlrt_transfer = 0,
b_gender = 0,
b_age1 = 0,
b_age2 = 0,
b_income1 = 0,
b_income2 = 0,
b_veh_av = 0,
b_car_av = 0,
b_work = 0,
#b_cost_shift_business = 0,
#cost_income_elast = 0,
#b_no_frills = 0,
#b_wifi = 0,
#b_food = 0,
lambda_nonLRT = 0.7,
lambda_LRT = 0.3)
### 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_lrt_stc","b_nonlrt_transfer")
### Read in starting values for at least some parameters from existing model output file
apollo_beta=apollo_readBeta(apollo_beta,apollo_fixed,"Apollo_MNL_example_estimates",overwriteFixed=FALSE)
# ################################################################# #
#### 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()
### Create alternative specific constants and coefficients using interactions with socio-demographics
#asc_lrt_ctc_value = asc_lrt_ctc + b_gender*gender + b_age1*age1 + b_age2*age2 + b_income1*income1 + b_income2*income2 + b_veh_av*veh_av + b_car_av*car_av + b_work*work + b_ctc*ctc
#asc_nonlrt_stc_value = asc_nonlrt_stc + b_gender*gender + b_age1*age1 + b_age2*age2 + b_income1*income1 + b_income2*income2 + b_veh_av*veh_av + b_car_av*car_av + b_work*work
#asc_nonlrt_ctc_value = asc_nonlrt_ctc + b_gender*gender + b_age1*age1 + b_age2*age2 + b_income1*income1 + b_income2*income2 + b_veh_av*veh_av + b_car_av*car_av + b_work*work
#b_tt_car_value = b_tt_car + b_tt_shift_business * business
#b_tt_bus_value = b_tt_bus + b_tt_shift_business * business
#b_tt_air_value = b_tt_air + b_tt_shift_business * business
#b_tt_rail_value = b_tt_rail + b_tt_shift_business * business
#b_cost_value = ( b_cost + b_cost_shift_business * business ) * ( income / mean_income ) ^ cost_income_elast
### List of utilities: these must use the same names as in nl_settings, order is irrelevant
V = list()
V[['lrt_stc']] = asc_lrt_stc + b_lrt_cost1*(cost==1) + b_lrt_cost2*(cost==2) + b_lrt_cost3*(cost==3) + b_lrt_time1*(time==1) + b_lrt_time2*(time==2) + b_lrt_waiting1*(waiting==1) + b_lrt_waiting2*(waiting==2) + b_lrt_transfer*transfer + b_age1*(age==1) + b_age2*(age==2) + b_income1*(income==1) + b_income2*(income==2) + b_veh_av*veh_av + b_car_av*car_av + b_work*work + b_stc*(tripchain==1) + b_gender*gender #+b_tt_car_value * time_car + b_cost_value * cost_car
V[['lrt_ctc']] = asc_lrt_ctc + b_lrt_cost1*(cost==1) + b_lrt_cost2*(cost==2) + b_lrt_cost3*(cost==3) + b_lrt_time1*(time==1) + b_lrt_time2*(time==2) + b_lrt_waiting1*(waiting==1) + b_lrt_waiting2*(waiting==2) + b_lrt_transfer*transfer + b_age1*(age==1) + b_age2*(age==2) + b_income1*(income==1) + b_income2*(income==2) + b_veh_av*veh_av + b_car_av*car_av + b_work*work + b_ctc*(tripchain==2) + b_gender*gender #+ b_tt_bus_value * time_bus + b_access * access_bus + b_cost_value * cost_bus
V[['nonlrt_stc']] = asc_nonlrt_stc + b_nonlrt_cost1*(cost==1) + b_nonlrt_cost2*(cost==2) + b_nonlrt_cost3*(cost==3) + b_nonlrt_time1*(time==1) + b_nonlrt_time2*(time==2) + b_nonlrt_waiting1*(waiting==1) + b_nonlrt_waiting2*(waiting==2) + b_nonlrt_transfer*transfer + b_age1*(age==1) + b_age2*(age==2) + b_income1*(income==1) + b_income2*(income==2) + b_veh_av*veh_av + b_car_av*car_av + b_work*work + b_gender*gender #+ b_tt_air_value * time_air + b_access * access_air + b_cost_value * cost_air + b_no_frills * ( service_air == 1 ) + b_wifi * ( service_air == 2 ) + b_food * ( service_air == 3 )
V[['nonlrt_ctc']] = asc_nonlrt_ctc + b_nonlrt_cost1*(cost==1) + b_nonlrt_cost2*(cost==2) + b_nonlrt_cost3*(cost==3) + b_nonlrt_time1*(time==1) + b_nonlrt_time2*(time==2) + b_nonlrt_waiting1*(waiting==1) + b_nonlrt_waiting2*(waiting==2) + b_nonlrt_transfer*transfer + b_age1*(age==1) + b_age2*(age==2) + b_income1*(income==1) + b_income2*(income==2) + b_veh_av*veh_av + b_car_av*car_av + b_work*work + b_gender*gender #+b_tt_rail_value * time_rail + b_access * access_rail + b_cost_value * cost_rail + b_no_frills * ( service_rail == 1 ) + b_wifi * ( service_rail == 2 ) + b_food * ( service_rail == 3 )
### Specify nests for NL model
nlNests = list(root=1, LRT=lambda_LRT, nonLRT=lambda_nonLRT)
### Specify tree structure for NL model
nlStructure= list()
nlStructure[["root"]] = c("LRT","nonLRT")
nlStructure[["LRT"]] = c("lrt_stc","lrt_ctc")
nlStructure[["nonLRT"]] = c("nonlrt_stc", "nonlrt_ctc")
### Define settings for NL model
nl_settings <- list(
alternatives = c(lrt_stc=1, lrt_ctc=2, nonlrt_stc=3, nonlrt_ctc=4),
avail = list(lrt_stc=av_alt1, lrt_ctc=av_alt2, nonlrt_stc=av_alt3, nonlrt_ctc=av_alt4),
choiceVar = choice,
V = V,
nlNests = nlNests,
nlStructure = nlStructure
)
### Compute probabilities using NL model
P[["model"]] = apollo_nl(nl_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)
# ################################################################# #
##### POST-PROCESSING ####
# ################################################################# #
### Print outputs of additional diagnostics to new output file (remember to close file writing when complete)
sink(paste(model$apollo_control$modelName,"_additional_output.txt",sep=""),split=TRUE)
# ----------------------------------------------------------------- #
#---- LR TEST AGAINST MNL MODEL ----
# ----------------------------------------------------------------- #
apollo_lrTest("Apollo_MNL_example", "Apollo_NL_example")
apollo_lrTest("Apollo_MNL_example", model)
# ----------------------------------------------------------------- #
#---- switch off writing to file ----
# ----------------------------------------------------------------- #
if(sink.number()>0) sink()