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_data
andchoice_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 a given set ofchoice_parameters
. It can either take the original choice objects or a pre-computedchoice_likelihood
object.
Usage
choice_likelihood(
choice_data,
choice_effects,
choice_identifiers = extract_choice_identifiers(choice_data),
input_checks = TRUE,
lower_bound = 1e-10,
...
)
compute_choice_likelihood(
choice_parameters,
choice_data,
choice_effects,
logarithm = TRUE,
negative = FALSE,
lower_bound = 1e-10,
input_checks = TRUE,
...
)
Arguments
- choice_data
[
choice_data
]
Achoice_data
object with the observed choices.- choice_effects
[
choice_effects
]
Achoice_effects
object that determines the model effects.- choice_identifiers
[
choice_identifiers
]
Achoice_identifiers
object. The default extracts identifiers fromchoice_data
.- input_checks
[
logical(1)
]
Forwarded tochoiceprob_probit
orchoiceprob_logit
to control additional input validation.- lower_bound
[
numeric(1)
]
The minimum probability used when computing the log-likelihood. Values below this bound are truncated to avoid taking the logarithm of zero.- ...
Additional arguments passed to
choiceprob_probit
orchoiceprob_logit
.- choice_parameters
[
choice_parameters
|list
]
Achoice_parameters
object or a list as returned byswitch_parameter_space
. When a numeric vector in optimization space is supplied, it is converted viaswitch_parameter_space()
.- logarithm
[
logical(1)
]
Return the log-likelihood? IfFALSE
, the likelihood is returned.- negative
[
logical(1)
]
Return the negative (log-)likelihood? Useful for minimization routines.
Value
choice_likelihood()
returns an object of class choice_likelihood
, which is
a list
containing the design matrices, the choice indices, and the
identifiers. compute_choice_likelihood()
returns a single numeric value with
the (negative) log-likelihood or likelihood, depending on logarithm
and
negative
.
Examples
data(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)
compute_choice_likelihood(
choice_parameters = choice_parameters,
choice_data = likelihood,
choice_effects = choice_effects,
logarithm = TRUE
)
#> [1] -32584.22