Computes choice probabilities or aggregate market shares, either for the
data used at fit time (default) or for counterfactual newdata.
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
lambdaparameters), so a nest column innewdatais not required and is ignored if present.a list with elements
X,alt_idx,M(and optionallyweights) matching the layout ofobject$data— the "modified design matrix" path for policy simulation.alt_idxmust use the fit-time integer codes fromobject$alt_mapping.
When
NULL(default), the data stored at fit time is used (requireskeep_data = TRUE).- weights
Optional numeric vector with one weight per choice situation, used for
type = "shares"aggregation. For a data.framenewdata, supply one weight per id in order of first appearance innewdata(weights are realigned internally to the sorted row order). Defaults to equal weights. Ignored whennewdataisNULL(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.52156746
#> [4,] 0.32413263
#> [5,] 0.14587237
#> [6,] 0.15864544
#> [7,] 0.53363454
#> [8,] 0.16184765
#> [9,] 0.13749772
#> [10,] 0.14913866
#> [11,] 0.28853045
#> [12,] 0.42483317
#> [13,] 0.15431598
#> [14,] 0.16816856
#> [15,] 0.46900243
#> [16,] 0.20851303
#> [17,] 0.30161329
#> [18,] 0.32848857
#> [19,] 0.22819699
#> [20,] 0.14170116
#> [21,] 0.21482088
#> [22,] 0.23218038
#> [23,] 0.37315546
#> [24,] 0.17984328
#> [25,] 0.26180407
#> [26,] 0.28256032
#> [27,] 0.35817444
#> [28,] 0.09746117
#> [29,] 0.20252438
#> [30,] 0.22149714
#> [31,] 0.34139211
#> [32,] 0.23458638
#> [33,] 0.18999156
#> [34,] 0.20652119
#> [35,] 0.46699120
#> [36,] 0.13649605
#> [37,] 0.31329404
#> [38,] 0.33882630
#> [39,] 0.23582866
#> [40,] 0.11205099
#> [41,] 0.12388555
#> [42,] 0.13555687
#> [43,] 0.55077946
#> [44,] 0.18977812
#> [45,] 0.18159621
#> [46,] 0.20121468
#> [47,] 0.41695148
#> [48,] 0.20023763
#> [49,] 0.26991497
#> [50,] 0.29232574
#> [51,] 0.28627446
#> [52,] 0.15148483
#> [53,] 0.16707758
#> [54,] 0.18240665
#> [55,] 0.53192780
#> [56,] 0.11858797
#> [57,] 0.33679721
#> [58,] 0.36707094
#> [59,] 0.15008980
#> [60,] 0.14604205
#> [61,] 0.26686881
#> [62,] 0.29079705
#> [63,] 0.35198331
#> [64,] 0.09035083
#> [65,] 0.20882915
#> [66,] 0.22930387
#> [67,] 0.41005113
#> [68,] 0.15181586
#> [69,] 0.15804187
#> [70,] 0.17382173
#> [71,] 0.46103407
#> [72,] 0.20710234
#> [73,] 0.21616974
#> [74,] 0.23456433
#> [75,] 0.28409297
#> [76,] 0.26517296
#> [77,] 0.15378397
#> [78,] 0.16598976
#> [79,] 0.48290350
#> [80,] 0.19732277
#> [81,] 0.23205752
#> [82,] 0.24912408
#> [83,] 0.31809270
#> [84,] 0.20072571
#> [85,] 0.10947735
#> [86,] 0.11974374
#> [87,] 0.66373584
#> [88,] 0.10704307
#> [89,] 0.17944339
#> [90,] 0.19709463
#> [91,] 0.42668080
#> [92,] 0.19678119
#> [93,] 0.36895153
#> [94,] 0.40712055
#> [95,] 0.15776248
#> [96,] 0.06616545
#> [97,] 0.20789495
#> [98,] 0.22694903
#> [99,] 0.43851047
#> [100,] 0.12664554
#> [101,] 0.26836885
#> [102,] 0.29276129
#> [103,] 0.22851188
#> [104,] 0.21035799
#> [105,] 0.16390424
#> [106,] 0.17868537
#> [107,] 0.42279214
#> [108,] 0.23461825
#> [109,] 0.19716268
#> [110,] 0.21552658
#> [111,] 0.42870836
#> [112,] 0.15860237
#> [113,] 0.16735412
#> [114,] 0.18235004
#> [115,] 0.46128506
#> [116,] 0.18901078
#> [117,] 0.29695593
#> [118,] 0.32673703
#> [119,] 0.17146958
#> [120,] 0.20483746
#> [121,] 0.15218846
#> [122,] 0.16602741
#> [123,] 0.50039256
#> [124,] 0.18139157
#> [125,] 0.21889608
#> [126,] 0.23530205
#> [127,] 0.43775703
#> [128,] 0.10804484
#> [129,] 0.12378072
#> [130,] 0.13552170
#> [131,] 0.58892561
#> [132,] 0.15177198
#> [133,] 0.13323973
#> [134,] 0.14587855
#> [135,] 0.59780814
#> [136,] 0.12307358
#> [137,] 0.20623443
#> [138,] 0.22511038
#> [139,] 0.37397664
#> [140,] 0.19467855
#> [141,] 0.28618948
#> [142,] 0.31164055
#> [143,] 0.28937361
#> [144,] 0.11279637
#> [145,] 0.24927314
#> [146,] 0.27175518
#> [147,] 0.40402951
#> [148,] 0.07494216
#> [149,] 0.25699925
#> [150,] 0.27990305
#> [151,] 0.38239187
#> [152,] 0.08070583
#> [153,] 0.25270677
#> [154,] 0.27216877
#> [155,] 0.28695756
#> [156,] 0.18816690
#> [157,] 0.27484947
#> [158,] 0.29982843
#> [159,] 0.30111283
#> [160,] 0.12420926
#> [161,] 0.15213740
#> [162,] 0.16499217
#> [163,] 0.47806587
#> [164,] 0.20480456
#> [165,] 0.18887658
#> [166,] 0.20579089
#> [167,] 0.50166173
#> [168,] 0.10367080
#> [169,] 0.14652635
#> [170,] 0.16020248
#> [171,] 0.51987033
#> [172,] 0.17340084
#> [173,] 0.11145598
#> [174,] 0.12076215
#> [175,] 0.64416855
#> [176,] 0.12361332
#> [177,] 0.09711637
#> [178,] 0.10579393
#> [179,] 0.47132994
#> [180,] 0.32575977
#> [181,] 0.18104064
#> [182,] 0.19377429
#> [183,] 0.39376786
#> [184,] 0.23141721
#> [185,] 0.14264606
#> [186,] 0.15478054
#> [187,] 0.44458692
#> [188,] 0.25798649
#> [189,] 0.23174564
#> [190,] 0.25091771
#> [191,] 0.38751735
#> [192,] 0.12981930
#> [193,] 0.17711773
#> [194,] 0.19115212
#> [195,] 0.31761023
#> [196,] 0.31411992
#> [197,] 0.20117847
#> [198,] 0.21875858
#> [199,] 0.39084117
#> [200,] 0.18922178
#>
#> $utility
#> [,1]
#> [1,] -0.81230865
#> [2,] 13.37475330
#> [3,] 117.15128628
#> [4,] 116.15383878
#> [5,] -0.75738349
#> [6,] 12.56642819
#> [7,] 116.33852687
#> [8,] 113.83685372
#> [9,] 0.31791467
#> [10,] 13.21783433
#> [11,] 115.93641790
#> [12,] 116.74768523
#> [13,] -0.44025865
#> [14,] 13.20495557
#> [15,] 116.52384566
#> [16,] 114.82411596
#> [17,] -0.19155708
#> [18,] 13.35712270
#> [19,] 115.20563703
#> [20,] 114.20650560
#> [21,] 0.14096419
#> [22,] 12.47593898
#> [23,] 115.84002848
#> [24,] 114.30950944
#> [25,] 0.97948237
#> [26,] 13.08995311
#> [27,] 116.49141940
#> [28,] 113.76221503
#> [29,] -0.26404794
#> [30,] 13.95020627
#> [31,] 116.23549887
#> [32,] 115.44873987
#> [33,] -0.40983551
#> [34,] 12.83202534
#> [35,] 116.25530758
#> [36,] 113.67613658
#> [37,] 0.59891882
#> [38,] 13.03475197
#> [39,] 115.51887745
#> [40,] 113.95849460
#> [41,] -0.40411297
#> [42,] 13.88690937
#> [43,] 117.35404663
#> [44,] 115.11988462
#> [45,] -1.41228103
#> [46,] 14.87140853
#> [47,] 116.61733376
#> [48,] 115.07935886
#> [49,] 0.30427881
#> [50,] 12.96492792
#> [51,] 115.64377981
#> [52,] 114.30919945
#> [53,] -0.20881951
#> [54,] 13.72461289
#> [55,] 117.13384668
#> [56,] 113.98676572
#> [57,] -0.23598200
#> [58,] 13.42663386
#> [59,] 114.47534418
#> [60,] 114.41801764
#> [61,] 0.43923719
#> [62,] 14.06920563
#> [63,] 116.71350197
#> [64,] 113.86201408
#> [65,] -0.30322840
#> [66,] 14.54312305
#> [67,] 116.90554234
#> [68,] 114.82207199
#> [69,] -1.43806902
#> [70,] 13.66837844
#> [71,] 116.24040389
#> [72,] 114.56237150
#> [73,] 0.33309592
#> [74,] 13.29599971
#> [75,] 115.78593988
#> [76,] 115.64142571
#> [77,] 0.59232847
#> [78,] 12.71575754
#> [79,] 116.82998471
#> [80,] 114.95334356
#> [81,] 1.09913757
#> [82,] 12.36359709
#> [83,] 115.90458806
#> [84,] 114.93918550
#> [85,] -0.41081463
#> [86,] 13.81722197
#> [87,] 117.78558288
#> [88,] 113.95953769
#> [89,] -1.04726919
#> [90,] 13.84552148
#> [91,] 116.30657235
#> [92,] 114.68371911
#> [93,] -0.29530005
#> [94,] 15.33080464
#> [95,] 115.75646577
#> [96,] 113.93443504
#> [97,] -0.52597785
#> [98,] 13.39353387
#> [99,] 116.34022817
#> [100,] 113.73594339
#> [101,] 0.43899173
#> [102,] 14.24781224
#> [103,] 115.90314107
#> [104,] 115.72956829
#> [105,] -0.34181655
#> [106,] 13.36366094
#> [107,] 116.40877848
#> [108,] 115.17389262
#> [109,] -0.75104700
#> [110,] 13.38474268
#> [111,] 116.19078547
#> [112,] 114.10571506
#> [113,] -0.85943433
#> [114,] 12.76222331
#> [115,] 116.02154006
#> [116,] 114.15069438
#> [117,] -0.70526171
#> [118,] 14.46494638
#> [119,] 114.93143556
#> [120,] 115.30428048
#> [121,] -0.86868665
#> [122,] 12.94618312
#> [123,] 116.32619317
#> [124,] 114.19843431
#> [125,] 1.19017250
#> [126,] 12.66210765
#> [127,] 116.77474450
#> [128,] 113.84098788
#> [129,] -1.00047644
#> [130,] 13.38374045
#> [131,] 116.94713311
#> [132,] 114.10395479
#> [133,] -0.50812216
#> [134,] 13.87676108
#> [135,] 117.42732906
#> [136,] 114.11326501
#> [137,] -0.95605160
#> [138,] 12.94516491
#> [139,] 115.56811524
#> [140,] 114.19919238
#> [141,] -0.04579526
#> [142,] 13.47748805
#> [143,] 115.79700955
#> [144,] 113.82148362
#> [145,] 0.24723584
#> [146,] 13.95401334
#> [147,] 116.83139545
#> [148,] 113.29866270
#> [149,] 0.31395946
#> [150,] 13.86482573
#> [151,] 116.70835960
#> [152,] 113.44640757
#> [153,] 0.91170687
#> [154,] 12.68834626
#> [155,] 115.77612433
#> [156,] 114.89123817
#> [157,] -0.19381583
#> [158,] 13.61367495
#> [159,] 115.85869391
#> [160,] 114.00188640
#> [161,] -0.62325224
#> [162,] 12.25202987
#> [163,] 115.98806009
#> [164,] 114.21056619
#> [165,] 0.11623075
#> [166,] 13.73005442
#> [167,] 117.12670111
#> [168,] 113.82056733
#> [169,] -0.24176831
#> [170,] 13.92229728
#> [171,] 117.23390962
#> [172,] 114.93161084
#> [173,] 0.17325270
#> [174,] 12.90239071
#> [175,] 117.51693744
#> [176,] 114.05543340
#> [177,] -0.71526118
#> [178,] 12.86942923
#> [179,] 116.51267742
#> [180,] 115.73810051
#> [181,] 1.64989043
#> [182,] 12.43921960
#> [183,] 116.70229133
#> [184,] 115.58772664
#> [185,] -0.23155688
#> [186,] 12.72751051
#> [187,] 116.30405635
#> [188,] 115.16286364
#> [189,] 0.61190217
#> [190,] 13.22856842
#> [191,] 116.53289435
#> [192,] 114.23973020
#> [193,] 0.42096522
#> [194,] 12.52496971
#> [195,] 115.70989569
#> [196,] 115.68672510
#> [197,] 0.43952582
#> [198,] 13.73741354
#> [199,] 116.74661828
#> [200,] 115.22559350
#>
# }