Skip to contents

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 by choice_data and choice_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 of choice_parameters. It can either take the original choice objects or a pre-computed choice_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]
A choice_data object with the observed choices.

choice_effects

[choice_effects]
A choice_effects object that determines the model effects.

choice_identifiers

[choice_identifiers]
A choice_identifiers object. The default extracts identifiers from choice_data.

input_checks

[logical(1)]
Forwarded to choiceprob_probit or choiceprob_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 or choiceprob_logit.

choice_parameters

[choice_parameters | list]
A choice_parameters object or a list as returned by switch_parameter_space. When a numeric vector in optimization space is supplied, it is converted via switch_parameter_space().

logarithm

[logical(1)]
Return the log-likelihood? If FALSE, 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