R is a programming language for statistical computing and graphics. To install R, required installation files can be found in the following link:

Download RStudio

Apollo Choice Modeling framework has been employed in running the mode choice model component. We prepared the R notebooks for all models included in the guidebook.

Clear memory

rm(list = ls())

Install and load Apollo library

In first step of data processing in R, we need to call required package

library(apollo)
## Apollo 0.2.8
## http://www.ApolloChoiceModelling.com
## See url for a detailed manual, examples and a user forum.
## Sign up to the user forum to receive updates on new releases.
## 
## Your version of Apollo is more than six months old.
## Using the latest version will ensure you have all
##  current functionality and bug fixes.
## You can update to the latest version by typing:
##  install.packages("apollo")

Initialize code and set core controls

apollo_initialise()
apollo_control = list(
  modelName       = "MNL_Mode Choice Model_Base",
  modelDescr      = "Simple MNL model on base mode choice data",
  indivID         = "id",
  outputDirectory = "output",
  panelData = FALSE
)

Load data and check summary

database = read.csv("D:/Projects/20-102/Project Task/Task 4/R code/Mode choice data_base.csv",header=TRUE)
summary(database)
##        id           Choice          Drive          Passenger     
##  Min.   :   1   Min.   :1.000   Min.   :0.0000   Min.   :0.0000  
##  1st Qu.:1251   1st Qu.:2.000   1st Qu.:0.0000   1st Qu.:0.0000  
##  Median :2500   Median :2.000   Median :1.0000   Median :0.0000  
##  Mean   :2500   Mean   :2.825   Mean   :0.6468   Mean   :0.0548  
##  3rd Qu.:3750   3rd Qu.:4.000   3rd Qu.:1.0000   3rd Qu.:0.0000  
##  Max.   :5000   Max.   :7.000   Max.   :1.0000   Max.   :1.0000  
##     Transit            Bike             Walk            PRKR      
##  Min.   :0.0000   Min.   :0.0000   Min.   :0.000   Min.   :0.000  
##  1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:0.000   1st Qu.:0.000  
##  Median :0.0000   Median :0.0000   Median :0.000   Median :0.000  
##  Mean   :0.1968   Mean   :0.0132   Mean   :0.052   Mean   :0.034  
##  3rd Qu.:0.0000   3rd Qu.:0.0000   3rd Qu.:0.000   3rd Qu.:0.000  
##  Max.   :1.0000   Max.   :1.0000   Max.   :1.000   Max.   :1.000  
##      Other           av_Drive      av_Passenger   av_Transit   
##  Min.   :0.0000   Min.   :0.000   Min.   :1     Min.   :0.000  
##  1st Qu.:0.0000   1st Qu.:1.000   1st Qu.:1     1st Qu.:1.000  
##  Median :0.0000   Median :1.000   Median :1     Median :1.000  
##  Mean   :0.0024   Mean   :0.865   Mean   :1     Mean   :0.992  
##  3rd Qu.:0.0000   3rd Qu.:1.000   3rd Qu.:1     3rd Qu.:1.000  
##  Max.   :1.0000   Max.   :1.000   Max.   :1     Max.   :1.000  
##     av_Bike          av_Walk          av_Other    av_PRKR    time_Drive      
##  Min.   :0.0000   Min.   :0.0000   Min.   :1   Min.   :1   Min.   : 0.01667  
##  1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:1   1st Qu.:1   1st Qu.: 9.84583  
##  Median :1.0000   Median :0.0000   Median :1   Median :1   Median :16.60833  
##  Mean   :0.5688   Mean   :0.1916   Mean   :1   Mean   :1   Mean   :18.10670  
##  3rd Qu.:1.0000   3rd Qu.:0.0000   3rd Qu.:1   3rd Qu.:1   3rd Qu.:24.39167  
##  Max.   :1.0000   Max.   :1.0000   Max.   :1   Max.   :1   Max.   :78.28333  
##   time_Transit     time_PRKR       time_Bike        time_Walk        
##  Min.   :  0.0   Min.   : 10.0   Min.   :  0.10   Min.   :   0.3167  
##  1st Qu.: 14.0   1st Qu.: 24.0   1st Qu.: 22.54   1st Qu.:  60.7750  
##  Median : 29.0   Median : 39.0   Median : 50.33   Median : 134.2083  
##  Mean   : 34.2   Mean   : 44.2   Mean   : 65.56   Mean   : 171.9537  
##  3rd Qu.: 48.0   3rd Qu.: 58.0   3rd Qu.: 93.91   3rd Qu.: 245.9250  
##  Max.   :202.7   Max.   :212.7   Max.   :380.65   Max.   :1451.8333  
##    time_Other          cost_Drive      cost_Transit     cost_PRKR     
##  Min.   :  0.02167   Min.   : 1.335   Min.   :1.280   Min.   : 1.428  
##  1st Qu.: 12.79958   1st Qu.: 2.266   1st Qu.:3.250   1st Qu.: 3.410  
##  Median : 21.59083   Median : 3.465   Median :3.250   Median : 3.953  
##  Mean   : 23.53870   Mean   : 4.095   Mean   :4.468   Mean   : 4.878  
##  3rd Qu.: 31.70917   3rd Qu.: 5.289   3rd Qu.:6.750   3rd Qu.: 6.977  
##  Max.   :101.76833   Max.   :19.818   Max.   :9.750   Max.   :10.204  
##    cost_Other          OCBD              DCBD              Male       
##  Min.   : 1.934   Min.   : 0.2863   Min.   : 0.1264   Min.   :0.0000  
##  1st Qu.: 3.987   1st Qu.: 8.2237   1st Qu.: 3.4522   1st Qu.:0.0000  
##  Median : 5.864   Median :14.3008   Median : 9.6320   Median :1.0000  
##  Mean   : 5.994   Mean   :18.0263   Mean   :12.7275   Mean   :0.5278  
##  3rd Qu.: 7.441   3rd Qu.:25.3998   3rd Qu.:17.1038   3rd Qu.:1.0000  
##  Max.   :18.598   Max.   :76.6941   Max.   :68.9812   Max.   :1.0000  
##      Age20           Age2140          HH_1pers         HH_0car      
##  Min.   :0.0000   Min.   :0.0000   Min.   :0.0000   Min.   :0.0000  
##  1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:0.0000  
##  Median :0.0000   Median :0.0000   Median :0.0000   Median :0.0000  
##  Mean   :0.0342   Mean   :0.3782   Mean   :0.1028   Mean   :0.0836  
##  3rd Qu.:0.0000   3rd Qu.:1.0000   3rd Qu.:0.0000   3rd Qu.:0.0000  
##  Max.   :1.0000   Max.   :1.0000   Max.   :1.0000   Max.   :1.0000  
##     HH_2cars         MOR_PIK    
##  Min.   :0.0000   Min.   :0.00  
##  1st Qu.:0.0000   1st Qu.:0.00  
##  Median :0.0000   Median :1.00  
##  Mean   :0.4278   Mean   :0.68  
##  3rd Qu.:1.0000   3rd Qu.:1.00  
##  Max.   :1.0000   Max.   :1.00
table(database$Choice)
## 
##    1    2    3    4    5    6    7 
##   12 3234  274  984  260   66  170

Here, Choice = Mode alternatives, Drive = Drive mode, Passenger = Passenger mode, Transit = Transit mode, Walk = Walk mode, Bike = Bike mode, PRKR = Park and Ride mode, Other = Other mode, TNC = TNC mode, Micro = Shared-micromobility mode, av_Drive = Availability of drive mode, av_Passenger = Availability of Passenger mode, av_Transit = Availability of Transit mode, av_Walk = Availability of Walk mode, av_Bike = Availability of Bike mode, av_PRKR = Availability of Park and Ride mode, av_TNC = Availability of TNC mode, av_Micro = Availability of Shared-micromobility mode, av_Other = Availability of Other mode, time_Other = Travel time of Other mode, time_Drive = Travel time of Drive mode, time_Transit = Travel time of Transit mode, time_PRKR = Travel time of Park and Ride mode, time_Bike = Travel time of Bike mode, time_Walk = Travel time of Walk mode, time_TNC = Travel time of TNC mode, time_Micro = Travel time of Shared-micromobility mode, cost_Drive = Travel cost of Drive mode, cost_Transit = Travel cost of Transit mode, cost_PRKR = Travel cost of Park and Ride mode, cost_Other = Travel cost of Other mode, cost_TNC = Travel cost of TNC mode, cost_Micro = Travel cost of Shared-micromobility mode, OCBD = Origin distance from CBD, DCBD = Destination distance from CBD, Male = Male, Age 20 = Age <= 20, Age2140 = Age 21-40, HH_1pers = One person HH, HH_0car = Zero car HH, HH_2cars = HH with two cars, MOR_PIK = Morning peak

Initialize vector of parameters

Include fixed parameters in estimation process. 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_beta=c(asc_Other           = 0,
              asc_Drive           = 0,
              asc_Passenger       = 0,
              asc_Transit         = 0,
              asc_Walk            = 0,
              asc_Bike            = 0,
              asc_PRKR            = 0,
              b_tt                = 0,
              b_cost              = 0,
              b_OCBD_PRKR       =   0,
              b_DCBD_Drive    = 0,
              b_DCBD_Passenger= 0,
              b_male_Passenger    = 0,
              b_male                =   0,
              b_Age20               =   0,
              b_Age2140_Transit   = 0,
              b_HH1pers_Passenger   =   0,
              b_HH0car            = 0,
              b_HH2cars_Drive       =   0,
              b_Morpik_Transit    = 0,
              b_Morpik_PRKR       = 0)



apollo_fixed = c("asc_Other")


apollo_inputs = apollo_validateInputs()
## All checks on apollo_control completed.
## All checks on database completed.

Define the utilities of MNL model and compute the likelihood function

# 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()
  
  # List of utilities: these must use the same names as in mnl_settings, order is irrelevant
  V = list()
  V[["Other"]]      =   asc_Other   +   b_tt    *   time_Other    + b_cost  *   cost_Other                                                                                                                                              
  V[["Drive"]]      =   asc_Drive       +   b_tt    *   time_Drive    + b_cost  *   cost_Drive +    b_DCBD_Drive    *   DCBD +  b_HH2cars_Drive *   HH_2cars
  
  V[["Passenger"]]  =   asc_Passenger   +   b_tt    *   time_Drive +    b_DCBD_Passenger    *   DCBD    +   b_male_Passenger    *   Male    +   b_Age20 *   Age20   +   b_HH1pers_Passenger *   HH_1pers
  
  V[["Transit"]]      = asc_Transit  +  b_tt    *   time_Transit    +   b_cost  *   cost_Transit +  b_male  *   Male +  b_Age20 *   Age20   +   b_Age2140_Transit   *   Age2140 +   b_HH0car    *   HH_0car +   b_Morpik_Transit    *   MOR_PIK
  
  V[["Walk"]]         = asc_Walk    +   b_tt    *   time_Walk + b_male  *   Male +  b_HH0car    *   HH_0car
  
  V[["Bike"]]         = asc_Bike +  b_tt    *   time_Bike   +   b_Age20 *   Age20   +   b_HH0car    *   HH_0car
  
  V[["PRKR"]]         = asc_PRKR    +   b_tt    *   time_PRKR       +   b_cost  *   cost_PRKR      +    b_OCBD_PRKR *   OCBD +  b_male  *   Male    +   b_Morpik_PRKR   *   MOR_PIK
  
  # Define settings for MNL model component
  mnl_settings = list(alternatives  = c(Other=1, Drive=2, Passenger=3, Transit=4,Walk=5, Bike=6, PRKR=7), 
  avail = list(Other=av_Other, Drive=av_Drive, Passenger=av_Passenger, Transit=av_Transit, Walk=av_Walk, Bike=av_Bike, PRKR=av_PRKR), 
  choiceVar     = Choice,
  utilities     = V
  )
  
  # Compute probabilities using MNL model
  P[["model"]] = apollo_mnl(mnl_settings, functionality)
  
  # Prepare and return outputs of function
  P = apollo_prepareProb(P, apollo_inputs, functionality)
  return(P)
  nrow(p)
}

Model estimation

model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
## Preparing user-defined functions.
## 
## Testing likelihood function...
## 
## Overview of choices for MNL model component :
##                                    Other   Drive Passenger Transit   Walk
## Times available                  5000.00 4325.00   5000.00 4960.00 958.00
## Times chosen                       12.00 3234.00    274.00  984.00 260.00
## Percentage chosen overall           0.24   64.68      5.48   19.68   5.20
## Percentage chosen when available    0.24   74.77      5.48   19.84  27.14
##                                     Bike   PRKR
## Times available                  2844.00 5000.0
## Times chosen                       66.00  170.0
## Percentage chosen overall           1.32    3.4
## Percentage chosen when available    2.32    3.4
## 
## Pre-processing likelihood function...
## 
## Testing influence of parameters
## Starting main estimation
## Initial function value: -8584.2 
## Initial gradient value:
##           asc_Drive       asc_Passenger         asc_Transit            asc_Walk 
##          2460.74048          -632.22619            86.72381           117.32381 
##            asc_Bike            asc_PRKR                b_tt              b_cost 
##          -402.10952          -736.22619        -38033.93695          -150.22688 
##         b_OCBD_PRKR        b_DCBD_Drive    b_DCBD_Passenger    b_male_Passenger 
##        -12968.84476         41359.96703         -7990.23703          -394.76667 
##              b_male             b_Age20   b_Age2140_Transit b_HH1pers_Passenger 
##          -376.15000           -16.30476            91.75714           -84.34286 
##            b_HH0car     b_HH2cars_Drive    b_Morpik_Transit       b_Morpik_PRKR 
##           213.62619          1378.11667           103.90000          -472.65000 
## initial  value 8584.199774 
## iter   2 value 5790.374284
## iter   3 value 5129.513130
## iter   4 value 5021.011772
## iter   5 value 4737.526476
## iter   6 value 4184.796678
## iter   7 value 3987.561533
## iter   8 value 3616.890087
## iter   9 value 3500.816661
## iter  10 value 3454.496394
## iter  11 value 3428.048370
## iter  12 value 3427.739581
## iter  13 value 3425.719540
## iter  14 value 3419.304027
## iter  15 value 3398.563290
## iter  16 value 3379.135393
## iter  17 value 3373.656452
## iter  18 value 3363.711461
## iter  19 value 3343.540335
## iter  20 value 3332.866488
## iter  21 value 3327.187416
## iter  22 value 3310.865493
## iter  23 value 3307.039842
## iter  24 value 3300.175629
## iter  25 value 3296.198586
## iter  26 value 3294.474197
## iter  27 value 3293.799964
## iter  28 value 3293.390589
## iter  29 value 3293.143748
## iter  30 value 3293.036674
## iter  31 value 3293.006856
## iter  32 value 3293.000534
## iter  33 value 3292.999934
## iter  33 value 3292.999902
## iter  33 value 3292.999901
## final  value 3292.999901 
## converged
## Additional convergence test using scaled estimation. Parameters will be
##   scaled by their current estimates and additional iterations will be
##   performed.
## initial  value 3292.999901 
## iter   1 value 3292.999901
## final  value 3292.999901 
## converged
## Estimated parameters:
##                        Estimate
## asc_Other               0.00000
## asc_Drive               3.62365
## asc_Passenger           0.76950
## asc_Transit             3.66729
## asc_Walk                4.57696
## asc_Bike                1.18742
## asc_PRKR                1.04307
## b_tt                   -0.05247
## b_cost                 -0.34554
## b_OCBD_PRKR             0.08008
## b_DCBD_Drive            0.11435
## b_DCBD_Passenger        0.07263
## b_male_Passenger       -1.36442
## b_male                 -0.29545
## b_Age20                 0.57697
## b_Age2140_Transit       0.27045
## b_HH1pers_Passenger    -1.37528
## b_HH0car                1.10681
## b_HH2cars_Drive         0.96670
## b_Morpik_Transit        0.34726
## b_Morpik_PRKR           1.09273
## 
## Final LL: -3292.9999
## 
## Calculating log-likelihood at equal shares (LL(0)) for applicable
##   models...
## Calculating log-likelihood at observed shares from estimation data
##   (LL(c)) for applicable models...
## Calculating LL of each model component...
## Calculating other model fit measures
## Computing covariance matrix using analytical gradient.
##  0%....25%....50%....75%..100%
## Negative definite Hessian with maximum eigenvalue: -1.846626
## Computing score matrix...

Model outputs

apollo_modelOutput(model)
## Model run by Surface using Apollo 0.2.8 on R 4.1.3 for Windows.
## www.ApolloChoiceModelling.com
## 
## Model name                                  : MNL_Mode Choice Model_Base
## Model description                           : Simple MNL model on base mode choice data
## Model run at                                : 2023-11-20 12:42:52
## Estimation method                           : bfgs
## Model diagnosis                             : successful convergence 
## Number of individuals                       : 5000
## Number of rows in database                  : 5000
## Number of modelled outcomes                 : 5000
## 
## Number of cores used                        :  1 
## Model without mixing
## 
## LL(start)                                   : -8584.2
## LL at equal shares, LL(0)                   : -8584.2
## LL at observed shares, LL(C)                : -4243.58
## LL(final)                                   : -3293
## Rho-squared vs equal shares                  :  0.6164 
## Adj.Rho-squared vs equal shares              :  0.6141 
## Rho-squared vs observed shares               :  0.224 
## Adj.Rho-squared vs observed shares           :  0.2193 
## AIC                                         :  6626 
## BIC                                         :  6756.34 
## 
## Estimated parameters                        :  20
## Time taken (hh:mm:ss)                       :  00:00:7.14 
##      pre-estimation                         :  00:00:1.89 
##      estimation                             :  00:00:1.6 
##      post-estimation                        :  00:00:3.66 
## Iterations                                  :  37  
## Min abs eigenvalue of Hessian               :  1.846626 
## 
## Unconstrained optimisation.
## 
## These outputs have had the scaling used in estimation applied to them.
## Estimates:
##                        Estimate        s.e.   t.rat.(0)    Rob.s.e.
## asc_Other               0.00000          NA          NA          NA
## asc_Drive               3.62365    0.297517      12.180    0.290690
## asc_Passenger           0.76950    0.327189       2.352    0.337169
## asc_Transit             3.66729    0.309269      11.858    0.305553
## asc_Walk                4.57696    0.314072      14.573    0.325422
## asc_Bike                1.18742    0.325348       3.650    0.329857
## asc_PRKR                1.04307    0.386642       2.698    0.378206
## b_tt                   -0.05247    0.003225     -16.272    0.006129
## b_cost                 -0.34554    0.018197     -18.989    0.019922
## b_OCBD_PRKR             0.08008    0.006760      11.846    0.007112
## b_DCBD_Drive            0.11435    0.006523      17.531    0.008583
## b_DCBD_Passenger        0.07263    0.008123       8.942    0.009909
## b_male_Passenger       -1.36442    0.147991      -9.220    0.161200
## b_male                 -0.29545    0.087859      -3.363    0.082639
## b_Age20                 0.57697    0.226425       2.548    0.230349
## b_Age2140_Transit       0.27045    0.092055       2.938    0.083892
## b_HH1pers_Passenger    -1.37528    0.354065      -3.884    0.342276
## b_HH0car                1.10681    0.218646       5.062    0.196778
## b_HH2cars_Drive         0.96670    0.086522      11.173    0.084132
## b_Morpik_Transit        0.34726    0.103280       3.362    0.093022
## b_Morpik_PRKR           1.09273    0.229737       4.756    0.228538
##                     Rob.t.rat.(0)
## asc_Other                      NA
## asc_Drive                  12.466
## asc_Passenger               2.282
## asc_Transit                12.002
## asc_Walk                   14.065
## asc_Bike                    3.600
## asc_PRKR                    2.758
## b_tt                       -8.561
## b_cost                    -17.345
## b_OCBD_PRKR                11.260
## b_DCBD_Drive               13.324
## b_DCBD_Passenger            7.330
## b_male_Passenger           -8.464
## b_male                     -3.575
## b_Age20                     2.505
## b_Age2140_Transit           3.224
## b_HH1pers_Passenger        -4.018
## b_HH0car                    5.625
## b_HH2cars_Drive            11.490
## b_Morpik_Transit            3.733
## b_Morpik_PRKR               4.781

Here, asc_Other = Alternative specific constant for Other mode, asc_Drive = Alternative specific constant for Drive mode, asc_Passenger = Alternative specific constant Passenger mode, asc_Transit = Alternative specific constant for Transit mode, asc_Walk = Alternative specific constant for Wlak mode, asc_Bike = Alternative specific constant for Bike mode, asc_PRKR = Alternative specific constant for Park and Ride mode, asc_TNC = Alternative specific constant for TNC mode, asc_Micro = Alternative specific constant for Shared-micromobility mode, b_tt = Coefficient for travel time, b_cost = Coefficient for travel cost (Drive, Transit and Park & Ride mode), b_OCBD_Drive = Coefficient for destination distance from CBD (Park and Ride mode), b_DCBD_Drive = Coefficient for destination distance from CBD (Drive mode), b_DCBD_Passenger = Coefficient for destination distance from CBD (Passenger mode), b_male_Passenger = Coefficient for male (Passenger mode), b_male = Coefficient for male (Transit, Walk and Park & Ride mode), b_Age20 = Coefficient for Age <= 20 (Passenger, Transit and Bike mode), b_Age2140_Transit = Coefficient for Age 21-40 (Transit mode), b_HH1pers_Passenger = Coefficient for HH with 1 person (Passenger mode), b_HH0car = Coefficient for HH with zero car (transit, Walk and Bike mode), b_HH2cars_Drive = Coefficient for HH with two cars (Drive mode), b_Morpik_Transit = Coefficient for Morning peak (Transit mode) b_Morpik_PRKR = Coefficient for Morning peak (Park and Ride mode)