| predict.choicer_nl | R Documentation |
Computes choice probabilities or aggregate market shares, either for the
data used at fit time (default) or for counterfactual newdata.
## S3 method for class 'choicer_nl'
predict(
object,
type = c("probabilities", "shares"),
newdata = NULL,
weights = NULL,
...
)
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:
When |
weights |
Optional numeric vector with one weight per choice situation,
used for |
... |
Additional arguments (ignored). |
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).
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)]
dt[, `:=`(x1 = rnorm(.N), x2 = rnorm(.N))]
dt[, choice := 0L]
dt[, choice := sample(c(1L, rep(0L, J - 1))), by = id]
fit <- run_nestlogit(dt, "id", "alt", "choice", c("x1", "x2"), "nest")
predict(fit, type = "shares")
predict(fit, type = "probabilities")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.