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,
one-column matrices with one row per row of the data; with a data.frame
newdata, rows are ordered by id, then by fit-time alternative code
(alt_int in object$alt_mapping). For "shares": the market shares as
a one-column matrix with one row per alternative of object$alt_mapping,
in its order (the outside option's first when it is modelled), 0 for an
alternative that newdata does not offer.
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:0s
predict(fit, type = "shares")
#> [,1]
#> [1,] 0.2010829
#> [2,] 0.2189171
#> [3,] 0.4034452
#> [4,] 0.1765548
predict(fit, type = "probabilities")
#> $choice_prob
#> [,1]
#> [1,] 0.07366054
#> [2,] 0.08059868
#> [3,] 0.52158987
#> [4,] 0.32415091
#> [5,] 0.14586383
#> [6,] 0.15861358
#> [7,] 0.53366293
#> [8,] 0.16185967
#> [9,] 0.13751702
#> [10,] 0.14908140
#> [11,] 0.28854192
#> [12,] 0.42485966
#> [13,] 0.15428536
#> [14,] 0.16815998
#> [15,] 0.46902947
#> [16,] 0.20852519
#> [17,] 0.30160630
#> [18,] 0.32850098
#> [19,] 0.22819609
#> [20,] 0.14169663
#> [21,] 0.21494416
#> [22,] 0.23207348
#> [23,] 0.37314502
#> [24,] 0.17983734
#> [25,] 0.26198795
#> [26,] 0.28240933
#> [27,] 0.35814723
#> [28,] 0.09745549
#> [29,] 0.20244780
#> [30,] 0.22155985
#> [31,] 0.34139814
#> [32,] 0.23459421
#> [33,] 0.18999604
#> [34,] 0.20648147
#> [35,] 0.46701555
#> [36,] 0.13650694
#> [37,] 0.31346190
#> [38,] 0.33868815
#> [39,] 0.23580989
#> [40,] 0.11204006
#> [41,] 0.12382258
#> [42,] 0.13558696
#> [43,] 0.55080078
#> [44,] 0.18978969
#> [45,] 0.18143683
#> [46,] 0.20154692
#> [47,] 0.41683678
#> [48,] 0.20017947
#> [49,] 0.27002295
#> [50,] 0.29222611
#> [51,] 0.28626638
#> [52,] 0.15148456
#> [53,] 0.16702194
#> [54,] 0.18242029
#> [55,] 0.53196278
#> [56,] 0.11859499
#> [57,] 0.33677067
#> [58,] 0.36710201
#> [59,] 0.15008847
#> [60,] 0.14603884
#> [61,] 0.26684838
#> [62,] 0.29081370
#> [63,] 0.35198711
#> [64,] 0.09035081
#> [65,] 0.20870562
#> [66,] 0.22945050
#> [67,] 0.41003451
#> [68,] 0.15180936
#> [69,] 0.15792688
#> [70,] 0.17395008
#> [71,] 0.46102503
#> [72,] 0.20709801
#> [73,] 0.21621241
#> [74,] 0.23450093
#> [75,] 0.28410210
#> [76,] 0.26518456
#> [77,] 0.15387780
#> [78,] 0.16588766
#> [79,] 0.48290789
#> [80,] 0.19732666
#> [81,] 0.23235068
#> [82,] 0.24894039
#> [83,] 0.31802318
#> [84,] 0.20068574
#> [85,] 0.10942018
#> [86,] 0.11976179
#> [87,] 0.66377057
#> [88,] 0.10704747
#> [89,] 0.17933023
#> [90,] 0.19722129
#> [91,] 0.42666715
#> [92,] 0.19678133
#> [93,] 0.36862651
#> [94,] 0.40754964
#> [95,] 0.15768889
#> [96,] 0.06613496
#> [97,] 0.20784465
#> [98,] 0.22698380
#> [99,] 0.43852311
#> [100,] 0.12664845
#> [101,] 0.26832162
#> [102,] 0.29279621
#> [103,] 0.22851929
#> [104,] 0.21036288
#> [105,] 0.16386688
#> [106,] 0.17868070
#> [107,] 0.42281755
#> [108,] 0.23463488
#> [109,] 0.19709107
#> [110,] 0.21557551
#> [111,] 0.42872440
#> [112,] 0.15860902
#> [113,] 0.16732114
#> [114,] 0.18233715
#> [115,] 0.46131854
#> [116,] 0.18902316
#> [117,] 0.29674977
#> [118,] 0.32700783
#> [119,] 0.17143899
#> [120,] 0.20480341
#> [121,] 0.15214769
#> [122,] 0.16603286
#> [123,] 0.50041596
#> [124,] 0.18140349
#> [125,] 0.21914105
#> [126,] 0.23513866
#> [127,] 0.43769034
#> [128,] 0.10802995
#> [129,] 0.12371486
#> [130,] 0.13555992
#> [131,] 0.58894622
#> [132,] 0.15177900
#> [133,] 0.13316981
#> [134,] 0.14592084
#> [135,] 0.59782859
#> [136,] 0.12308076
#> [137,] 0.20617856
#> [138,] 0.22513456
#> [139,] 0.37399645
#> [140,] 0.19469043
#> [141,] 0.28618312
#> [142,] 0.31164540
#> [143,] 0.28937451
#> [144,] 0.11279697
#> [145,] 0.24923726
#> [146,] 0.27177121
#> [147,] 0.40404681
#> [148,] 0.07494472
#> [149,] 0.25698435
#> [150,] 0.27990440
#> [151,] 0.38240222
#> [152,] 0.08070902
#> [153,] 0.25293634
#> [154,] 0.27199737
#> [155,] 0.28691937
#> [156,] 0.18814692
#> [157,] 0.27480672
#> [158,] 0.29987000
#> [159,] 0.30111485
#> [160,] 0.12420844
#> [161,] 0.15216492
#> [162,] 0.16493180
#> [163,] 0.47808668
#> [164,] 0.20481660
#> [165,] 0.18884277
#> [166,] 0.20577857
#> [167,] 0.50170194
#> [168,] 0.10367672
#> [169,] 0.14646135
#> [170,] 0.16023003
#> [171,] 0.51989954
#> [172,] 0.17340907
#> [173,] 0.11147810
#> [174,] 0.12070408
#> [175,] 0.64419822
#> [176,] 0.12361960
#> [177,] 0.09708864
#> [178,] 0.10577352
#> [179,] 0.47135366
#> [180,] 0.32578418
#> [181,] 0.18134314
#> [182,] 0.19362720
#> [183,] 0.39366760
#> [184,] 0.23136207
#> [185,] 0.14266508
#> [186,] 0.15472825
#> [187,] 0.44460520
#> [188,] 0.25800147
#> [189,] 0.23184284
#> [190,] 0.25082719
#> [191,] 0.38751133
#> [192,] 0.12981865
#> [193,] 0.17723767
#> [194,] 0.19104403
#> [195,] 0.31760398
#> [196,] 0.31411432
#> [197,] 0.20118284
#> [198,] 0.21872708
#> [199,] 0.39085542
#> [200,] 0.18923465
#>
#> $utility
#> [,1]
#> [1,] -0.81248107
#> [2,] 11.68791133
#> [3,] 102.52370970
#> [4,] 101.52614091
#> [5,] -0.75752249
#> [6,] 10.87943718
#> [7,] 101.71078333
#> [8,] 99.20877950
#> [9,] 0.31791856
#> [10,] 11.53095315
#> [11,] 101.30861105
#> [12,] 102.12004730
#> [13,] -0.44029903
#> [14,] 11.51807757
#> [15,] 101.89617462
#> [16,] 100.19619163
#> [17,] -0.19158262
#> [18,] 11.67032979
#> [19,] 100.57779899
#> [20,] 99.57845910
#> [21,] 0.14099582
#> [22,] 10.78897427
#> [23,] 101.21224198
#> [24,] 99.68148309
#> [25,] 0.97960195
#> [26,] 11.40305931
#> [27,] 101.86374592
#> [28,] 99.13416964
#> [29,] -0.26410434
#> [30,] 12.26346414
#> [31,] 101.60776408
#> [32,] 100.82092017
#> [33,] -0.40993105
#> [34,] 11.14509206
#> [35,] 101.62757493
#> [36,] 99.04807539
#> [37,] 0.59904031
#> [38,] 11.34785924
#> [39,] 100.89109209
#> [40,] 99.33043783
#> [41,] -0.40418689
#> [42,] 12.20014951
#> [43,] 102.72649474
#> [44,] 100.49204456
#> [45,] -1.41248741
#> [46,] 13.18479653
#> [47,] 101.98969551
#> [48,] 100.45145787
#> [49,] 0.30434135
#> [50,] 11.27798580
#> [51,] 101.01594761
#> [52,] 99.68122280
#> [53,] -0.20889429
#> [54,] 12.03780041
#> [55,] 102.50627393
#> [56,] 99.35870778
#> [57,] -0.23603908
#> [58,] 11.73978443
#> [59,] 99.84739557
#> [60,] 99.79003291
#> [61,] 0.43932111
#> [62,] 12.38246322
#> [63,] 102.08584542
#> [64,] 99.23390694
#> [65,] -0.30326192
#> [66,] 12.85643351
#> [67,] 102.27792460
#> [68,] 100.19413729
#> [69,] -1.43833676
#> [70,] 11.98155445
#> [71,] 101.61270723
#> [72,] 99.93442069
#> [73,] 0.33313711
#> [74,] 11.60915401
#> [75,] 101.15815340
#> [76,] 101.01364190
#> [77,] 0.59241019
#> [78,] 11.02877777
#> [79,] 102.20238451
#> [80,] 100.32548444
#> [81,] 1.09928704
#> [82,] 10.67656249
#> [83,] 101.27680607
#> [84,] 100.31129960
#> [85,] -0.41085484
#> [86,] 12.13042603
#> [87,] 103.15812873
#> [88,] 99.33148655
#> [89,] -1.04747071
#> [90,] 12.15872514
#> [91,] 101.67882635
#> [92,] 100.05579856
#> [93,] -0.29536651
#> [94,] 13.64424699
#> [95,] 101.12868841
#> [96,] 99.30639650
#> [97,] -0.52603923
#> [98,] 11.70671735
#> [99,] 101.71252056
#> [100,] 99.10783338
#> [101,] 0.43903657
#> [102,] 12.56107883
#> [103,] 101.27538601
#> [104,] 101.10176793
#> [105,] -0.34185815
#> [106,] 11.67680039
#> [107,] 101.78109504
#> [108,] 100.54604671
#> [109,] -0.75117955
#> [110,] 11.69788538
#> [111,] 101.56305504
#> [112,] 99.47768157
#> [113,] -0.85956856
#> [114,] 11.07527555
#> [115,] 101.39382153
#> [116,] 99.52268075
#> [117,] -0.70536902
#> [118,] 12.77820870
#> [119,] 100.30352400
#> [120,] 100.67645031
#> [121,] -0.86879736
#> [122,] 11.25929072
#> [123,] 101.69848206
#> [124,] 99.57044410
#> [125,] 1.19037517
#> [126,] 10.97514095
#> [127,] 102.14712813
#> [128,] 99.21296241
#> [129,] -1.00061628
#> [130,] 11.69688148
#> [131,] 102.31951669
#> [132,] 99.47593596
#> [133,] -0.50820867
#> [134,] 12.18996256
#> [135,] 102.79977267
#> [136,] 99.48526280
#> [137,] -0.95621144
#> [138,] 11.25820583
#> [139,] 100.94028986
#> [140,] 99.57117879
#> [141,] -0.04580091
#> [142,] 11.79067160
#> [143,] 101.16922251
#> [144,] 99.19340516
#> [145,] 0.24728823
#> [146,] 12.26722821
#> [147,] 102.20378542
#> [148,] 98.67050528
#> [149,] 0.31399556
#> [150,] 12.17807942
#> [151,] 102.08071766
#> [152,] 98.81830315
#> [153,] 0.91183303
#> [154,] 11.00138018
#> [155,] 101.14833432
#> [156,] 100.26337192
#> [157,] -0.19384864
#> [158,] 11.92687951
#> [159,] 101.23091959
#> [160,] 99.37380586
#> [161,] -0.62335397
#> [162,] 10.56500937
#> [163,] 101.36029045
#> [164,] 99.58256227
#> [165,] 0.11623585
#> [166,] 12.04322322
#> [167,] 102.49915409
#> [168,] 99.19247666
#> [169,] -0.24181158
#> [170,] 12.23551218
#> [171,] 102.60636187
#> [172,] 100.30369982
#> [173,] 0.17332563
#> [174,] 11.21541221
#> [175,] 102.88941062
#> [176,] 99.42739779
#> [177,] -0.71534688
#> [178,] 11.18247116
#> [179,] 101.88498912
#> [180,] 101.11034778
#> [181,] 1.65016755
#> [182,] 10.75221884
#> [183,] 102.07463816
#> [184,] 100.95994081
#> [185,] -0.23158935
#> [186,] 11.04057755
#> [187,] 101.67632713
#> [188,] 100.53499901
#> [189,] 0.61202716
#> [190,] 11.54172719
#> [191,] 101.90521643
#> [192,] 99.61173080
#> [193,] 0.42103060
#> [194,] 10.83800121
#> [195,] 101.08210663
#> [196,] 101.05893643
#> [197,] 0.43959060
#> [198,] 12.05060465
#> [199,] 102.11893666
#> [200,] 100.59775015
#>
# }