These functions prepare and evaluate the likelihood contribution of observed choices for a given choice model.
choice_likelihood()pre-computes the design matrices and choice indices implied bychoice_dataandchoice_effects. The returned object stores these quantities so that repeated likelihood evaluations during maximum likelihood estimation avoid redundant work.compute_choice_likelihood()evaluates the (log-)likelihood for givenchoice_parametersand a pre-computedchoice_likelihoodobject.
Usage
choice_likelihood(
choice_data,
choice_effects,
choice_identifiers = extract_choice_identifiers(choice_data),
input_checks = TRUE,
...
)
compute_choice_likelihood(
choice_parameters,
choice_likelihood,
logarithm = TRUE,
negative = FALSE,
aggregate = c("total", "decider", "occasion"),
...
)Arguments
- choice_data
[
choice_data]
Achoice_dataobject.- choice_effects
[
choice_effects]
Achoice_effectsobject.- choice_identifiers
[
choice_identifiers]
Achoice_identifiersobject. The default is extracted fromchoice_data.- input_checks
[
logical(1)]
Check the pre-computed design matrices and choice indices against the parameters on the first likelihood evaluation? Later evaluations skip these checks because the pre-computed quantities do not change.- ...
Additional arguments for the probability computation:
n_draws[integer(1)]: The number of draws for simulated probabilities, which are required for mixed logit models and for mixed probit models with non-normal random effects. The default is200.draws[matrix]: A matrix of standard normal draws with one column per random effect that replaces the generated draws.cml[character(1)]: Composite marginal likelihood for panel probit models. Either"no"(default, the full likelihood),"fp"(all pairs of choice occasions), or"ap"(adjacent pairs of choice occasions).ghk_draws[integer(1)]: The number of draws of the GHK simulator for multivariate normal probabilities in probit models, seepmvnorm. Probabilities of up to three dimensions are computed exactly; higher dimensions use the GHK simulator on a fixed sequence of quasi-random Halton points. The default is500.
- choice_parameters
[
choice_parameters|numeric()|list()]
Achoice_parametersobject.A numeric vector in optimization space is also accepted and converted with
switch_parameter_space().A list of either representation is evaluated as a batch of parameter draws.
- choice_likelihood
[
choice_likelihood]
Achoice_likelihoodobject.- logarithm
[
logical(1)]
Return the log-likelihood? IfFALSE, the likelihood is returned.- negative
[
logical(1)]
Return the negative (log-)likelihood? Useful for minimization routines.- aggregate
[
character(1)]
Unit of the returned likelihood:"occasion"returns one contribution per observed choice occasion,"decider"returns one joint contribution per decider,"total"sums the decider contributions.
Value
choice_likelihood() returns an object of class choice_likelihood, which
is a list containing the design matrices, choice indices, and identifiers.
compute_choice_likelihood() returns a numeric scalar for
aggregate = "total" and a named numeric vector otherwise.
Examples
### compute choice likelihood
data(list = "train_choice")
choice_effects <- choice_effects(
choice_formula = choice_formula(
formula = choice ~ price | time,
error_term = "probit"
),
choice_alternatives = choice_alternatives(
J = 2,
alternatives = c("A", "B")
)
)
choice_data <- choice_data(
data_frame = train_choice,
format = "wide",
column_choice = "choice",
column_decider = "deciderID",
column_occasion = "occasionID"
)
likelihood <- choice_likelihood(
choice_data = choice_data,
choice_effects = choice_effects
)
choice_parameters <- generate_choice_parameters(
choice_effects = choice_effects
)
compute_choice_likelihood(
choice_parameters = choice_parameters,
choice_likelihood = likelihood,
logarithm = TRUE
)
#> [1] -141614.3
