Skip to contents

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.

Usage

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

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 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). Halton draws are regenerated deterministically from object$draws_info with one block of draws per choice situation in newdata.

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 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).

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:0.01s
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.15200164
#>  [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.48769368
#>  [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.43420128
#> [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.28294924
#> [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.1249172050
#>   [2,]  0.4913419134
#>   [3,] -0.8305745832
#>   [4,] -0.0525720618
#>   [5,]  0.2571403948
#>   [6,] -0.6383276554
#>   [7,] -0.1420759322
#>   [8,]  0.3055983686
#>   [9,] -0.8607869650
#>  [10,] -0.0002005646
#>  [11,]  0.1613280960
#>  [12,] -0.8898980117
#>  [13,]  0.1362490832
#>  [14,]  0.2970807402
#>  [15,] -0.6471185708
#>  [16,] -0.1484249855
#>  [17,]  0.3714004918
#>  [18,] -0.4580414089
#>  [19,]  0.2308768590
#>  [20,]  0.1725028901
#>  [21,] -0.6544444607
#>  [22,]  0.1622125150
#>  [23,]  0.3106474371
#>  [24,] -0.8049787949
#>  [25,] -0.1669123509
#>  [26,]  0.3217737940
#>  [27,] -0.6747259876
#>  [28,]  0.1739005525
#>  [29,]  0.2460002756
#>  [30,] -0.6197125918
#>  [31,] -0.0485508367
#>  [32,]  0.2389366561
#>  [33,] -0.7911124394
#>  [34,]  0.0503889845
#>  [35,]  0.2422928297
#>  [36,] -0.4870166093
#>  [37,]  0.0739240115
#>  [38,]  0.3634108347
#>  [39,] -0.4528571304
#>  [40,] -0.0161613999
#>  [41,]  0.2655203563
#>  [42,] -0.6486382336
#>  [43,] -0.0685472879
#>  [44,]  0.3373801133
#>  [45,] -0.5616777198
#>  [46,] -0.0418927418
#>  [47,]  0.3591615440
#>  [48,] -0.8163415330
#>  [49,] -0.0280066907
#>  [50,]  0.2167080383
#>  [51,] -0.6345167386
#>  [52,]  0.0878305769
#>  [53,]  0.1844453070
#>  [54,] -0.6534145084
#>  [55,]  0.0108393013
#>  [56,]  0.2733609463
#>  [57,] -0.7338678471
#>  [58,] -0.0397506830
#>  [59,]  0.5456801776
#>  [60,] -0.7121517940
#>  [61,]  0.0489822831
#>  [62,]  0.2413601900
#>  [63,] -0.7358786957
#>  [64,] -0.1286633034
#>  [65,]  0.3338541096
#>  [66,] -0.8037362136
#>  [67,] -0.0380580612
#>  [68,]  0.2577921826
#>  [69,] -0.7770843333
#>  [70,]  0.0127729311
#>  [71,]  0.3571835690
#>  [72,] -0.6492894918
#>  [73,] -0.0624982358
#>  [74,]  0.3651155244
#>  [75,] -0.6256251062
#>  [76,] -0.0519013938
#>  [77,]  0.2028116056
#>  [78,] -0.7197603157
#>  [79,]  0.0861060195
#>  [80,]  0.3814687363
#>  [81,] -0.8210191433
#>  [82,] -0.0078374386
#>  [83,]  0.2541162982
#>  [84,] -0.6765487410
#>  [85,]  0.1213375332
#>  [86,]  0.2387077980
#>  [87,] -0.7043476839
#>  [88,]  0.0163567111
#>  [89,]  0.2044328160
#>  [90,] -0.7918199866
#>  [91,] -0.1536602527
#>  [92,]  0.3591122705
#>  [93,] -0.7018760076
#>  [94,] -0.1476183523
#>  [95,]  0.4062722061
#>  [96,] -0.6263394458
#>  [97,]  0.0704357362
#>  [98,]  0.4214924552
#>  [99,] -0.7123070553
#> [100,] -0.0425361431
#> [101,]  0.1916202183
#> [102,] -0.7790919881
#> [103,]  0.0821054708
#> [104,]  0.1112992740
#> [105,] -0.6038197394
#> [106,] -0.0539405511
#> [107,]  0.2915503900
#> [108,] -0.6663517985
#> [109,]  0.0056813049
#> [110,]  0.2808584786
#> [111,] -0.6975422377
#> [112,] -0.0140348949
#> [113,]  0.3273999385
#> [114,] -0.6250221382
#> [115,]  0.1543047319
#> [116,]  0.3581550580
#> [117,] -0.6018868210
#> [118,] -0.2558228445
#> [119,]  0.3884182212
#> [120,] -0.6922390676
#> [121,]  0.1740690440
#> [122,]  0.4242793781
#> [123,] -0.6823734378
#> [124,]  0.0586367052
#> [125,]  0.3157103793
#> [126,] -0.7461834027
#> [127,]  0.0501091584
#> [128,]  0.4938912488
#> [129,] -0.5870168530
#> [130,] -0.0369498220
#> [131,]  0.2161327412
#> [132,] -0.6057827299
#> [133,]  0.0069287092
#> [134,]  0.1698763420
#> [135,] -0.8158413001
#> [136,]  0.1231627937
#> [137,]  0.4352583735
#> [138,] -0.8397854068
#> [139,]  0.1690397819
#> [140,]  0.2720271487
#> [141,] -0.6311209640
#> [142,]  0.0864046803
#> [143,]  0.3198324704
#> [144,] -0.6627397202
#> [145,]  0.0414469716
#> [146,]  0.1881950896
#> [147,] -0.6312195788
#> [148,]  0.0564742546
#> [149,]  0.2194728096
#> [150,] -0.6100887709
#> 
# }