0% found this document useful (0 votes)
80 views

Nested Logit Models

The document describes building a nested logit model to analyze travel mode choice data. It includes: 1) Preprocessing the original "TravelMode" data set by creating new variables for time, income levels for different modes, and grouping modes into categories. 2) Transforming the data into the format required by the mlogit package and building a nested logit model with air and car grouped together and train and bus grouped together. 3) Using the model to calculate choice probabilities for a given scenario involving time and income values for each mode.

Uploaded by

Pramesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Nested Logit Models

The document describes building a nested logit model to analyze travel mode choice data. It includes: 1) Preprocessing the original "TravelMode" data set by creating new variables for time, income levels for different modes, and grouping modes into categories. 2) Transforming the data into the format required by the mlogit package and building a nested logit model with air and car grouped together and train and bus grouped together. 3) Using the model to calculate choice probabilities for a given scenario involving time and income values for each mode.

Uploaded by

Pramesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Nested Logit Models

Yufeng Zhang
February 10, 2019

Install the package “mlogit” if you haven’t, and install the package “AER” where there is a data set called
“TravelMode”.
#install.packages("mlogit", repos = "http://cran.us.r-project.org")
#install.packages("AER", repos = "http://cran.us.r-project.org")
library(mlogit)
library(AER)
data(TravelMode) # load the data set "TravelMode"
head(TravelMode)

Now we will need to operate on the data to create a new data set. After this you should be able to see new
columns in your data TravelMode.
# create a new column called "time"
TravelMode$time <- with(TravelMode, (travel + wait)/60)

# create a new column called "timeair" whose value equals


# the time if the mode is "air", and 0 otherwise
TravelMode$timeair <- with(TravelMode, time * I(mode == "air"))

# rescale data column "income"


TravelMode$income <- with(TravelMode, income / 10)

# create a new column "incomeother"


TravelMode$incomeother <- with(TravelMode, ifelse(mode %in% c('air', 'car'), income, 0))

# create a new column "group", "air" and "car" are


# grouped into "other" and "train" and "bus" are grouped into "public"
TravelMode$group <- with(TravelMode, ifelse( mode == "air" | mode == "car" , "other", "public"))

# transform the data set to mlogit.data format


TravelMode <- mlogit.data(TravelMode, choice = "choice", shape = "long",
alt.levels = c("air", "train", "bus", "car"),
group = "group")

Up to here, we finished the construction of the data set “TravelMode”.


Now let’s build a nested logit model.
nl <- mlogit(choice~ time + incomeother, TravelMode,
shape='long', alt.var='mode',
nests=list(public=c('train', 'bus'), other=c('car','air')), reflevel = "car")
summary(nl)

##
## Call:
## mlogit(formula = choice ~ time + incomeother, data = TravelMode,
## reflevel = "car", nests = list(public = c("train", "bus"),
## other = c("car", "air")), shape = "long", alt.var = "mode")
##

1
## Frequencies of alternatives:
## car air train bus
## 0.28095 0.27619 0.30000 0.14286
##
## bfgs method
## 3 iterations, 0h:0m:1s
## g'(-H)^-1g = 18.6
## last step couldn't find higher value
##
## Coefficients :
## Estimate Std. Error z-value Pr(>|z|)
## air:(intercept) -3.966671 1.094979 -3.6226 0.0002917 ***
## train:(intercept) 3.579651 0.741452 4.8279 1.380e-06 ***
## bus:(intercept) 2.873627 0.746598 3.8490 0.0001186 ***
## time -0.589287 0.126603 -4.6546 3.246e-06 ***
## incomeother 0.469757 0.099707 4.7114 2.461e-06 ***
## iv:public 1.020640 0.459500 2.2212 0.0263377 *
## iv:other 2.818374 0.871087 3.2355 0.0012144 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Log-Likelihood: -229.4
## McFadden R^2: 0.19158
## Likelihood ratio test : chisq = 108.73 (p.value = < 2.22e-16)
Now lets calculate a the probability of using each mode for the following case:

Mode time incomeother


Air 2.816 3.5
Train 6.767 0.0
Bus 7.533 0.0
Car 3.000 3.5

Let’s have a look at the coefficients.


nl$coefficients

## air:(intercept) train:(intercept) bus:(intercept) time


## -3.9666705 3.5796509 2.8736272 -0.5892872
## incomeother iv:public iv:other
## 0.4697572 1.0206400 2.8183744
## attr(,"fixed")
## air:(intercept) train:(intercept) bus:(intercept) time
## FALSE FALSE FALSE FALSE
## incomeother iv:public iv:other
## FALSE FALSE FALSE
The following calculates the utilities of four modes:
v_air <- nl$coefficients[1] + nl$coefficients[4] * 2.816 + nl$coefficients[5] * 3.5
v_train <- nl$coefficients[2] + nl$coefficients[4] * 6.767 + nl$coefficients[5] * 0.0
v_bus <- nl$coefficients[3] + nl$coefficients[4] * 7.533 + nl$coefficients[5] * 0.0
v_car <- 0 + nl$coefficients[4] * 3.000 + nl$coefficients[5] * 3.5

Calculate conditional probabilities:

2
c_air <- exp(v_air/nl$coefficients[7]) / ( exp(v_air/nl$coefficients[7]) +
exp(v_car/nl$coefficients[7]) )
c_car <- exp(v_car/nl$coefficients[7]) / ( exp(v_air/nl$coefficients[7]) +
exp(v_car/nl$coefficients[7]) )
c_bus <- exp(v_bus/nl$coefficients[6]) / ( exp(v_bus/nl$coefficients[6]) +
exp(v_train/nl$coefficients[6]) )
c_train <- exp(v_train/nl$coefficients[6]) / ( exp(v_bus/nl$coefficients[6]) +
exp(v_train/nl$coefficients[6]))

Calculate inclusive values:


iv_public <- nl$coefficients[6] * log( exp(v_bus/nl$coefficients[6]) +
exp(v_train/nl$coefficients[6]) )
iv_other <- nl$coefficients[7] * log( exp(v_air/nl$coefficients[7]) +
exp(v_car/nl$coefficients[7]) )

Calculate branch probabilities:


b_public <- exp(iv_public) /( exp(iv_public)+ exp(iv_other))
b_other <- exp(iv_other) /( exp(iv_public)+ exp(iv_other))

Calculate alternative probabilities:


p_air <- c_air * b_other
p_car <- c_car * b_other
p_train <- c_train * b_public
p_bus <- c_bus * b_public
c(p_air[[1]], p_car[[1]], p_train[[1]], p_bus[[1]])

## [1] 0.13270177 0.52168515 0.26148342 0.08412966

You might also like