Computes simulated choice probabilities or aggregate market shares using
deterministic Halton draws, either for the data used at fit time (default)
or for counterfactual newdata.
Arguments
- object
A choicer_mxl object.
- type
Either "probabilities" (per-observation simulated choice probabilities) or "shares" (aggregate simulated 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, fixed-coefficient, and random-coefficient 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,W,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). Halton draws are regenerated deterministically fromobject$draws_infowith one block of draws per choice situation innewdata.- 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 averaged across simulation draws. For "shares": a named numeric
vector of simulated 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); for a panel fit whose person_col
is present in newdata, by decision maker first, which reproduces the
in-sample row order. Predictions are unconditional: each choice
situation integrates over the population taste distribution (see
conditional_tastes() for tastes conditional on observed choices).
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), w1 = rnorm(.N))]
#> id alt x1 w1
#> <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 w1 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 w1 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_mxlogit(
data = dt, id_col = "id", alt_col = "alt", choice_col = "choice",
covariate_cols = "x1", random_var_cols = "w1", S = 50L
)
#> Optimization run time 0h:0m:0s
predict(fit, type = "shares")
#> [,1]
#> [1,] 0.3475232
#> [2,] 0.4301771
#> [3,] 0.2222997
predict(fit, type = "probabilities")
#> $choice_prob
#> [,1]
#> [1,] 0.19852658
#> [2,] 0.51224636
#> [3,] 0.28922706
#> [4,] 0.33309726
#> [5,] 0.42038100
#> [6,] 0.24652174
#> [7,] 0.35690329
#> [8,] 0.50003778
#> [9,] 0.14305893
#> [10,] 0.43668228
#> [11,] 0.31078840
#> [12,] 0.25252932
#> [13,] 0.34676164
#> [14,] 0.44961991
#> [15,] 0.20361845
#> [16,] 0.38476402
#> [17,] 0.48465431
#> [18,] 0.13058166
#> [19,] 0.38153971
#> [20,] 0.42356080
#> [21,] 0.19489948
#> [22,] 0.25997375
#> [23,] 0.48390139
#> [24,] 0.25612486
#> [25,] 0.35937248
#> [26,] 0.33947915
#> [27,] 0.30114837
#> [28,] 0.38964031
#> [29,] 0.40486808
#> [30,] 0.20549161
#> [31,] 0.33505466
#> [32,] 0.47683203
#> [33,] 0.18811332
#> [34,] 0.37008946
#> [35,] 0.32949465
#> [36,] 0.30041589
#> [37,] 0.31134834
#> [38,] 0.38071675
#> [39,] 0.30793491
#> [40,] 0.41522729
#> [41,] 0.45426500
#> [42,] 0.13050771
#> [43,] 0.39123001
#> [44,] 0.45676834
#> [45,] 0.15200165
#> [46,] 0.36495212
#> [47,] 0.49329126
#> [48,] 0.14175662
#> [49,] 0.41753479
#> [50,] 0.21962668
#> [51,] 0.36283854
#> [52,] 0.41235694
#> [53,] 0.31930927
#> [54,] 0.26833378
#> [55,] 0.35975573
#> [56,] 0.44645573
#> [57,] 0.19378854
#> [58,] 0.31723843
#> [59,] 0.49977069
#> [60,] 0.18299087
#> [61,] 0.43635038
#> [62,] 0.47864317
#> [63,] 0.08500645
#> [64,] 0.33047521
#> [65,] 0.51118963
#> [66,] 0.15833515
#> [67,] 0.33800876
#> [68,] 0.48769367
#> [69,] 0.17429757
#> [70,] 0.40125826
#> [71,] 0.47870628
#> [72,] 0.12003546
#> [73,] 0.25343134
#> [74,] 0.47508683
#> [75,] 0.27148183
#> [76,] 0.34693538
#> [77,] 0.46815063
#> [78,] 0.18491398
#> [79,] 0.39082488
#> [80,] 0.49503034
#> [81,] 0.11414478
#> [82,] 0.42678214
#> [83,] 0.45979397
#> [84,] 0.11342389
#> [85,] 0.31654924
#> [86,] 0.38280290
#> [87,] 0.30064787
#> [88,] 0.32416650
#> [89,] 0.43140686
#> [90,] 0.24442664
#> [91,] 0.39909879
#> [92,] 0.41801514
#> [93,] 0.18288607
#> [94,] 0.20179065
#> [95,] 0.49939845
#> [96,] 0.29881090
#> [97,] 0.36687417
#> [98,] 0.49385326
#> [99,] 0.13927257
#> [100,] 0.31794562
#> [101,] 0.42446849
#> [102,] 0.25758589
#> [103,] 0.43420129
#> [104,] 0.26363452
#> [105,] 0.30216419
#> [106,] 0.33677590
#> [107,] 0.41561757
#> [108,] 0.24760652
#> [109,] 0.36530730
#> [110,] 0.35038478
#> [111,] 0.28430791
#> [112,] 0.33705923
#> [113,] 0.30324361
#> [114,] 0.35969716
#> [115,] 0.40750107
#> [116,] 0.44426009
#> [117,] 0.14823883
#> [118,] 0.25891981
#> [119,] 0.51518231
#> [120,] 0.22589788
#> [121,] 0.38354884
#> [122,] 0.46799615
#> [123,] 0.14845501
#> [124,] 0.28294925
#> [125,] 0.46368415
#> [126,] 0.25336660
#> [127,] 0.22600933
#> [128,] 0.49346284
#> [129,] 0.28052783
#> [130,] 0.32282496
#> [131,] 0.30126232
#> [132,] 0.37591272
#> [133,] 0.40932153
#> [134,] 0.45633513
#> [135,] 0.13434334
#> [136,] 0.23243793
#> [137,] 0.48875471
#> [138,] 0.27880736
#> [139,] 0.43591639
#> [140,] 0.46017313
#> [141,] 0.10391048
#> [142,] 0.19437700
#> [143,] 0.46029900
#> [144,] 0.34532400
#> [145,] 0.37391086
#> [146,] 0.43760592
#> [147,] 0.18848322
#> [148,] 0.38256037
#> [149,] 0.27665012
#> [150,] 0.34078951
#>
#> $utility
#> [,1]
#> [1,] -0.1249172057
#> [2,] 0.4913419116
#> [3,] -0.8305745829
#> [4,] -0.0525720621
#> [5,] 0.2571403926
#> [6,] -0.6383276551
#> [7,] -0.1420759329
#> [8,] 0.3055983667
#> [9,] -0.8607869657
#> [10,] -0.0002005646
#> [11,] 0.1613280935
#> [12,] -0.8898980125
#> [13,] 0.1362490838
#> [14,] 0.2970807385
#> [15,] -0.6471185704
#> [16,] -0.1484249857
#> [17,] 0.3714004900
#> [18,] -0.4580414073
#> [19,] 0.2308768602
#> [20,] 0.1725028875
#> [21,] -0.6544444603
#> [22,] 0.1622125159
#> [23,] 0.3106474353
#> [24,] -0.8049787952
#> [25,] -0.1669123518
#> [26,] 0.3217737923
#> [27,] -0.6747259871
#> [28,] 0.1739005533
#> [29,] 0.2460002734
#> [30,] -0.6197125912
#> [31,] -0.0485508369
#> [32,] 0.2389366538
#> [33,] -0.7911124395
#> [34,] 0.0503889848
#> [35,] 0.2422928276
#> [36,] -0.4870166082
#> [37,] 0.0739240118
#> [38,] 0.3634108332
#> [39,] -0.4528571289
#> [40,] -0.0161613999
#> [41,] 0.2655203542
#> [42,] -0.6486382331
#> [43,] -0.0685472883
#> [44,] 0.3373801117
#> [45,] -0.5616777188
#> [46,] -0.0418927420
#> [47,] 0.3591615425
#> [48,] -0.8163415334
#> [49,] -0.0280066904
#> [50,] 0.2167080361
#> [51,] -0.6345167385
#> [52,] 0.0878305773
#> [53,] 0.1844453042
#> [54,] -0.6534145085
#> [55,] 0.0108393013
#> [56,] 0.2733609442
#> [57,] -0.7338678471
#> [58,] -0.0397506830
#> [59,] 0.5456801771
#> [60,] -0.7121517938
#> [61,] 0.0489822833
#> [62,] 0.2413601880
#> [63,] -0.7358786957
#> [64,] -0.1286633041
#> [65,] 0.3338541081
#> [66,] -0.8037362139
#> [67,] -0.0380580613
#> [68,] 0.2577921801
#> [69,] -0.7770843334
#> [70,] 0.0127729306
#> [71,] 0.3571835676
#> [72,] -0.6492894914
#> [73,] -0.0624982360
#> [74,] 0.3651155230
#> [75,] -0.6256251057
#> [76,] -0.0519013941
#> [77,] 0.2028116033
#> [78,] -0.7197603156
#> [79,] 0.0861060200
#> [80,] 0.3814687349
#> [81,] -0.8210191438
#> [82,] -0.0078374387
#> [83,] 0.2541162963
#> [84,] -0.6765487406
#> [85,] 0.1213375337
#> [86,] 0.2387077958
#> [87,] -0.7043476835
#> [88,] 0.0163567112
#> [89,] 0.2044328136
#> [90,] -0.7918199866
#> [91,] -0.1536602533
#> [92,] 0.3591122688
#> [93,] -0.7018760077
#> [94,] -0.1476183529
#> [95,] 0.4062722047
#> [96,] -0.6263394451
#> [97,] 0.0704357368
#> [98,] 0.4214924540
#> [99,] -0.7123070550
#> [100,] -0.0425361434
#> [101,] 0.1916202158
#> [102,] -0.7790919883
#> [103,] 0.0821054713
#> [104,] 0.1112992711
#> [105,] -0.6038197387
#> [106,] -0.0539405511
#> [107,] 0.2915503883
#> [108,] -0.6663517981
#> [109,] 0.0056813048
#> [110,] 0.2808584766
#> [111,] -0.6975422373
#> [112,] -0.0140348950
#> [113,] 0.3273999368
#> [114,] -0.6250221377
#> [115,] 0.1543047327
#> [116,] 0.3581550562
#> [117,] -0.6018868204
#> [118,] -0.2558228458
#> [119,] 0.3884182200
#> [120,] -0.6922390674
#> [121,] 0.1740690447
#> [122,] 0.4242793769
#> [123,] -0.6823734375
#> [124,] 0.0586367057
#> [125,] 0.3157103773
#> [126,] -0.7461834020
#> [127,] 0.0501091587
#> [128,] 0.4938912478
#> [129,] -0.5870168520
#> [130,] -0.0369498220
#> [131,] 0.2161327390
#> [132,] -0.6057827294
#> [133,] 0.0069287092
#> [134,] 0.1698763395
#> [135,] -0.8158413005
#> [136,] 0.1231627942
#> [137,] 0.4352583715
#> [138,] -0.8397854070
#> [139,] 0.1690397820
#> [140,] 0.2720271468
#> [141,] -0.6311209637
#> [142,] 0.0864046807
#> [143,] 0.3198324687
#> [144,] -0.6627397199
#> [145,] 0.0414469718
#> [146,] 0.1881950871
#> [147,] -0.6312195782
#> [148,] 0.0564742548
#> [149,] 0.2194728074
#> [150,] -0.6100887701
#>
# }