Nested Logit Models
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)
##
## 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:
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]))