Skip to contents

BLP share inversion for the nested logit

Usage

# S3 method for class 'choicer_nl'
blp(
  object,
  target_shares,
  delta_init = NULL,
  damping = 1,
  tol = 1e-12,
  max_iter = 1000,
  method = "anderson",
  control = list(),
  ...
)

Arguments

object

A choicer_nl object fitted with keep_data = TRUE.

target_shares

Numeric vector of target market shares. Length J_inside when no outside option, or J_inside + 1 (with the outside option's share at index 1) when include_outside_option = TRUE.

delta_init

Initial guess for delta (ASC) values. If NULL, uses the estimated ASCs from the fitted model (zeros for a model fitted without them, use_asc = FALSE).

damping

Multiplies each step, in (0, 1] (default 1); seldom needed (see nl_blp_contraction()).

tol

Convergence tolerance on the largest absolute log-share residual (default 1e-12; see blp_contraction()).

max_iter

The most evaluations of the shares (default 1000).

method

The solver: "anderson" (the default), Anderson acceleration of the two-level step; "simple", that step alone; "original", Berry, Levinsohn and Pakes's contraction; or "squarem", SQUAREM on the step (see blp()).

control

A list of the method's settings: for "anderson", memory and max_condition; for "squarem", scheme, step_min, step_max and step_factor (see blp_contraction()); none for the others.

...

Additional arguments (ignored).

Value

The constants, an n x 1 matrix (see blp()).

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
blp(fit, target_shares = rep(1/J, J))
#>         [,1]
#> 1  0.0000000
#> 2 -0.1203909
#> 3 94.5516335
#> 4 94.6161791
#> attr(,"convergence")
#> attr(,"convergence")$converged
#> [1] TRUE
#> 
#> attr(,"convergence")$status
#> [1] "converged"
#> 
#> attr(,"convergence")$iterations
#> [1] 13
#> 
#> attr(,"convergence")$evaluations
#> [1] 14
#> 
#> attr(,"convergence")$residual
#> [1] 4.884981e-15
#> 
#> attr(,"convergence")$tol
#> [1] 1e-12
#> 
#> attr(,"convergence")$method
#> [1] "anderson"
#> 
# }