Skip to contents

Diversion ratios for nested logit model

Usage

# S3 method for class 'choicer_nl'
diversion_ratios(object, ...)

Arguments

object

A choicer_nl object fitted with keep_data = TRUE.

...

Additional arguments (ignored).

Value

A J x J diversion ratio matrix with alternative labels.

Examples

# \donttest{
library(data.table)
set.seed(42)
N <- 50; J <- 4
dt <- data.table(id = rep(1:N, each = J), alt = rep(1:J, N))
dt[, nest := rep(c(1L, 1L, 2L, 2L), N)]
#>         id   alt  nest
#>      <int> <int> <int>
#>   1:     1     1     1
#>   2:     1     2     1
#>   3:     1     3     2
#>   4:     1     4     2
#>   5:     2     1     1
#>  ---                  
#> 196:    49     4     2
#> 197:    50     1     1
#> 198:    50     2     1
#> 199:    50     3     2
#> 200:    50     4     2
dt[, `:=`(x1 = rnorm(.N), x2 = rnorm(.N))]
#>         id   alt  nest         x1         x2
#>      <int> <int> <int>      <num>      <num>
#>   1:     1     1     1  1.3709584 -2.0009292
#>   2:     1     2     1 -0.5646982  0.3337772
#>   3:     1     3     2  0.3631284  1.1713251
#>   4:     1     4     2  0.6328626  2.0595392
#>   5:     2     1     1  0.4042683 -1.3768616
#>  ---                                        
#> 196:    49     4     2  1.0857749  1.0965134
#> 197:    50     1     1  0.4037749  0.4420131
#> 198:    50     2     1  0.5864875  0.2410163
#> 199:    50     3     2  1.8152284 -0.2556077
#> 200:    50     4     2  0.1288214  0.9310329
dt[, choice := 0L]
#>         id   alt  nest         x1         x2 choice
#>      <int> <int> <int>      <num>      <num>  <int>
#>   1:     1     1     1  1.3709584 -2.0009292      0
#>   2:     1     2     1 -0.5646982  0.3337772      0
#>   3:     1     3     2  0.3631284  1.1713251      0
#>   4:     1     4     2  0.6328626  2.0595392      0
#>   5:     2     1     1  0.4042683 -1.3768616      0
#>  ---                                               
#> 196:    49     4     2  1.0857749  1.0965134      0
#> 197:    50     1     1  0.4037749  0.4420131      0
#> 198:    50     2     1  0.5864875  0.2410163      0
#> 199:    50     3     2  1.8152284 -0.2556077      0
#> 200:    50     4     2  0.1288214  0.9310329      0
dt[, choice := sample(c(1L, rep(0L, J - 1))), by = id]
#>         id   alt  nest         x1         x2 choice
#>      <int> <int> <int>      <num>      <num>  <int>
#>   1:     1     1     1  1.3709584 -2.0009292      0
#>   2:     1     2     1 -0.5646982  0.3337772      0
#>   3:     1     3     2  0.3631284  1.1713251      0
#>   4:     1     4     2  0.6328626  2.0595392      1
#>   5:     2     1     1  0.4042683 -1.3768616      0
#>  ---                                               
#> 196:    49     4     2  1.0857749  1.0965134      1
#> 197:    50     1     1  0.4037749  0.4420131      0
#> 198:    50     2     1  0.5864875  0.2410163      0
#> 199:    50     3     2  1.8152284 -0.2556077      0
#> 200:    50     4     2  0.1288214  0.9310329      1
fit <- run_nestlogit(dt, "id", "alt", "choice", c("x1", "x2"), "nest")
#> Optimization run time 0h:0m:0.01s
diversion_ratios(fit)
#>            1          2          3         4
#> 1  0.0000000 -0.8981740 0.45017345 0.4192658
#> 2 -1.0621318  0.0000000 0.49007051 0.4562837
#> 3  1.4249183  1.3117490 0.00000000 0.1244504
#> 4  0.6372135  0.5864251 0.05975604 0.0000000
# }