Computes choice probabilities or aggregate market shares, either for the
data used at fit time (default) or for counterfactual newdata.
Arguments
- object
A choicer_mnl 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.
a list with elements
X,alt_idx,M(and optionallyweights) matching the layout ofobject$data— the "modified design matrix" path for policy simulation (e.g., perturb a column ofobject$data$X).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 <- 3
dt <- data.table(id = rep(1:N, each = J), alt = rep(1:J, N))
dt[, `:=`(x1 = rnorm(.N), x2 = rnorm(.N))]
#> id alt x1 x2
#> <int> <int> <num> <num>
#> 1: 1 1 1.3709584 -0.04069848
#> 2: 1 2 -0.5646982 -1.55154482
#> 3: 1 3 0.3631284 1.16716955
#> 4: 2 1 0.6328626 -0.27364570
#> 5: 2 2 0.4042683 -0.46784532
#> ---
#> 146: 49 2 1.1133860 -0.47733551
#> 147: 49 3 -0.4809928 -0.16626149
#> 148: 50 1 -0.4331690 0.86256338
#> 149: 50 2 0.6968626 0.09734049
#> 150: 50 3 -1.0563684 -1.62561674
dt[, choice := 0L]
#> id alt x1 x2 choice
#> <int> <int> <num> <num> <int>
#> 1: 1 1 1.3709584 -0.04069848 0
#> 2: 1 2 -0.5646982 -1.55154482 0
#> 3: 1 3 0.3631284 1.16716955 0
#> 4: 2 1 0.6328626 -0.27364570 0
#> 5: 2 2 0.4042683 -0.46784532 0
#> ---
#> 146: 49 2 1.1133860 -0.47733551 0
#> 147: 49 3 -0.4809928 -0.16626149 0
#> 148: 50 1 -0.4331690 0.86256338 0
#> 149: 50 2 0.6968626 0.09734049 0
#> 150: 50 3 -1.0563684 -1.62561674 0
dt[, choice := sample(c(1L, rep(0L, J - 1))), by = id]
#> id alt x1 x2 choice
#> <int> <int> <num> <num> <int>
#> 1: 1 1 1.3709584 -0.04069848 0
#> 2: 1 2 -0.5646982 -1.55154482 0
#> 3: 1 3 0.3631284 1.16716955 1
#> 4: 2 1 0.6328626 -0.27364570 0
#> 5: 2 2 0.4042683 -0.46784532 0
#> ---
#> 146: 49 2 1.1133860 -0.47733551 0
#> 147: 49 3 -0.4809928 -0.16626149 0
#> 148: 50 1 -0.4331690 0.86256338 1
#> 149: 50 2 0.6968626 0.09734049 0
#> 150: 50 3 -1.0563684 -1.62561674 0
fit <- run_mnlogit(dt, "id", "alt", "choice", c("x1", "x2"))
#> Optimization run time 0h:0m:0s
predict(fit, type = "shares")
#> [,1]
#> [1,] 0.34
#> [2,] 0.42
#> [3,] 0.24
predict(fit, type = "probabilities")
#> $choice_prob
#> [,1]
#> [1,] 0.2708302
#> [2,] 0.5673645
#> [3,] 0.1618052
#> [4,] 0.3077412
#> [5,] 0.3980263
#> [6,] 0.2942325
#> [7,] 0.2878646
#> [8,] 0.4939281
#> [9,] 0.2182073
#> [10,] 0.2929888
#> [11,] 0.4284749
#> [12,] 0.2785363
#> [13,] 0.3603875
#> [14,] 0.4367447
#> [15,] 0.2028678
#> [16,] 0.2184864
#> [17,] 0.5072112
#> [18,] 0.2743023
#> [19,] 0.4381148
#> [20,] 0.2953873
#> [21,] 0.2664979
#> [22,] 0.3060323
#> [23,] 0.5639483
#> [24,] 0.1300194
#> [25,] 0.2241651
#> [26,] 0.4367540
#> [27,] 0.3390809
#> [28,] 0.3674445
#> [29,] 0.3640458
#> [30,] 0.2685097
#> [31,] 0.3303873
#> [32,] 0.4600522
#> [33,] 0.2095605
#> [34,] 0.2955867
#> [35,] 0.3620339
#> [36,] 0.3423794
#> [37,] 0.2894037
#> [38,] 0.3639589
#> [39,] 0.3466373
#> [40,] 0.4274169
#> [41,] 0.3354348
#> [42,] 0.2371483
#> [43,] 0.2389208
#> [44,] 0.4812054
#> [45,] 0.2798739
#> [46,] 0.3007901
#> [47,] 0.4843279
#> [48,] 0.2148820
#> [49,] 0.2405516
#> [50,] 0.3813995
#> [51,] 0.3780489
#> [52,] 0.4572181
#> [53,] 0.3551708
#> [54,] 0.1876111
#> [55,] 0.3710444
#> [56,] 0.4170994
#> [57,] 0.2118562
#> [58,] 0.3235239
#> [59,] 0.4924426
#> [60,] 0.1840335
#> [61,] 0.4958708
#> [62,] 0.2686636
#> [63,] 0.2354656
#> [64,] 0.3311905
#> [65,] 0.4381744
#> [66,] 0.2306352
#> [67,] 0.4074041
#> [68,] 0.3157735
#> [69,] 0.2768223
#> [70,] 0.3824068
#> [71,] 0.3792008
#> [72,] 0.2383924
#> [73,] 0.3066418
#> [74,] 0.4957183
#> [75,] 0.1976399
#> [76,] 0.3652028
#> [77,] 0.3690758
#> [78,] 0.2657213
#> [79,] 0.4346713
#> [80,] 0.3464354
#> [81,] 0.2188933
#> [82,] 0.2479087
#> [83,] 0.4991078
#> [84,] 0.2529835
#> [85,] 0.4053025
#> [86,] 0.4159476
#> [87,] 0.1787499
#> [88,] 0.3575386
#> [89,] 0.3609724
#> [90,] 0.2814890
#> [91,] 0.4040337
#> [92,] 0.3997212
#> [93,] 0.1962451
#> [94,] 0.2163260
#> [95,] 0.6218865
#> [96,] 0.1617875
#> [97,] 0.3068949
#> [98,] 0.4874201
#> [99,] 0.2056849
#> [100,] 0.3688766
#> [101,] 0.4326311
#> [102,] 0.1984923
#> [103,] 0.4912425
#> [104,] 0.3192631
#> [105,] 0.1894944
#> [106,] 0.3623769
#> [107,] 0.4323076
#> [108,] 0.2053155
#> [109,] 0.3966166
#> [110,] 0.4170711
#> [111,] 0.1863123
#> [112,] 0.2554243
#> [113,] 0.3756015
#> [114,] 0.3689741
#> [115,] 0.4290376
#> [116,] 0.3501858
#> [117,] 0.2207766
#> [118,] 0.1932650
#> [119,] 0.6372750
#> [120,] 0.1694599
#> [121,] 0.3314357
#> [122,] 0.4506649
#> [123,] 0.2178994
#> [124,] 0.3495967
#> [125,] 0.4596451
#> [126,] 0.1907581
#> [127,] 0.2710333
#> [128,] 0.5627363
#> [129,] 0.1662304
#> [130,] 0.2678105
#> [131,] 0.3387315
#> [132,] 0.3934580
#> [133,] 0.4268929
#> [134,] 0.3472883
#> [135,] 0.2258189
#> [136,] 0.4170867
#> [137,] 0.2797535
#> [138,] 0.3031598
#> [139,] 0.4632003
#> [140,] 0.3094932
#> [141,] 0.2273065
#> [142,] 0.3367077
#> [143,] 0.5179396
#> [144,] 0.1453527
#> [145,] 0.3526659
#> [146,] 0.3931274
#> [147,] 0.2542067
#> [148,] 0.2764395
#> [149,] 0.3531767
#> [150,] 0.3703838
#>
#> $utility
#> [,1]
#> [1,] -0.135290167
#> [2,] 0.604219805
#> [3,] -0.650388810
#> [4,] -0.003964078
#> [5,] 0.253294775
#> [6,] -0.048853237
#> [7,] -0.157677756
#> [8,] 0.382221947
#> [9,] -0.434722614
#> [10,] -0.288898482
#> [11,] 0.091199445
#> [12,] -0.339484178
#> [13,] 0.109063756
#> [14,] 0.301232903
#> [15,] -0.465561285
#> [16,] -0.395823182
#> [17,] 0.446380428
#> [18,] -0.168316300
#> [19,] 0.237975167
#> [20,] -0.156218433
#> [21,] -0.259139268
#> [22,] -0.004079166
#> [23,] 0.607192854
#> [24,] -0.860085903
#> [25,] -0.398396698
#> [26,] 0.268590294
#> [27,] 0.015458995
#> [28,] 0.038439116
#> [29,] 0.029146666
#> [30,] -0.275245873
#> [31,] -0.082806352
#> [32,] 0.248267912
#> [33,] -0.538059330
#> [34,] -0.003382462
#> [35,] 0.199393285
#> [36,] 0.143575024
#> [37,] -0.078061021
#> [38,] 0.151157325
#> [39,] 0.102395268
#> [40,] 0.362138636
#> [41,] 0.119806500
#> [42,] -0.226935339
#> [43,] -0.137956005
#> [44,] 0.562206226
#> [45,] 0.020251082
#> [46,] -0.294839945
#> [47,] 0.181509699
#> [48,] -0.631163416
#> [49,] -0.371069448
#> [50,] 0.089843493
#> [51,] 0.081019597
#> [52,] 0.006091580
#> [53,] -0.246469969
#> [54,] -0.884697492
#> [55,] 0.306512976
#> [56,] 0.423515613
#> [57,] -0.253901020
#> [58,] 0.232423154
#> [59,] 0.652528271
#> [60,] -0.331731860
#> [61,] 0.314415690
#> [62,] -0.298439837
#> [63,] -0.430334813
#> [64,] -0.128363887
#> [65,] 0.151559518
#> [66,] -0.490220432
#> [67,] -0.005117044
#> [68,] -0.259897364
#> [69,] -0.391546831
#> [70,] 0.218530249
#> [71,] 0.210111238
#> [72,] -0.254036582
#> [73,] 0.053970903
#> [74,] 0.534298369
#> [75,] -0.385262733
#> [76,] -0.021356394
#> [77,] -0.010807178
#> [78,] -0.339361099
#> [79,] 0.244568606
#> [80,] 0.017674957
#> [81,] -0.441436978
#> [82,] -0.244759498
#> [83,] 0.455002010
#> [84,] -0.224495968
#> [85,] 0.187811284
#> [86,] 0.213736771
#> [87,] -0.630834865
#> [88,] 0.024504427
#> [89,] 0.034062800
#> [90,] -0.214645477
#> [91,] 0.020489128
#> [92,] 0.009758222
#> [93,] -0.701644946
#> [94,] -0.433363706
#> [95,] 0.622607621
#> [96,] -0.723866208
#> [97,] -0.113973265
#> [98,] 0.348647709
#> [99,] -0.514133198
#> [100,] 0.154017429
#> [101,] 0.313440625
#> [102,] -0.465694758
#> [103,] 0.380882578
#> [104,] -0.050039904
#> [105,] -0.571695967
#> [106,] 0.226137209
#> [107,] 0.402589738
#> [108,] -0.341999930
#> [109,] 0.213679283
#> [110,] 0.263966002
#> [111,] -0.541866697
#> [112,] -0.233906582
#> [113,] 0.151695956
#> [114,] 0.133893651
#> [115,] 0.187630804
#> [116,] -0.015449797
#> [117,] -0.476762660
#> [118,] -0.241246580
#> [119,] 0.951892181
#> [120,] -0.372692635
#> [121,] 0.025898119
#> [122,] 0.333188365
#> [123,] -0.393502076
#> [124,] 0.006099228
#> [125,] 0.279773676
#> [126,] -0.599674662
#> [127,] -0.043213030
#> [128,] 0.687356361
#> [129,] -0.532079654
#> [130,] -0.295355071
#> [131,] -0.060426727
#> [132,] 0.089339791
#> [133,] 0.130646113
#> [134,] -0.075731694
#> [135,] -0.506153759
#> [136,] 0.035873853
#> [137,] -0.363511430
#> [138,] -0.283160226
#> [139,] 0.534522533
#> [140,] 0.131299075
#> [141,] -0.177337443
#> [142,] -0.008717688
#> [143,] 0.421925552
#> [144,] -0.848769715
#> [145,] 0.072051248
#> [146,] 0.180663838
#> [147,] -0.255322371
#> [148,] -0.152256184
#> [149,] 0.092720226
#> [150,] 0.140291478
#>
# Counterfactual: increase x1 for alternative 2
dt_cf <- copy(dt)[alt == 2, x1 := x1 + 1]
predict(fit, type = "shares", newdata = dt_cf)
#> [,1]
#> [1,] 0.3544688
#> [2,] 0.3952819
#> [3,] 0.2502493
# }