| fit | R Documentation |
Fits a Bayesian probit choice model to a data.frame of choice data. If
data = NULL, probit choice data are simulated with the
choicedata package before
estimation.
fit(
formula,
data = NULL,
random_effects = character(),
latent_class_effects = character(),
alternatives = NULL,
base = NULL,
choice_type = c("unordered", "ordered", "ranked"),
format = c("wide", "long"),
column_decider = "deciderID",
column_occasion = NULL,
column_alternative = NULL,
delimiter = "_",
scale = NULL,
prior = NULL,
classes = 1L,
class_update = c("fixed", "sparse", "dirichlet_process", "weight_based"),
max_classes = 10L,
weight_based_control = NULL,
iterations = 1000L,
warmup = iterations%/%2L,
thin = 1L,
chains = 4L,
save_individual_draws = FALSE,
n_deciders = 100L,
n_occasions = 1L,
n_alternatives = NULL,
covariates = NULL,
dgp_parameters = NULL,
progress = interactive()
)
formula |
[ |
data |
[ |
random_effects |
[ |
latent_class_effects |
[ |
alternatives |
[ |
base |
[ |
choice_type |
[
|
format |
[
|
column_decider |
[ |
column_occasion |
[ |
column_alternative |
[ |
delimiter |
[ |
scale |
[
|
prior |
[ |
classes |
[ |
class_update |
[
|
max_classes |
[ |
weight_based_control |
[
|
iterations |
[ |
warmup |
[ |
thin |
[ |
chains |
[ |
save_individual_draws |
[ Enable this only when you need them, because they dominate the memory the fitted object occupies. |
n_deciders |
[ |
n_occasions |
[ |
n_alternatives |
[ |
covariates |
[ |
dgp_parameters |
[
Unspecified parameters are drawn at random. |
progress |
[ |
An RprobitB_fit object, which is a list with the components:
call: the matched call.
data: the data used.
model: the model specification.
prior: the prior specification.
draws: the posterior draws.
sampler: iterations, warmup, thinning, chains, and elapsed times.
simulation: the dgp_parameters that generated the data and the
simulation sizes if data = NULL, otherwise NULL.
Utilities are identified only up to level and scale. base selects the
alternative whose utility is subtracted from all others, and scale fixes
one parameter to identify the scale. The sampler fixes the error variance
of the first utility difference at one and draws the error covariance by
the marginal data augmentation of Imai and van Dyk (2005), which expands
the scale of the latent utilities in every iteration and returns to the
fixed one afterwards, so that the covariance and the coefficients move
freely. Every retained draw is then rescaled to the normalization in
scale.
Unordered choices may be made from occasion-specific subsets of the alternatives. In long format, an occasion lists only the rows of its available alternatives. The latent utilities of unavailable alternatives are imputed from their conditional distribution without a truncation, so they do not restrict the choice. Predictions assign probability zero to unavailable alternatives. Ordered and ranked models require complete choice sets.
An unnamed random_effects vector is a shorthand for correlated normal
effects, so random_effects = c("price", "time") is the same as
random_effects = c(price = "cn", time = "cn").
A mixture model divides the deciders into classes latent classes and
allocates every decider to exactly one of them. latent_class_effects
decides which mixture model this is:
A random effect named there follows a class-specific normal
distribution. This is the latent class mixed probit model, reported as
mu[<effect>,<class>] and Omega[<effect>,<effect>,<class>].
An effect that is named there but has no random effect is a single
coefficient per class. This is the classical latent class model,
reported as beta[<effect>,<class>].
class_update decides how many of the latent classes are used:
"fixed" keeps all classes classes occupied, so the posterior is the
finite-mixture posterior given that all of them are used.
"sparse" starts from classes classes, deliberately more than
expected, and empties the superfluous ones through a small symmetric
Dirichlet weight prior (Rousseau and Mengersen 2011;
Frühwirth-Schnatter and Malsiner-Walli 2019).
"dirichlet_process" creates and removes occupied classes with
Neal's (2000) auxiliary-parameter allocation update, up to
max_classes. Its precision hyperparameter is updated with the
beta-gamma augmentation of Escobar and West (1995).
"weight_based" splits, removes, and merges classes during warmup
and then keeps their number fixed. Every buffer iterations it removes the
class below epsmin, splits the class above epsmax, or merges
the closest pair of class means below deltamin, attempting at most
one change in that order. A split moves the two means by deltashift times
the leading within-class standard deviation. Updates stop after warmup, and
retained iterations condition on the dimension selected separately by each
chain.
Numbering the latent classes differently describes the same mixture, every fit with more than one possible class therefore relabels its retained draws before they are summarized. The representative assignment of deciders to classes is the draw that is closest to the posterior co-clustering matrix in the least-squares sense (Dahl 2006). Every draw is then renumbered to agree with it, using the equivalence classes representatives assignment of Papastamoulis and Iliopoulos (2010), and the same permutation is applied to weights, means, covariances, and allocations. Finally the classes are numbered by decreasing posterior mean weight.
The prior is conjugate where available; the finite-mixture concentration is
updated by a log-scale Metropolis-Hastings step when it has a gamma
hyperprior. prior is a named list that overrides the following defaults,
where P_f, P_l, and P_r count the fixed effects without latent
classes, the latent class effects, and the random effects, and J the
alternatives:
fixed_mean [numeric(P_f)] and fixed_covariance
[matrix(P_f, P_f)]: normal prior for the fixed coefficients without
latent classes, default rep(0, P_f) and 10 * diag(P_f).
latent_class_mean [numeric(P_l)] and latent_class_covariance
[matrix(P_l, P_l)]: normal prior for the class-specific values of
the latent class effects, the same for every class, default
rep(0, P_l) and 10 * diag(P_l).
random_mean [numeric(P_r)] and random_mean_covariance
[matrix(P_r, P_r)]: normal prior for the means of the random
coefficients, default rep(0, P_r) and 10 * diag(P_r).
random_covariance_df [integer(1)] and random_covariance_scale
[matrix(P_r, P_r)]: inverse Wishart prior for the covariance
matrices of the random coefficients, default P_r + 2 and diag(P_r).
Entries between uncorrelated random effects must be zero. In a mixture,
both priors apply to the block of the random effects with latent classes
in every class and to the block without latent classes once.
class_concentration [numeric(1) | named numeric(2)]: fixed
symmetric Dirichlet concentration for finite weights or precision of the
Dirichlet process. A named c(shape = ..., rate = ...) instead places a
gamma hyperprior on it. Defaults are 1 for a fixed finite or weight-based
mixture,
c(shape = 1, rate = 200) for a sparse finite mixture, and
c(shape = 2, rate = 4) for a Dirichlet process mixture.
error_covariance_df [integer(1)] and error_covariance_scale
[matrix(J - 1, J - 1)]: inverse Wishart prior for the unrestricted
error covariance of the utility differences, default J + 1 and
diag(J - 1). The prior of the identified covariance is that of the
unrestricted covariance divided by its first diagonal element. Not used
for ordered models.
threshold_mean [numeric(J - 2)] and threshold_covariance
[matrix(J - 2, J - 2)]: normal prior for the logarithmic increments
between the ordered thresholds, default rep(0, J - 2) and
diag(J - 2). Only used for ordered models.
The structure of formula is choice ~ A | B | C, i.e., a standard
formula object but with three parts on the right-hand
side, separated by |, where
choice is the name of the discrete response variable,
A are names of alternative-specific covariates with
a coefficient that is constant across alternatives,
B are names of covariates that are constant across
alternatives,
and C are names of alternative-specific covariates
with alternative-specific coefficients.
The following rules apply:
By default, intercepts (referred to as alternative-specific
constants, ASCs) are added to the model. They can be removed by adding
+ 0 in the second part, e.g., choice ~ A | B + 0 | C. To not include
any covariates of the second type but to estimate ASCs, add 1 in the
second part, e.g., choice ~ A | 1 | C. The expression
choice ~ A | 0 | C is interpreted as no covariates of the second type
and no ASCs.
To not include covariates of any type, add 0 in the respective
part, e.g., choice ~ 0 | B | C.
Some parts of the formula can be omitted when there is no ambiguity.
For example, choice ~ A is equivalent to choice ~ A | 1 | 0.
Multiple covariates in one part are separated by a + sign, e.g.,
choice ~ A1 + A2.
Arithmetic transformations of covariates in all three parts of the
right-hand side are possible via the function I(), e.g.,
choice ~ I(A1^2 + A2 * 2). In this case, a random effect can be defined
for the transformed covariate, e.g.,
random_effects = c("I(A1^2 + A2 * 2)" = "cn").
Ordered choice models have a single utility per choice occasion.
Their covariates must be placed in the first part and ASCs must be
removed, e.g., choice ~ age + income | 0.
Specify random effects as "<covariate>" = "<distribution>". Each covariate
must appear explicitly on the right-hand side of formula; use "ASC" for
alternative-specific constants.
Available distributions are:
"cn": correlated normal
"n": uncorrelated normal
"cln": positively signed correlated log-normal
"ln": positively signed uncorrelated log-normal
"cln-": negatively signed correlated log-normal
"ln-": negatively signed uncorrelated log-normal
The covariates in latent_class_effects have effects that differ between
the latent classes of a mixture model; use "ASC" for alternative-specific
constants. A random effect named here has a class-specific mean and
covariance, any other effect a class-specific coefficient. Effects that
are not named are the same in every class, and random effects with and
without latent class effects are uncorrelated. A model with more than one
latent class needs at least one latent class effect.
Dahl2006RprobitB
\insertRefEscobar1995RprobitB
\insertRefFruehwirthSchnatter2019RprobitB
\insertRefGreene2003RprobitB
\insertRefImai2005aRprobitB
\insertRefNeal2000RprobitB
\insertRefOelschlaeger2021RprobitB
\insertRefOelschlaeger2026cRprobitB
\insertRefPapastamoulis2010RprobitB
\insertRefRousseau2011RprobitB
### Fit a probit model to panel choice data
data("Train", package = "mlogit")
Train$price_A <- Train$price_A / 100 / 2.20371 # price in Euro
Train$price_B <- Train$price_B / 100 / 2.20371
Train$time_A <- Train$time_A / 60 # time in hours
Train$time_B <- Train$time_B / 60
model <- fit(
choice ~ price + time + change + factor(comfort) | 0,
data = Train,
column_decider = "id",
column_occasion = "choiceid",
scale = c(price = -1), # other coefficients are willingness-to-pay
chains = 1
)
summary(model)
interpret(model)
### Simulate choice data and compare the estimates with the truth
set.seed(1)
simulated <- fit(
choice ~ x | y,
dgp_parameters = list(beta = c(x = 1, y_B = -1)),
covariates = list(y = rpois(400, lambda = 3)),
iterations = 2000,
chains = 1,
n_deciders = 400
)
head(model.frame(simulated))
summary(simulated)
confint(simulated)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.