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. This may take a day or two at busy times. There is no need to submit the post multiple times.

ICLV model convergence

Ask questions about errors you encouunter. Please make sure to include full details about your model specifications, and ideally your model file.
Post Reply
eeshanbhaduri
Posts: 5
Joined: 11 Nov 2020, 08:44

ICLV model convergence

Post by eeshanbhaduri »

Hi Stephane and David,

I am working on an ICLV model which has been running into convergence errors. The error message:

Error in maxOptim(fn = fn, grad = grad, hess = hess, start = start, method = "BFGS", :
NA in the initial gradient

I tried with scaleAfterConvergence=False, which results in estimation but the LV indicator (zeta) and thresholds (tau) blow up. I have already built MNL and ML models with the same dataset, which produced acceptable results. Moreover, I have checked the structural part, which seems to be ok.

Could you please help me with this issue?

Estimates:
Estimate s.e. t.rat.(0) Rob.s.e.
asc_car 0.00000 NA NA NA
asc_rhs -0.65585 NaN NaN 1.833167
asc_taxi -0.68284 NaN NaN 1.585281
asc_bus -2.42742 NaN NaN 2.239964
asc_metro -2.25005 NaN NaN 2.212473
asc_auto -2.07023 NaN NaN 2.248978
b_tt_car_group -0.07394 0.003941 -18.762 0.020513
b_tt_PT_group -0.05559 0.002752 -20.199 0.007417
b_tt_SD_car_group -0.07773 0.011495 -6.762 0.020896
b_tt_SD_PT_group -0.03061 0.003568 -8.580 0.013442
b_wt_car_group -0.03892 0.004513 -8.624 0.016325
b_wt_PT_group -0.01741 0.001877 -9.276 0.002311
b_crowd_lvl1 -0.21544 0.022350 -9.639 0.070759
b_crowd_lvl2 -0.23236 0.020842 -11.149 0.068856
b_flex_detour 0.10507 0.021958 4.785 0.034389
b_flex_avlbl 0.11495 NaN NaN 2.337639
b_cost -0.01723 6.1143e-04 -28.186 0.001900
b_cost_sharing 0.01806 6.3399e-04 28.482 8.3286e-04
cost_income_elast -0.41934 0.025635 -16.358 0.041267
lambda_mulmod_car -0.18554 NaN NaN 0.380622
gamma_female 0.27449 0.056322 4.874 0.285596
zeta_mulmod_cost_sensitivity 1.89398 0.060736 31.184 0.189758
zeta_mulmod_travel_need 1.97506 0.068228 28.948 0.123740
zeta_mulmod_comfort 111.93278 12.802603 8.743 10.250963
tau_mulmod_cost_sensitivity_1 -6.60821 0.257760 -25.637 0.985355
tau_mulmod_cost_sensitivity_2 -3.72794 NaN NaN 1.062607
tau_mulmod_cost_sensitivity_3 -3.14588 NaN NaN 1.097277
tau_mulmod_cost_sensitivity_4 -1.82066 NaN NaN 1.135079
tau_mulmod_cost_sensitivity_5 -0.34802 NaN NaN 1.269961
tau_mulmod_cost_sensitivity_6 3.17523 NaN NaN 0.930441
tau_mulmod_travel_need_1 -6.85643 0.273675 -25.053 1.083728
tau_mulmod_travel_need_2 -4.02307 NaN NaN 1.059733
tau_mulmod_travel_need_3 -3.41325 NaN NaN 1.042428
tau_mulmod_travel_need_4 -1.79043 NaN NaN 1.057745
tau_mulmod_travel_need_5 -0.31703 NaN NaN 1.068302
tau_mulmod_travel_need_6 3.91792 0.061268 63.947 0.676999
tau_mulmod_comfort_1 -199.26332 20.552124 -9.696 36.999974
tau_mulmod_comfort_2 -146.35060 15.267704 -9.586 45.561683
tau_mulmod_comfort_3 -115.42219 10.031044 -11.506 52.430491
tau_mulmod_comfort_4 -68.73936 NaN NaN 56.074071
tau_mulmod_comfort_5 -5.90809 NaN NaN 76.281732
tau_mulmod_comfort_6 161.15898 18.698930 8.619 36.902923
mu_pf 1.75807 0.078390 22.427 0.202243
mu_ps 3.76081 0.193334 19.452 0.567017
mu_fc 1.00000 NA NA NA
stephanehess
Site Admin
Posts: 1039
Joined: 24 Apr 2020, 16:29

Re: ICLV model convergence

Post by stephanehess »

Hi

apologies for the slow response, the forum was offline due to a technical issue.

We need to see the actual code in order to be able to understand what could be going wrong.

When you receive that error, does it happen in the second stage estimation, i.e. after rescaling?

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
eeshanbhaduri
Posts: 5
Joined: 11 Nov 2020, 08:44

Re: ICLV model convergence

Post by eeshanbhaduri »

Hi,
yes, the error happens in the second stage estimation.

Please find the code below.

Best,
Eeshan

Code:::

# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################################################# #
install.packages("apollo")
install.packages("ggcorrplot")
version

### Load Apollo library
#install.packages("apollo")
library(apollo)
library(readxl)
library(tidyverse)
library(ggcorrplot)

### Clear memory
rm(list = ls())

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
modelName = "MNL_RP_SP_Kolkata",
modelDescr = "Kolkata SP model FINAL",
indivID = "sur_mk_id",
nCores = 4,
outputDirectory = "output"
)

# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #

database <- read_excel("Full_dataset_KOLKATA_merged_MOD_V11.xlsx", sheet="Final Merged_Choice wise")

database <- arrange(database, desc(sur_mk_id))

database <- filter(database, !is.na(mode_choice))

database <- filter(database, !(mode_choice == "Car" & car_count == 0))
database <- filter(database, !(block_id == 5 & set==3))


database <- mutate(database, choice = ifelse(mode_choice == "Car",1,
ifelse(mode_choice == "Ride-hailing",2,
ifelse(mode_choice == "Taxi",3,
ifelse(mode_choice == "Bus",4,
ifelse(mode_choice == "Metro rail",5,6))))))
ifelse(arkshw_crowd == 75,1,0)))

#adding the LV dummy variables

database <- mutate(database, rhsatt_interface_ordered = ifelse(rhsatt_interface == "Strongly agree",7,
ifelse(rhsatt_interface == "Agree",6,
ifelse(rhsatt_interface == "Somewhat agree",5,
ifelse(rhsatt_interface == "Neither agree nor disagree",4,
ifelse(rhsatt_interface == "Somewhat disagree",3,
ifelse(rhsatt_interface == "Disagree",2,1)))))))

database <- mutate(database, rhsatt_payment_ordered = ifelse(rhsatt_payment == "Strongly agree",7,
ifelse(rhsatt_payment == "Agree",6,
ifelse(rhsatt_payment == "Somewhat agree",5,
ifelse(rhsatt_payment == "Neither agree nor disagree",4,
ifelse(rhsatt_payment == "Somewhat disagree",3,
ifelse(rhsatt_payment == "Disagree",2,1)))))))

database <- mutate(database, rhsatt_trustworthy_ordered = ifelse(rhsatt_trustworthy == "Strongly agree",7,
ifelse(rhsatt_trustworthy == "Agree",6,
ifelse(rhsatt_trustworthy == "Somewhat agree",5,
ifelse(rhsatt_trustworthy == "Neither agree nor disagree",4,
ifelse(rhsatt_trustworthy == "Somewhat disagree",3,
ifelse(rhsatt_trustworthy == "Disagree",2,1)))))))

#adding the availability
database <- mutate(database, car_avl = ifelse(car_count == 0,0,1))
### Create new variable with average income
database$mean_income_value = mean(database$income_value)

# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(asc_car = 0,
asc_rhs = 0,
asc_taxi = 0,
asc_bus = 0,
asc_metro = 0,
asc_auto = 0,


b_tt_generic = 0,


b_tt_SD_generic = 0,

b_wt_generic = 0,


b_crowd_generic = 0,


b_flex_detour = 0,
b_flex_avlbl = 0,


b_cost = 0,

############# Coefficient for the LV parameter #########################

lambda_rhs_att_car = 0,

############# Covariates for the LV parameter #########################

gamma_female = 0,

############# Indicators for the LV parameter #########################
# LV = Attitude towards Ride-hailing services
zeta_rhsatt_interface = 1,
zeta_rhsatt_payment = 1,
zeta_rhsatt_trustworthy = 1,

############# Thresholds for the LV parameter #########################
# LV = Attitude towards Ride-hailing services
tau_rhsatt_interface_1 =-3,
tau_rhsatt_interface_2 =-2,
tau_rhsatt_interface_3 = -1,
tau_rhsatt_interface_4 = 1,
tau_rhsatt_interface_5 = 2,
tau_rhsatt_interface_6 = 3,

tau_rhsatt_payment_1 =-3,
tau_rhsatt_payment_2 =-2,
tau_rhsatt_payment_3 = -1,
tau_rhsatt_payment_4 = 1,
tau_rhsatt_payment_5 = 2,
tau_rhsatt_payment_6 = 3,

tau_rhsatt_trust_1 =-3,
tau_rhsatt_trust_2 =-2,
tau_rhsatt_trust_3 = -1,
tau_rhsatt_trust_4 = 1,
tau_rhsatt_trust_5 = 2,
tau_rhsatt_trust_6 = 3,


mu_pf = 1,
mu_ps = 1,
mu_fc = 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_car", "mu_fc")

### Read in starting values for at least some parameters from existing model output file
#apollo_beta = apollo_readBeta(apollo_beta, apollo_fixed, "MNL_SP", overwriteFixed=FALSE)

# ################################################################# #
#### DEFINE RANDOM COMPONENTS ####
# ################################################################# #

### Set parameters for generating draws
apollo_draws = list(
interDrawsType = "halton",
interNDraws = 500,
interUnifDraws = c(),
interNormDraws = c("eta"),

intraDrawsType = "",
intraNDraws = 0,
intraUnifDraws = c(),
intraNormDraws = c()
)

### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
randcoeff = list()


randcoeff[["LV_rhs_att"]] = gamma_female * gender_dummy + eta


return(randcoeff)
}


# ################################################################# #
#### 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()

### Likelihood of indicators
ol_settings1 = list(outcomeOrdered = rhsatt_interface_ordered ,
V = zeta_rhsatt_interface*LV_rhs_att,
tau = list(tau_rhsatt_interface_1, tau_rhsatt_interface_2, tau_rhsatt_interface_3,
tau_rhsatt_interface_4, tau_rhsatt_interface_5, tau_rhsatt_interface_6),
rows = (pref_first==1),
componentName = "rhsatt_interface")
ol_settings2 = list(outcomeOrdered = rhsatt_payment_ordered ,
V = zeta_rhsatt_payment*LV_rhs_att,
tau = list(tau_rhsatt_payment_1, tau_rhsatt_payment_2, tau_rhsatt_payment_3,
tau_rhsatt_payment_4, tau_rhsatt_payment_5, tau_rhsatt_payment_6),
rows = (pref_first==1),
componentName = "rhsatt_payment")
ol_settings3 = list(outcomeOrdered = rhsatt_trustworthy_ordered ,
V = zeta_rhsatt_trustworthy*LV_rhs_att,
tau = list(tau_rhsatt_trust_1, tau_rhsatt_trust_2, tau_rhsatt_trust_3,
tau_rhsatt_trust_4, tau_rhsatt_trust_5, tau_rhsatt_trust_6),
rows = (pref_first==1),
componentName = "rhsatt_trustworthy")


P[["rhsatt_interface"]] = apollo_ol(ol_settings1, functionality)
P[["rhsatt_payment"]] = apollo_ol(ol_settings2, functionality)
P[["rhsatt_trustworthy"]] = apollo_ol(ol_settings3, functionality)


### Likelihood of choices
### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[['car']] = asc_car + b_cost *car_cost + b_tt_generic * car_tt + b_tt_SD_generic * car_tt_SD +
b_flex_detour * car_flex_D2 + b_flex_avlbl * car_flex_D3 +
lambda_rhs_att_car * LV_rhs_att

V[['rhs']] = asc_rhs + b_cost * rhs_cost + b_tt_generic * rhs_tt + b_tt_SD_generic * rhs_tt_SD +
b_wt_generic * rhs_wt + b_flex_detour * rhs_flex_D2

V[['taxi']] = asc_taxi + b_cost * taxi_cost + b_tt_generic * taxi_tt + b_tt_SD_generic * taxi_tt_SD +
b_wt_generic * taxi_wt + b_flex_detour * taxi_flex_D2

V[['bus']] = asc_bus + b_cost * bus_cost + b_tt_generic * bus_tt + b_tt_SD_generic * bus_tt_SD +
b_wt_generic * bus_wt + b_crowd_generic * bus_crowd

V[['metro']] = asc_metro + b_cost * metro_cost + b_tt_generic * metro_tt + b_tt_SD_generic * metro_tt_SD +
b_wt_generic * metro_wt + b_crowd_generic * metro_crowd

V[['auto']] = asc_auto + b_cost * arkshw_cost + b_tt_generic * arkshw_tt + b_tt_SD_generic * arkshw_tt_SD +
b_wt_generic * arkshw_wt + b_flex_detour * arkshw_flex_D2


### Compute probabilities for the first preference part of the data using MNL model
mnl_settings_pref_first = list(
alternatives = c(car=1, rhs=2, taxi=3),
avail = list(car=car_avl, rhs=1, taxi=1),
choiceVar = choice,
utilities = list(car = mu_pf*V[["car"]],
rhs = mu_pf*V[["rhs"]],
taxi = mu_pf*V[["taxi"]]),
rows = (pref_first ==1),
componentName='pref_first'
)

### Compute probabilities using MNL model
P[["pref_first"]] = apollo_mnl(mnl_settings_pref_first, functionality)

### Compute probabilities for the second preference part of the data using MNL model
mnl_settings_pref_second = list(
alternatives = c(bus=4, metro=5, auto=6),
avail = list(bus=1, metro=1, auto=1),
choiceVar = choice,
utilities = list(bus = mu_ps*V[["bus"]],
metro = mu_ps*V[["metro"]],
auto = mu_ps*V[["auto"]]),
rows = (pref_second ==1),
componentName='pref_second'
)

### Compute probabilities using MNL model
P[["pref_second"]] = apollo_mnl(mnl_settings_pref_second, functionality)

### Compute probabilities for the final choice part of the data using MNL model
mnl_settings_final_choice = list(
alternatives = c(car=1, rhs=2, taxi=3, bus=4, metro=5, auto=6),
avail = list(car=car_avl, rhs=1, taxi=1, bus=1, metro=1, auto=1),
choiceVar = choice,
utilities = list(car = mu_fc*V[["car"]],
rhs = mu_fc*V[["rhs"]],
taxi = mu_fc*V[["taxi"]],
bus = mu_fc*V[["bus"]],
metro = mu_fc*V[["metro"]],
auto = mu_fc*V[["auto"]]),
rows = (final_choice ==1),
componentName='final_choice'
)

### Compute probabilities using MNL model
P[["final_choice"]] = apollo_mnl(mnl_settings_final_choice, functionality)

### Average across intra-individual draws
#P = apollo_avgIntraDraws(P, apollo_inputs, functionality)

### Combined model
P = apollo_combineModels(P, apollo_inputs, functionality)

### Take product across observation for same individual
P = apollo_panelProd(P, apollo_inputs, functionality)

### Average across inter-individual draws
P = apollo_avgInterDraws(P, apollo_inputs, functionality)

### Prepare and return outputs of function
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}

# ################################################################# #
#### MODEL ESTIMATION ####
# ################################################################# #
#apollo_beta=apollo_searchStart(apollo_beta, apollo_fixed,apollo_probabilities, apollo_inputs)

### Optional: calculate LL before model estimation
#apollo_llCalc(apollo_beta, apollo_probabilities, apollo_inputs)

model = apollo_estimate(apollo_beta, apollo_fixed,
apollo_probabilities, apollo_inputs, estimate_settings=list(hessianRoutine="maxLik"))

# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #

# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #

apollo_modelOutput(model,modelOutput_settings=list(printOutliers=10))

# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS ####
# ################################################################# #
install.packages("apollo")
install.packages("ggcorrplot")
version

### Load Apollo library
#install.packages("apollo")
library(apollo)
library(readxl)
library(tidyverse)
library(ggcorrplot)

### Clear memory
rm(list = ls())

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
modelName = "MNL_RP_SP_Kolkata",
modelDescr = "Kolkata SP model FINAL",
indivID = "sur_mk_id",
nCores = 4,
outputDirectory = "output"
)

# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #

database <- read_excel("Full_dataset_KOLKATA_merged_MOD_V11.xlsx", sheet="Final Merged_Choice wise")

database <- arrange(database, desc(sur_mk_id))

database <- filter(database, !is.na(mode_choice))

database <- filter(database, !(mode_choice == "Car" & car_count == 0))
database <- filter(database, !(block_id == 5 & set==3))


database <- mutate(database, choice = ifelse(mode_choice == "Car",1,
ifelse(mode_choice == "Ride-hailing",2,
ifelse(mode_choice == "Taxi",3,
ifelse(mode_choice == "Bus",4,
ifelse(mode_choice == "Metro rail",5,6))))))
ifelse(arkshw_crowd == 75,1,0)))

#adding the LV dummy variables

database <- mutate(database, rhsatt_interface_ordered = ifelse(rhsatt_interface == "Strongly agree",7,
ifelse(rhsatt_interface == "Agree",6,
ifelse(rhsatt_interface == "Somewhat agree",5,
ifelse(rhsatt_interface == "Neither agree nor disagree",4,
ifelse(rhsatt_interface == "Somewhat disagree",3,
ifelse(rhsatt_interface == "Disagree",2,1)))))))

database <- mutate(database, rhsatt_payment_ordered = ifelse(rhsatt_payment == "Strongly agree",7,
ifelse(rhsatt_payment == "Agree",6,
ifelse(rhsatt_payment == "Somewhat agree",5,
ifelse(rhsatt_payment == "Neither agree nor disagree",4,
ifelse(rhsatt_payment == "Somewhat disagree",3,
ifelse(rhsatt_payment == "Disagree",2,1)))))))

database <- mutate(database, rhsatt_trustworthy_ordered = ifelse(rhsatt_trustworthy == "Strongly agree",7,
ifelse(rhsatt_trustworthy == "Agree",6,
ifelse(rhsatt_trustworthy == "Somewhat agree",5,
ifelse(rhsatt_trustworthy == "Neither agree nor disagree",4,
ifelse(rhsatt_trustworthy == "Somewhat disagree",3,
ifelse(rhsatt_trustworthy == "Disagree",2,1)))))))

#adding the availability
database <- mutate(database, car_avl = ifelse(car_count == 0,0,1))
### Create new variable with average income
database$mean_income_value = mean(database$income_value)

# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(asc_car = 0,
asc_rhs = 0,
asc_taxi = 0,
asc_bus = 0,
asc_metro = 0,
asc_auto = 0,


b_tt_generic = 0,


b_tt_SD_generic = 0,

b_wt_generic = 0,


b_crowd_generic = 0,


b_flex_detour = 0,
b_flex_avlbl = 0,


b_cost = 0,

############# Coefficient for the LV parameter #########################

lambda_rhs_att_car = 0,

############# Covariates for the LV parameter #########################

gamma_female = 0,

############# Indicators for the LV parameter #########################
# LV = Attitude towards Ride-hailing services
zeta_rhsatt_interface = 1,
zeta_rhsatt_payment = 1,
zeta_rhsatt_trustworthy = 1,

############# Thresholds for the LV parameter #########################
# LV = Attitude towards Ride-hailing services
tau_rhsatt_interface_1 =-3,
tau_rhsatt_interface_2 =-2,
tau_rhsatt_interface_3 = -1,
tau_rhsatt_interface_4 = 1,
tau_rhsatt_interface_5 = 2,
tau_rhsatt_interface_6 = 3,

tau_rhsatt_payment_1 =-3,
tau_rhsatt_payment_2 =-2,
tau_rhsatt_payment_3 = -1,
tau_rhsatt_payment_4 = 1,
tau_rhsatt_payment_5 = 2,
tau_rhsatt_payment_6 = 3,

tau_rhsatt_trust_1 =-3,
tau_rhsatt_trust_2 =-2,
tau_rhsatt_trust_3 = -1,
tau_rhsatt_trust_4 = 1,
tau_rhsatt_trust_5 = 2,
tau_rhsatt_trust_6 = 3,


mu_pf = 1,
mu_ps = 1,
mu_fc = 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_car", "mu_fc")

### Read in starting values for at least some parameters from existing model output file
#apollo_beta = apollo_readBeta(apollo_beta, apollo_fixed, "MNL_SP", overwriteFixed=FALSE)

# ################################################################# #
#### DEFINE RANDOM COMPONENTS ####
# ################################################################# #

### Set parameters for generating draws
apollo_draws = list(
interDrawsType = "halton",
interNDraws = 500,
interUnifDraws = c(),
interNormDraws = c("eta"),

intraDrawsType = "",
intraNDraws = 0,
intraUnifDraws = c(),
intraNormDraws = c()
)

### Create random parameters
apollo_randCoeff = function(apollo_beta, apollo_inputs){
randcoeff = list()


randcoeff[["LV_rhs_att"]] = gamma_female * gender_dummy + eta


return(randcoeff)
}


# ################################################################# #
#### 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()

### Likelihood of indicators
ol_settings1 = list(outcomeOrdered = rhsatt_interface_ordered ,
V = zeta_rhsatt_interface*LV_rhs_att,
tau = list(tau_rhsatt_interface_1, tau_rhsatt_interface_2, tau_rhsatt_interface_3,
tau_rhsatt_interface_4, tau_rhsatt_interface_5, tau_rhsatt_interface_6),
rows = (pref_first==1),
componentName = "rhsatt_interface")
ol_settings2 = list(outcomeOrdered = rhsatt_payment_ordered ,
V = zeta_rhsatt_payment*LV_rhs_att,
tau = list(tau_rhsatt_payment_1, tau_rhsatt_payment_2, tau_rhsatt_payment_3,
tau_rhsatt_payment_4, tau_rhsatt_payment_5, tau_rhsatt_payment_6),
rows = (pref_first==1),
componentName = "rhsatt_payment")
ol_settings3 = list(outcomeOrdered = rhsatt_trustworthy_ordered ,
V = zeta_rhsatt_trustworthy*LV_rhs_att,
tau = list(tau_rhsatt_trust_1, tau_rhsatt_trust_2, tau_rhsatt_trust_3,
tau_rhsatt_trust_4, tau_rhsatt_trust_5, tau_rhsatt_trust_6),
rows = (pref_first==1),
componentName = "rhsatt_trustworthy")


P[["rhsatt_interface"]] = apollo_ol(ol_settings1, functionality)
P[["rhsatt_payment"]] = apollo_ol(ol_settings2, functionality)
P[["rhsatt_trustworthy"]] = apollo_ol(ol_settings3, functionality)


### Likelihood of choices
### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[['car']] = asc_car + b_cost *car_cost + b_tt_generic * car_tt + b_tt_SD_generic * car_tt_SD +
b_flex_detour * car_flex_D2 + b_flex_avlbl * car_flex_D3 +
lambda_rhs_att_car * LV_rhs_att

V[['rhs']] = asc_rhs + b_cost * rhs_cost + b_tt_generic * rhs_tt + b_tt_SD_generic * rhs_tt_SD +
b_wt_generic * rhs_wt + b_flex_detour * rhs_flex_D2

V[['taxi']] = asc_taxi + b_cost * taxi_cost + b_tt_generic * taxi_tt + b_tt_SD_generic * taxi_tt_SD +
b_wt_generic * taxi_wt + b_flex_detour * taxi_flex_D2

V[['bus']] = asc_bus + b_cost * bus_cost + b_tt_generic * bus_tt + b_tt_SD_generic * bus_tt_SD +
b_wt_generic * bus_wt + b_crowd_generic * bus_crowd

V[['metro']] = asc_metro + b_cost * metro_cost + b_tt_generic * metro_tt + b_tt_SD_generic * metro_tt_SD +
b_wt_generic * metro_wt + b_crowd_generic * metro_crowd

V[['auto']] = asc_auto + b_cost * arkshw_cost + b_tt_generic * arkshw_tt + b_tt_SD_generic * arkshw_tt_SD +
b_wt_generic * arkshw_wt + b_flex_detour * arkshw_flex_D2


### Compute probabilities for the first preference part of the data using MNL model
mnl_settings_pref_first = list(
alternatives = c(car=1, rhs=2, taxi=3),
avail = list(car=car_avl, rhs=1, taxi=1),
choiceVar = choice,
utilities = list(car = mu_pf*V[["car"]],
rhs = mu_pf*V[["rhs"]],
taxi = mu_pf*V[["taxi"]]),
rows = (pref_first ==1),
componentName='pref_first'
)

### Compute probabilities using MNL model
P[["pref_first"]] = apollo_mnl(mnl_settings_pref_first, functionality)

### Compute probabilities for the second preference part of the data using MNL model
mnl_settings_pref_second = list(
alternatives = c(bus=4, metro=5, auto=6),
avail = list(bus=1, metro=1, auto=1),
choiceVar = choice,
utilities = list(bus = mu_ps*V[["bus"]],
metro = mu_ps*V[["metro"]],
auto = mu_ps*V[["auto"]]),
rows = (pref_second ==1),
componentName='pref_second'
)

### Compute probabilities using MNL model
P[["pref_second"]] = apollo_mnl(mnl_settings_pref_second, functionality)

### Compute probabilities for the final choice part of the data using MNL model
mnl_settings_final_choice = list(
alternatives = c(car=1, rhs=2, taxi=3, bus=4, metro=5, auto=6),
avail = list(car=car_avl, rhs=1, taxi=1, bus=1, metro=1, auto=1),
choiceVar = choice,
utilities = list(car = mu_fc*V[["car"]],
rhs = mu_fc*V[["rhs"]],
taxi = mu_fc*V[["taxi"]],
bus = mu_fc*V[["bus"]],
metro = mu_fc*V[["metro"]],
auto = mu_fc*V[["auto"]]),
rows = (final_choice ==1),
componentName='final_choice'
)

### Compute probabilities using MNL model
P[["final_choice"]] = apollo_mnl(mnl_settings_final_choice, functionality)

### Average across intra-individual draws
#P = apollo_avgIntraDraws(P, apollo_inputs, functionality)

### Combined model
P = apollo_combineModels(P, apollo_inputs, functionality)

### Take product across observation for same individual
P = apollo_panelProd(P, apollo_inputs, functionality)

### Average across inter-individual draws
P = apollo_avgInterDraws(P, apollo_inputs, functionality)

### Prepare and return outputs of function
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}

# ################################################################# #
#### MODEL ESTIMATION ####
# ################################################################# #
#apollo_beta=apollo_searchStart(apollo_beta, apollo_fixed,apollo_probabilities, apollo_inputs)

### Optional: calculate LL before model estimation
#apollo_llCalc(apollo_beta, apollo_probabilities, apollo_inputs)

model = apollo_estimate(apollo_beta, apollo_fixed,
apollo_probabilities, apollo_inputs, estimate_settings=list(hessianRoutine="maxLik"))

# ################################################################# #
#### MODEL OUTPUTS ####
# ################################################################# #

# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN) ----
# ----------------------------------------------------------------- #

apollo_modelOutput(model,modelOutput_settings=list(printOutliers=10))
stephanehess
Site Admin
Posts: 1039
Joined: 24 Apr 2020, 16:29

Re: ICLV model convergence

Post by stephanehess »

This code is not for the results you sent, as you have different parameters.
--------------------------------
Stephane Hess
www.stephanehess.me.uk
eeshanbhaduri
Posts: 5
Joined: 11 Nov 2020, 08:44

Re: ICLV model convergence

Post by eeshanbhaduri »

Hi Stephane,

Apologies. I tried to simplify the model reducing number of variables but the problem still persists (with "rhsatt_interface").

I am attaching the results for the updated code. Please have a look.
Thanks!

Result:

Model run by eurg using Apollo 0.2.8 on R 4.2.1 for Linux.
www.ApolloChoiceModelling.com

Model name : MNL_RP_SP_Kolkata
Model description : Kolkata SP model FINAL
Model run at : 2023-04-25 11:01:42
Estimation method : bfgs
Model diagnosis : successful convergence
Number of individuals : 1478
Number of rows in database : 11027
Number of modelled outcomes : 21026
rhsatt_interface : 3333
rhsatt_payment : 3333
rhsatt_trustworthy : 3333
pref_first : 3333
pref_second : 4021
final_choice : 3673

Number of cores used : 75
Number of inter-individual draws : 500 (halton)

LL(start) : -29869.51
LL (whole model) at equal shares, LL(0) : -32839.59
LL (whole model) at observed shares, LL(C) : -26462.6
LL(final, whole model) : -20409.1
Rho-squared vs equal shares : 0.3785
Adj.Rho-squared vs equal shares : 0.3774
Rho-squared vs observed shares : 0.2288
Adj.Rho-squared vs observed shares : 0.2274
AIC : 40892.19
BIC : 41118.32

LL(0,rhsatt_interface) : -6485.72
LL(final,rhsatt_interface) : -2068.55
LL(0,rhsatt_payment) : -6485.72
LL(final,rhsatt_payment) : -3398.38
LL(0,rhsatt_trustworthy) : -6485.72
LL(final,rhsatt_trustworthy) : -3578.26
LL(0,pref_first) : -2823.17
LL(final,pref_first) : -1687.25
LL(0,pref_second) : -4417.52
LL(final,pref_second) : -4128.23
LL(0,final_choice) : -6141.74
LL(final,final_choice) : -5275.1

Estimated parameters : 37
Time taken (hh:mm:ss) : 00:26:45.85
pre-estimation : 00:04:27.84
estimation : 00:07:4.19
post-estimation : 00:15:13.82
Iterations : 119
Min abs eigenvalue of Hessian : 1.412592
Some eigenvalues of Hessian are positive, indicating potential problems!

Unconstrained optimisation.

These outputs have had the scaling used in estimation applied to them.
Estimates:
Estimate s.e. t.rat.(0) Rob.s.e. Rob.t.rat.(0)
asc_car 0.00000 NA NA NA NA
asc_rhs -0.72995 0.061193 -11.929 0.224585 -3.2502
asc_taxi -0.62744 0.037158 -16.885 0.231819 -2.7066
asc_bus -0.88868 NaN NaN 0.613085 -1.4495
asc_metro -0.64334 NaN NaN 0.700297 -0.9187
asc_auto -1.16182 NaN NaN 0.330376 -3.5166
b_tt_generic -0.08687 0.003867 -22.467 0.004976 -17.4573
b_tt_SD_generic -0.04398 0.005883 -7.477 0.017632 -2.4946
b_wt_generic -0.04300 0.002292 -18.760 0.009542 -4.5068
b_crowd_generic -0.01093 2.5203e-04 -43.366 0.002975 -3.6737
b_flex_detour 0.30069 NaN NaN 0.145782 2.0626
b_flex_avlbl 0.21943 0.067205 3.265 0.186989 1.1735
b_cost -0.01680 5.9111e-04 -28.418 0.001524 -11.0233
lambda_rhs_att_car -0.22712 NaN NaN 0.159305 -1.4257
gamma_female 0.12117 NaN NaN 0.014526 8.3418
zeta_rhsatt_interface 135.60999 13.485271 10.056 28.310398 4.7901
zeta_rhsatt_payment 2.60168 0.072020 36.125 0.175666 14.8104
zeta_rhsatt_trustworthy 2.49447 0.070473 35.396 0.179212 13.9191
tau_rhsatt_interface_1 -340.88868 35.569536 -9.584 69.880196 -4.8782
tau_rhsatt_interface_2 -198.00833 22.639807 -8.746 28.330757 -6.9892
tau_rhsatt_interface_3 -160.75923 17.014042 -9.449 31.054458 -5.1767
tau_rhsatt_interface_4 -83.37484 7.874655 -10.588 20.405833 -4.0858
tau_rhsatt_interface_5 -10.24663 NaN NaN 15.954228 -0.6423
tau_rhsatt_interface_6 192.04068 20.646455 9.301 37.616547 5.1052
tau_rhsatt_payment_1 -8.09702 0.363001 -22.306 0.547125 -14.7992
tau_rhsatt_payment_2 -5.60930 0.158427 -35.406 0.274309 -20.4488
tau_rhsatt_payment_3 -4.44350 0.114495 -38.809 0.229676 -19.3468
tau_rhsatt_payment_4 -2.18494 0.058557 -37.313 0.179128 -12.1977
tau_rhsatt_payment_5 -0.20694 0.044404 -4.660 0.170015 -1.2172
tau_rhsatt_payment_6 4.13437 0.106865 38.688 0.258289 16.0068
tau_rhsatt_trust_1 -7.89241 0.351857 -22.431 0.486134 -16.2350
tau_rhsatt_trust_2 -4.92955 0.136304 -36.166 0.239513 -20.5816
tau_rhsatt_trust_3 -3.91755 0.096652 -40.533 0.218588 -17.9220
tau_rhsatt_trust_4 -1.53729 0.050289 -30.569 0.163256 -9.4165
tau_rhsatt_trust_5 0.15224 0.049807 3.057 0.118548 1.2842
tau_rhsatt_trust_6 4.56511 0.117034 39.007 0.241992 18.8648
mu_pf 1.43086 0.056888 25.152 0.093594 15.2879
mu_ps 1.11410 0.041466 26.868 0.232951 4.7826
mu_fc 1.00000 NA NA NA NA
stephanehess
Site Admin
Posts: 1039
Joined: 24 Apr 2020, 16:29

Re: ICLV model convergence

Post by stephanehess »

Hi

looks like an overspecification. Could you tell me more about your model specification and data, including what the different stages are. And how many tasks there are per person, etc

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
eeshanbhaduri
Posts: 5
Joined: 11 Nov 2020, 08:44

Re: ICLV model convergence

Post by eeshanbhaduri »

Hi,

its a SP choice model where the choices were made in 3 stages. 1st stage includes choice made between 3 modes (car, taxi, ride-hailing) while 2nd stage includes choice made between other 3 modes (bus, metro, auto-rickshaw). Finally respondents made final choice from two chosen alternatives in the previous stages. that's why 3 scale factors are specified (mu_pf, mu_ps, mu_fc) where one is kept fixed and other two are estimated...this part of the specification is more like RP-SP combined model

regarding the alt. specific attributes, I tried to estimate generic parameters for 3 travel time components - in vehicle travel time (tt), travel time reliability (tt_SD), waiting time (wt), and travel cost. in addition to that, model also includes qualitative attributes crowding and flexibility- both dummy coded.

In total, there are about 1478 respondents with 3 SP responses/tasks each (*3 for each stage = 9 rows).

-Eeshan
stephanehess
Site Admin
Posts: 1039
Joined: 24 Apr 2020, 16:29

Re: ICLV model convergence

Post by stephanehess »

Hi

to address the identification issue, can you try a model without that comfort indicator.

As an aside, I assume you are aware that making the third choiceset a function of previous choices creates an endogeneity issue?

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
eeshanbhaduri
Posts: 5
Joined: 11 Nov 2020, 08:44

Re: ICLV model convergence

Post by eeshanbhaduri »

Hi,

1. I tried removing the 'interface' indicator (one coming with blown up value for the updated specification) but it did not solve the problem. Now the other indicator is producing a blown up value. Is it happening because of some spurious correlation? - lil confused because factor analysis or even SEM works fine with same LV indicator dataset.

2. I am aware of the endogeneity issue- thanks for pointing it out. however, we had to rely on 3 stage because showing 6 alt. at once would be too demanding cognitively. As for accounting its effect, I am contemplating to have correlated error terms. Do you think that's a acceptable way? OR kindly let me know if you have any other suggestion regarding it.

Best,
Eeshan

Updated result (after removing 'interface' indicator)
Estimates:
Estimate s.e. t.rat.(0) Rob.s.e. Rob.t.rat.(0)
asc_car 0.000000 NA NA NA NA
asc_rhs -0.664362 0.083655 -7.942 0.108702 -6.112
asc_taxi -0.604933 0.074048 -8.169 0.102642 -5.894
asc_bus -1.299685 0.131339 -9.896 0.195239 -6.657
asc_metro -1.079258 0.136909 -7.883 0.218187 -4.946
asc_auto -1.276161 0.084132 -15.169 0.112516 -11.342
b_tt_generic -0.073646 0.004084 -18.032 0.005179 -14.221
b_tt_SD_generic -0.038267 0.006334 -6.042 0.008237 -4.646
b_wt_generic -0.033727 0.003253 -10.368 0.005034 -6.699
b_crowd_generic -0.007548 8.3875e-04 -8.999 0.001338 -5.641
b_flex_detour 0.181455 0.038377 4.728 0.057410 3.161
b_flex_avlbl 0.212057 0.074426 2.849 0.087476 2.424
b_cost -0.016253 6.8784e-04 -23.629 8.4583e-04 -19.215
lambda_rhs_att_car -0.240339 0.038193 -6.293 0.055745 -4.311
gamma_female 0.419404 0.023764 17.649 0.037990 11.040
zeta_rhsatt_payment 1.998590 0.064641 30.918 0.149033 13.410
zeta_rhsatt_trustworthy 124.250530 19.275431 6.446 24.152770 5.144
tau_rhsatt_payment_1 -7.640754 0.428852 -17.817 0.748026 -10.215
tau_rhsatt_payment_2 -4.669197 0.144968 -32.208 0.211694 -22.056
tau_rhsatt_payment_3 -3.760605 0.104920 -35.843 0.161581 -23.274
tau_rhsatt_payment_4 -1.656152 0.059947 -27.627 0.093360 -17.739
tau_rhsatt_payment_5 0.099554 0.043875 2.269 0.056389 1.765
tau_rhsatt_payment_6 3.908232 0.096296 40.586 0.209037 18.696
tau_rhsatt_trust_1 -275.200961 44.892120 -6.130 57.148898 -4.816
tau_rhsatt_trust_2 -192.959044 28.948014 -6.666 39.493019 -4.886
tau_rhsatt_trust_3 -154.022430 24.498392 -6.287 31.993587 -4.814
tau_rhsatt_trust_4 -46.032607 7.508708 -6.131 9.603395 -4.793
tau_rhsatt_trust_5 35.006261 8.588527 4.076 12.837098 2.727
tau_rhsatt_trust_6 205.889343 34.578409 5.954 43.992415 4.680
mu_pf 1.544576 0.071364 21.644 0.060938 25.347
mu_ps 1.641374 0.144190 11.383 0.217313 7.553
mu_fc 1.000000 NA NA NA NA
stephanehess
Site Admin
Posts: 1039
Joined: 24 Apr 2020, 16:29

Re: ICLV model convergence

Post by stephanehess »

Hi

1. It's not clear what's causing the problem without me trying to look at your model and data. If you can share it with me offline, then I'll have a look
2. Correlated error terms will not address the endogeneity issue. You'll probably need to consider a full maximum likelihood approach or a control function approach. Neither of these is easy

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