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.

Nested Logit Model

Ask questions about model specifications. Ideally include a mathematical explanation of your proposed model.
Post Reply
punyabeet
Posts: 19
Joined: 08 May 2020, 11:43

Nested Logit Model

Post by punyabeet »

Hello Prof. Hess,

Based on Apollo example 4, I just wanted to check if I want to specify a two-level nested logit model with slow_commute and fast_commute at the first level and 'car & bus' (slow) and 'air & rail' (fast) at the second level, whether the following specification is correct or not??

### Specify nests for NL model
nlNests = list(slow_commute=1, fast_commute=lambda_fast)

### Specify tree structure for NL model
nlStructure= list()
nlStructure[["root"]] = c("slow_commute", "fast_commute")
nlStructure[["slow_commute"]] = c("car","bus")
nlStructure[["fast_commute"]] = c("air","rail")

Because I get this message : Error in checkForRemoteErrors(lapply(cl, recvResult)) : 3 nodes produced errors; first error: incorrect length for 'group'
when I set lamda_fast = 0.5

Is there any other change do I need to make in the code ??

Any help is appreciated.

Regards
Punya
stephanehess
Site Admin
Posts: 977
Joined: 24 Apr 2020, 16:29

Re: Nested Logit Model

Post by stephanehess »

Hi

please show us the entire code

Thanks

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
punyabeet
Posts: 19
Joined: 08 May 2020, 11:43

Re: Nested Logit Model

Post by punyabeet »

It's the same as the example provided in Apollo Manual for two-level nested logit. Anyway here is the code.

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

### Load Apollo library
library(apollo)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
modelName ="Apollo_example_4",
modelDescr ="Two-level NL model with socio-demographics on mode choice SP data",
indivID ="ID",
nCores = 2
)

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

setwd("D:/Apollo Syntax/Apollo/4.NL_2Level_SP/New_NL")
database = read.csv("apollo_modeChoiceData.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_car = 0,
asc_bus = 0,
asc_air = 0,
asc_rail = 0,
asc_bus_shift_female = 0,
asc_air_shift_female = 0,
asc_rail_shift_female = 0,
b_tt_car = 0,
b_tt_bus = 0,
b_tt_air = 0,
b_tt_rail = 0,
b_tt_shift_business = 0,
b_access = 0,
b_cost = 0,
b_cost_shift_business = 0,
cost_income_elast = 0,
b_no_frills = 0,
b_wifi = 0,
b_food = 0,
lambda_fast = 0.5)

### 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","b_no_frills")

### Read in starting values for at least some parameters from existing model output file
## apollo_beta = apollo_readBeta(apollo_beta,apollo_fixed,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_bus_value = asc_bus + asc_bus_shift_female * female
asc_air_value = asc_air + asc_air_shift_female * female
asc_rail_value = asc_rail + asc_rail_shift_female * female
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[['car']] = asc_car + b_tt_car_value * time_car + b_cost_value * cost_car
V[['bus']] = asc_bus_value + b_tt_bus_value * time_bus + b_access * access_bus + b_cost_value * cost_bus
V[['air']] = asc_air_value + 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[['rail']] = asc_rail_value + 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(slow_commute=1, fast_commute=lambda_fast)

### Specify tree structure for NL model
nlStructure= list()
nlStructure[["root"]] = c("slow_commute", "fast_commute")
nlStructure[["slow_commute"]] = c("car","bus")
nlStructure[["fast_commute"]] = c("air","rail")

### Define settings for NL model
nl_settings <- list(
alternatives = c(car=1, bus=2, air=3, rail=4),
avail = list(car=av_car, bus=av_bus, air=av_air, rail=av_rail),
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 the function
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}

# ################################################################# #
#### MODEL ESTIMATION ####
# ################################################################# #

model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
stephanehess
Site Admin
Posts: 977
Joined: 24 Apr 2020, 16:29

Re: Nested Logit Model

Post by stephanehess »

Hi

you're missing the root in nlNests, but Apollo should not fail in that way. We will address this, but in the meantime, your model should work fine if you add the root in.

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
punyabeet
Posts: 19
Joined: 08 May 2020, 11:43

Re: Nested Logit Model

Post by punyabeet »

Thanks for the clarification.
punyabeet
Posts: 19
Joined: 08 May 2020, 11:43

Re: Nested Logit Model

Post by punyabeet »

Thanks, Prof Hess. Based on the nested logit model, I have more of a conceptual doubt.

My doubt is if a household uses a two-wheeler in the morning for maintenance purpose for home-based tour, and again they use app-based taxi like UBER, LYFT in the evening for same activity purpose. How to code them in the mode choice column as only one entry is allowed per unit of analysis in the wide format using Apollo ? I am for example interested in using a nested structure to model mode choice by activity purpose on a weekday with household as my unit of analysis.

I am sorry if this topic is beyond the scope of this forum.

Thanks
Punyabeet
stephanehess
Site Admin
Posts: 977
Joined: 24 Apr 2020, 16:29

Re: Nested Logit Model

Post by stephanehess »

Hi

what you're talking about here is modelling separate choices, so they should be separate observations. The choices might be related, so you could capture correlations or dependencies between them. But this is not really related to Nested Logit - NL is not about sequential choices

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