Apollo examples

David Palma

2019-01-11

Code structure

We recommend creating a separate model .R file for each model the user wants to estimate. In this document, three model file examples are presented.

Simple MNL model file example

The following code estimates a simple MNL model. No random coefficients are considered.

### Load libraries
library(apollo)

### Set core controls
apollo_control = list(
  modelName ="MNL", # Make sure to use a new name for every model
  indivID   ="ID",  # Name of column in the database with each individual's ID
  mixing    = FALSE,# TRUE for models that include random parameters
  nCores    = 1     # Number of cores to use in estimation
)

### Load data
data(apollo_modeChoiceData)

### Model parameters
apollo_beta = c(asc_1=0, asc_2=0,
                asc_3=0, asc_4=0,
                tt   =0, tc   =0,
                acc  =0)

### Name of parameters fixed to starting values.
apollo_beta_fixed = c("asc_2")

### Likelihood function (do not change the arguments)
### b contains the parameters, x contains the explanatory variables
apollo_probabilities=function(b, x, functionality="estimate"){
  P <- list() ### Do not delete. Store probabilities here.

  ### Enumerate alternatives and availability, and select choice variable.
  alternatives = c(car=1, bus=2, air=3, rail=4)
  avail        = list(car=x$av_car, bus=x$av_bus, air=x$av_air, rail=x$av_rail)
  choiceVar    = x$choice

  ### List of utilities
  V = list()
  V[['car' ]] = b$asc_1 + b$tt*x$time_car  + b$tc*x$cost_car
  V[['bus' ]] = b$asc_2 + b$tt*x$time_bus  + b$tc*x$cost_bus  + b$acc*x$access_bus
  V[['air' ]] = b$asc_3 + b$tt*x$time_air  + b$tc*x$cost_air  + b$acc*x$access_air
  V[['rail']] = b$asc_4 + b$tt*x$time_rail + b$tc*x$cost_rail + b$acc*x$access_rail

  ### Compute choice probabilities using MNL model
  P[['model']] = apollo_mnl(alternatives, avail, choiceVar, V, functionality)

  return(P)
}

### Estimate model
model = apollo_estimate(apollo_beta, apollo_beta_fixed, database,
                        apollo_probabilities, apollo_control)
#> Model description missing, set to default of 'No model description given'
#> Missing setting for seed, set to default of 13
#> Missing setting for HB, set to default of FALSE
#> Missing setting for noValidation, set to default of FALSE
#> Missing setting for noDiagnostics, set to default of FALSE
#> Missing setting for panelData.
#> Several observations per individual detected based on the value of ID.
#>   Setting panelData set to TRUE.
#> All checks on apollo_control completed.
#> All checks on data completed.
#> 
#> Testing probability function (apollo_probabilities)
#> 
#> All checks passed for MNL model component
#> Overview of choices for MNL model component:
#>                                         car     bus     air    rail
#> Times available                     6224.00 7216.00 6016.00 6992.00
#> Times chosen                        2278.00  484.00 1737.00 3501.00
#> Percentage of choice overall          28.48    6.05   21.71   43.76
#> Percentage of choice when available   36.60    6.71   28.87   50.07
#> 
#> 
#> 
#> Starting main estimation
#> Initial function value: -9366.881 
#> Initial gradient value:
#>       asc_1       asc_3       asc_4          tt          tc         acc 
#>     438.000     -47.000    1362.333 -336419.583    8729.583   -9149.167 
#> initial  value 9366.880608 
#> iter   2 value 9009.644916
#> iter   3 value 7836.603614
#> iter   4 value 7832.564953
#> iter   5 value 7663.850418
#> iter   6 value 7582.030287
#> iter   7 value 7513.497183
#> iter   8 value 6929.142700
#> iter   9 value 6904.716287
#> iter  10 value 6899.094569
#> iter  11 value 6898.870366
#> iter  12 value 6898.861202
#> iter  13 value 6898.860795
#> iter  13 value 6898.860784
#> iter  13 value 6898.860784
#> final  value 6898.860784 
#> converged
#>          [,1]
#> asc_1  1.9004
#> asc_3  1.8973
#> asc_4  1.6306
#> tt    -0.0097
#> tc    -0.0509
#> acc   -0.0165
#> Computing covariance matrix using numDeriv package.
#>  (this may take a while)
#> 0%....25%....50%....75%...100%

### Show output in screen
apollo_modeloutput(model)
#> Model run using CMC choice modelling code for R, version alpha 
#> www.cmc.leeds.ac.uk
#> 
#> Model name                       : MNL
#> Model description                : No model description given
#> Model run at                     : 2019-01-11 18:20:16
#> Estimation method                : bfgs
#> Model diagnosis                  : successful convergence 
#> Number of decision makers        : 500
#> Number of observations           : 8000
#> Estimated parameters             : 5
#> Model with no mixing
#> 
#> LL(start)                        : -9366.881
#> LL(0)                            : -9366.881
#> LL(final)                        : -6898.861
#> Rho-square (0)                   :  0.2635 
#> Adj.Rho-square (0)               :  0.2629 
#> AIC                              :  13807.72 
#> BIC                              :  13842.66 
#> 
#> Time taken (hh:mm:ss)            :  00:00:5.99 
#> Iterations                       :  15 
#> Number of cores used             :  1 
#> 
#> Estimates:
#>       Estimate Std.err. t.ratio(0) Rob.std.err. Rob.t.ratio(0)
#> asc_1   1.9004   0.0668      28.43       0.0813          23.38
#> asc_2   0.0000       NA         NA           NA             NA
#> asc_3   1.8973   0.1676      11.32       0.1829          10.37
#> asc_4   1.6306   0.1154      14.13       0.1277          12.77
#> tt     -0.0097   0.0005     -19.64       0.0005         -19.08
#> tc     -0.0509   0.0013     -39.64       0.0015         -34.11
#> acc    -0.0165   0.0023      -7.26       0.0022          -7.39
#> 
#> The following parameters were fixed (they have no std.err.):
#> asc_2
#> 
#> Overview of choices for MNL model component:
#>                                         car     bus     air    rail
#> Times available                     6224.00 7216.00 6016.00 6992.00
#> Times chosen                        2278.00  484.00 1737.00 3501.00
#> Percentage of choice overall          28.48    6.05   21.71   43.76
#> Percentage of choice when available   36.60    6.71   28.87   50.07

Mixed MNL model with inter and intra-individual randomness.

The following code estimates a mixed MNL model with random coefficients. Variability is both across and within individuals. Only 10 draws of each kind are used in the example, but significantly more are recommended for estimation.

If you want to use only one kind of draws, then set the corresponding nDraws variable to zero, and adjust the apollo_randcoeff function appropriately.

### Load libraries
library(apollo)

### Set core controls
apollo_control = list(
  modelName ="MMNL",# Make sure to use a new name for every model
  indivID   ="ID",  # Name of column in the database with each individual's ID
  mixing    = TRUE, # TRUE for models that include random parameters
  nCores    = 1     # Number of cores to use in estimation
)

### Load data
data(apollo_modeChoiceData)

### Model parameters
apollo_beta = c(asc_1= 1.9004, asc_2= 0.0000,
                asc_3= 1.8973, asc_4= 1.6306,
                mu_tt=-0.0097, sB_tt= 0.0000,
                sW_tt= 0.0000, mu_tc=-0.0509,
                sB_tc= 0.0000, acc  =-0.0165)

### Name of parameters fixed to starting values.
apollo_beta_fixed = c("asc_2")

### Set draws parameters
apollo_draws = list(
  inter_drawsType="halton", # type of inter-person draws (options are MLHS, halton or pmc)
  inter_nDraws=10,          # number of inter-person draws per person. Set to 0 if only using intra-person draws.
  inter_unifDraws=c(),      # names of uniform distributed inter-person draws
  inter_normDraws=c("tt_inter", "tc_inter", "tt_tc_inter"), # names of normaly distributed inter-person draws

  intra_drawsType='halton', # type of intra-person draws (options are MLHS, halton or pmc)
  intra_nDraws=10,          # number of intra-person draws per person. Set to 0 if only using inter-person draws.
  intra_unifDraws=c(),      # names of uniform distributed intra-person draws
  intra_normDraws=c("tt_intra") # names of normaly distributed intra-person draws
)

### Random coeffs must be created inside this function
apollo_randcoeff = function(theta,draws){
  randcoeff = list()

  randcoeff[["tt"]] = -exp(theta[["mu_tt"]] + theta[["sB_tt"]]*draws[["tt_inter"]] + theta[["sW_tt"]]*draws[["tt_intra"]])
  randcoeff[["tc"]] = -exp(theta[["mu_tc"]] + theta[["sB_tc"]]*draws[["tc_inter"]])

  return(randcoeff)
}

### Likelihood function (do not change the arguments)
### b contains the parameters, x contains the explanatory variables
apollo_probabilities=function(b, x, functionality="estimate"){
  P <- list() ### Do not delete. Store probabilities here.

  ### Enumerate alternatives and availability, and select choice variable.
  alternatives = c(car=1, bus=2, air=3, rail=4)
  avail        = list(car=x$av_car, bus=x$av_bus, air=x$av_air, rail=x$av_rail)
  choiceVar    = x$choice

  ### List of utilities
  V = list()
  V[['car' ]] = b$asc_1 + b$tt*x$time_car  + b$tc*x$cost_car
  V[['bus' ]] = b$asc_2 + b$tt*x$time_bus  + b$tc*x$cost_bus  + b$acc*x$access_bus
  V[['air' ]] = b$asc_3 + b$tt*x$time_air  + b$tc*x$cost_air  + b$acc*x$access_air
  V[['rail']] = b$asc_4 + b$tt*x$time_rail + b$tc*x$cost_rail + b$acc*x$access_rail

  ### Compute choice probabilities using MNL model
  P[['model']] = apollo_mnl(alternatives, avail, choiceVar, V, functionality)

  return(P)
}

### Estimate model
model = apollo_estimate(apollo_beta, apollo_beta_fixed, database,
                        apollo_probabilities, apollo_control,
                        apollo_draws, apollo_randcoeff, work_in_logs=TRUE)

### Show output in screen
apollo_modeloutput(model)