Skip to contents

Computes choice probabilities or aggregate market shares, either for the data used at fit time (default) or for counterfactual newdata.

Usage

# S3 method for class 'choicer_nl'
predict(
  object,
  type = c("probabilities", "shares"),
  newdata = NULL,
  weights = NULL,
  ...
)

Arguments

object

A choicer_nl object.

type

One of "probabilities" (individual-level choice probabilities) or "shares" (aggregate market shares).

newdata

Optional data for counterfactual prediction. Either:

  • a data.frame in the same long format used at fit time (one row per id-alternative pair, with the fit-time id, alternative, and covariate columns; a choice column is not required). Alternative labels must have been seen at fit time; per-id subsets of alternatives are allowed. The alternative-to-nest mapping always comes from the fitted object (it indexes the estimated lambda parameters), so a nest column in newdata is not required and is ignored if present.

  • a list with elements X, alt_idx, M (and optionally weights) matching the layout of object$data — the "modified design matrix" path for policy simulation. alt_idx must use the fit-time integer codes from object$alt_mapping.

When NULL (default), the data stored at fit time is used (requires keep_data = TRUE).

weights

Optional numeric vector with one weight per choice situation, used for type = "shares" aggregation. For a data.frame newdata, supply one weight per id in order of first appearance in newdata (weights are realigned internally to the sorted row order). Defaults to equal weights. Ignored when newdata is NULL (the stored fit weights apply).

...

Additional arguments (ignored).

Value

For "probabilities": a list with choice_prob and utility vectors. For "shares": a named numeric vector of market shares per alternative. With a data.frame newdata, rows are ordered by id, then by fit-time alternative code (alt_int in object$alt_mapping).

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
predict(fit, type = "shares")
#>           [,1]
#> [1,] 0.2010835
#> [2,] 0.2189165
#> [3,] 0.4034451
#> [4,] 0.1765549
predict(fit, type = "probabilities")
#> $choice_prob
#>              [,1]
#>   [1,] 0.07370449
#>   [2,] 0.08059542
#>   [3,] 0.52156748
#>   [4,] 0.32413261
#>   [5,] 0.14587238
#>   [6,] 0.15864543
#>   [7,] 0.53363455
#>   [8,] 0.16184764
#>   [9,] 0.13749773
#>  [10,] 0.14913867
#>  [11,] 0.28853046
#>  [12,] 0.42483314
#>  [13,] 0.15431599
#>  [14,] 0.16816856
#>  [15,] 0.46900244
#>  [16,] 0.20851301
#>  [17,] 0.30161330
#>  [18,] 0.32848856
#>  [19,] 0.22819700
#>  [20,] 0.14170114
#>  [21,] 0.21482089
#>  [22,] 0.23218038
#>  [23,] 0.37315547
#>  [24,] 0.17984326
#>  [25,] 0.26180407
#>  [26,] 0.28256030
#>  [27,] 0.35817446
#>  [28,] 0.09746117
#>  [29,] 0.20252438
#>  [30,] 0.22149714
#>  [31,] 0.34139212
#>  [32,] 0.23458636
#>  [33,] 0.18999156
#>  [34,] 0.20652118
#>  [35,] 0.46699121
#>  [36,] 0.13649605
#>  [37,] 0.31329404
#>  [38,] 0.33882629
#>  [39,] 0.23582868
#>  [40,] 0.11205099
#>  [41,] 0.12388555
#>  [42,] 0.13555686
#>  [43,] 0.55077947
#>  [44,] 0.18977811
#>  [45,] 0.18159621
#>  [46,] 0.20121468
#>  [47,] 0.41695150
#>  [48,] 0.20023761
#>  [49,] 0.26991497
#>  [50,] 0.29232573
#>  [51,] 0.28627447
#>  [52,] 0.15148482
#>  [53,] 0.16707758
#>  [54,] 0.18240664
#>  [55,] 0.53192782
#>  [56,] 0.11858796
#>  [57,] 0.33679721
#>  [58,] 0.36707092
#>  [59,] 0.15008982
#>  [60,] 0.14604204
#>  [61,] 0.26686881
#>  [62,] 0.29079705
#>  [63,] 0.35198332
#>  [64,] 0.09035083
#>  [65,] 0.20882915
#>  [66,] 0.22930386
#>  [67,] 0.41005114
#>  [68,] 0.15181585
#>  [69,] 0.15804186
#>  [70,] 0.17382171
#>  [71,] 0.46103410
#>  [72,] 0.20710233
#>  [73,] 0.21616975
#>  [74,] 0.23456433
#>  [75,] 0.28409298
#>  [76,] 0.26517294
#>  [77,] 0.15378397
#>  [78,] 0.16598975
#>  [79,] 0.48290352
#>  [80,] 0.19732276
#>  [81,] 0.23205753
#>  [82,] 0.24912407
#>  [83,] 0.31809271
#>  [84,] 0.20072570
#>  [85,] 0.10947735
#>  [86,] 0.11974373
#>  [87,] 0.66373585
#>  [88,] 0.10704307
#>  [89,] 0.17944339
#>  [90,] 0.19709462
#>  [91,] 0.42668081
#>  [92,] 0.19678118
#>  [93,] 0.36895152
#>  [94,] 0.40712052
#>  [95,] 0.15776250
#>  [96,] 0.06616545
#>  [97,] 0.20789496
#>  [98,] 0.22694903
#>  [99,] 0.43851048
#> [100,] 0.12664553
#> [101,] 0.26836886
#> [102,] 0.29276128
#> [103,] 0.22851189
#> [104,] 0.21035797
#> [105,] 0.16390425
#> [106,] 0.17868536
#> [107,] 0.42279215
#> [108,] 0.23461824
#> [109,] 0.19716268
#> [110,] 0.21552658
#> [111,] 0.42870838
#> [112,] 0.15860236
#> [113,] 0.16735412
#> [114,] 0.18235003
#> [115,] 0.46128509
#> [116,] 0.18901077
#> [117,] 0.29695593
#> [118,] 0.32673702
#> [119,] 0.17146960
#> [120,] 0.20483746
#> [121,] 0.15218846
#> [122,] 0.16602741
#> [123,] 0.50039257
#> [124,] 0.18139156
#> [125,] 0.21889608
#> [126,] 0.23530204
#> [127,] 0.43775704
#> [128,] 0.10804483
#> [129,] 0.12378072
#> [130,] 0.13552170
#> [131,] 0.58892562
#> [132,] 0.15177197
#> [133,] 0.13323973
#> [134,] 0.14587854
#> [135,] 0.59780816
#> [136,] 0.12307357
#> [137,] 0.20623443
#> [138,] 0.22511037
#> [139,] 0.37397666
#> [140,] 0.19467854
#> [141,] 0.28618948
#> [142,] 0.31164054
#> [143,] 0.28937362
#> [144,] 0.11279636
#> [145,] 0.24927314
#> [146,] 0.27175517
#> [147,] 0.40402954
#> [148,] 0.07494216
#> [149,] 0.25699926
#> [150,] 0.27990304
#> [151,] 0.38239188
#> [152,] 0.08070583
#> [153,] 0.25270678
#> [154,] 0.27216876
#> [155,] 0.28695757
#> [156,] 0.18816690
#> [157,] 0.27484948
#> [158,] 0.29982842
#> [159,] 0.30111285
#> [160,] 0.12420925
#> [161,] 0.15213741
#> [162,] 0.16499217
#> [163,] 0.47806588
#> [164,] 0.20480454
#> [165,] 0.18887658
#> [166,] 0.20579087
#> [167,] 0.50166176
#> [168,] 0.10367079
#> [169,] 0.14652635
#> [170,] 0.16020247
#> [171,] 0.51987035
#> [172,] 0.17340083
#> [173,] 0.11145598
#> [174,] 0.12076215
#> [175,] 0.64416856
#> [176,] 0.12361331
#> [177,] 0.09711637
#> [178,] 0.10579393
#> [179,] 0.47132995
#> [180,] 0.32575975
#> [181,] 0.18104065
#> [182,] 0.19377430
#> [183,] 0.39376787
#> [184,] 0.23141719
#> [185,] 0.14264607
#> [186,] 0.15478054
#> [187,] 0.44458692
#> [188,] 0.25798647
#> [189,] 0.23174564
#> [190,] 0.25091771
#> [191,] 0.38751736
#> [192,] 0.12981929
#> [193,] 0.17711774
#> [194,] 0.19115212
#> [195,] 0.31761025
#> [196,] 0.31411989
#> [197,] 0.20117847
#> [198,] 0.21875858
#> [199,] 0.39084117
#> [200,] 0.18922177
#> 
#> $utility
#>                [,1]
#>   [1,]  -0.81230869
#>   [2,]  13.37474614
#>   [3,] 117.15128694
#>   [4,] 116.15383925
#>   [5,]  -0.75738349
#>   [6,]  12.56642102
#>   [7,] 116.33852749
#>   [8,] 113.83685425
#>   [9,]   0.31791460
#>  [10,]  13.21782715
#>  [11,] 115.93641854
#>  [12,] 116.74768564
#>  [13,]  -0.44025859
#>  [14,]  13.20494840
#>  [15,] 116.52384635
#>  [16,] 114.82411645
#>  [17,]  -0.19155707
#>  [18,]  13.35711561
#>  [19,] 115.20563782
#>  [20,] 114.20650608
#>  [21,]   0.14096420
#>  [22,]  12.47593187
#>  [23,] 115.84002918
#>  [24,] 114.30950991
#>  [25,]   0.97948228
#>  [26,]  13.08994595
#>  [27,] 116.49142009
#>  [28,] 113.76221563
#>  [29,]  -0.26404795
#>  [30,]  13.95019911
#>  [31,] 116.23549954
#>  [32,] 115.44874036
#>  [33,]  -0.40983555
#>  [34,]  12.83201818
#>  [35,] 116.25530824
#>  [36,] 113.67613718
#>  [37,]   0.59891884
#>  [38,]  13.03474482
#>  [39,] 115.51887823
#>  [40,] 113.95849513
#>  [41,]  -0.40411297
#>  [42,]  13.88690219
#>  [43,] 117.35404727
#>  [44,] 115.11988516
#>  [45,]  -1.41228096
#>  [46,]  14.87140131
#>  [47,] 116.61733447
#>  [48,] 115.07935932
#>  [49,]   0.30427882
#>  [50,]  12.96492072
#>  [51,] 115.64378049
#>  [52,] 114.30920000
#>  [53,]  -0.20881957
#>  [54,]  13.72460567
#>  [55,] 117.13384735
#>  [56,] 113.98676624
#>  [57,]  -0.23598202
#>  [58,]  13.42662668
#>  [59,] 114.47534500
#>  [60,] 114.41801815
#>  [61,]   0.43923720
#>  [62,]  14.06919843
#>  [63,] 116.71350263
#>  [64,] 113.86201456
#>  [65,]  -0.30322836
#>  [66,]  14.54311580
#>  [67,] 116.90554300
#>  [68,] 114.82207247
#>  [69,]  -1.43806903
#>  [70,]  13.66837122
#>  [71,] 116.24040461
#>  [72,] 114.56237202
#>  [73,]   0.33309589
#>  [74,]  13.29599256
#>  [75,] 115.78594059
#>  [76,] 115.64142620
#>  [77,]   0.59232843
#>  [78,]  12.71575035
#>  [79,] 116.82998542
#>  [80,] 114.95334411
#>  [81,]   1.09913750
#>  [82,]  12.36358991
#>  [83,] 115.90458875
#>  [84,] 114.93918602
#>  [85,]  -0.41081458
#>  [86,]  13.81721476
#>  [87,] 117.78558356
#>  [88,] 113.95953823
#>  [89,]  -1.04726921
#>  [90,]  13.84551426
#>  [91,] 116.30657299
#>  [92,] 114.68371965
#>  [93,]  -0.29530007
#>  [94,]  15.33079738
#>  [95,] 115.75646650
#>  [96,] 113.93443560
#>  [97,]  -0.52597780
#>  [98,]  13.39352674
#>  [99,] 116.34022885
#> [100,] 113.73594390
#> [101,]   0.43899168
#> [102,]  14.24780500
#> [103,] 115.90314180
#> [104,] 115.72956872
#> [105,]  -0.34181652
#> [106,]  13.36365375
#> [107,] 116.40877918
#> [108,] 115.17389313
#> [109,]  -0.75104699
#> [110,]  13.38473549
#> [111,] 116.19078616
#> [112,] 114.10571558
#> [113,]  -0.85943430
#> [114,]  12.76221615
#> [115,] 116.02154081
#> [116,] 114.15069492
#> [117,]  -0.70526168
#> [118,]  14.46493908
#> [119,] 114.93143632
#> [120,] 115.30428098
#> [121,]  -0.86868658
#> [122,]  12.94617599
#> [123,] 116.32619385
#> [124,] 114.19843488
#> [125,]   1.19017248
#> [126,]  12.66210049
#> [127,] 116.77474520
#> [128,] 113.84098849
#> [129,]  -1.00047638
#> [130,]  13.38373326
#> [131,] 116.94713377
#> [132,] 114.10395534
#> [133,]  -0.50812215
#> [134,]  13.87675384
#> [135,] 117.42732967
#> [136,] 114.11326557
#> [137,]  -0.95605158
#> [138,]  12.94515768
#> [139,] 115.56811596
#> [140,] 114.19919290
#> [141,]  -0.04579525
#> [142,]  13.47748090
#> [143,] 115.79701025
#> [144,] 113.82148416
#> [145,]   0.24723585
#> [146,]  13.95400611
#> [147,] 116.83139614
#> [148,] 113.29866325
#> [149,]   0.31395943
#> [150,]  13.86481857
#> [151,] 116.70836028
#> [152,] 113.44640817
#> [153,]   0.91170682
#> [154,]  12.68833909
#> [155,] 115.77612504
#> [156,] 114.89123873
#> [157,]  -0.19381583
#> [158,]  13.61366779
#> [159,] 115.85869463
#> [160,] 114.00188688
#> [161,]  -0.62325222
#> [162,]  12.25202274
#> [163,] 115.98806077
#> [164,] 114.21056673
#> [165,]   0.11623073
#> [166,]  13.73004718
#> [167,] 117.12670182
#> [168,] 113.82056784
#> [169,]  -0.24176831
#> [170,]  13.92229006
#> [171,] 117.23391030
#> [172,] 114.93161132
#> [173,]   0.17325276
#> [174,]  12.90238347
#> [175,] 117.51693807
#> [176,] 114.05543393
#> [177,]  -0.71526111
#> [178,]  12.86942203
#> [179,] 116.51267808
#> [180,] 115.73810102
#> [181,]   1.64989040
#> [182,]  12.43921245
#> [183,] 116.70229199
#> [184,] 115.58772714
#> [185,]  -0.23155686
#> [186,]  12.72750338
#> [187,] 116.30405701
#> [188,] 115.16286413
#> [189,]   0.61190219
#> [190,]  13.22856130
#> [191,] 116.53289502
#> [192,] 114.23973074
#> [193,]   0.42096521
#> [194,]  12.52496258
#> [195,] 115.70989642
#> [196,] 115.68672556
#> [197,]   0.43952580
#> [198,]  13.73740633
#> [199,] 116.74661889
#> [200,] 115.22559401
#> 
# }