Skip to contents

Estimates a multinomial logit model via maximum likelihood.

Usage

run_mnlogit(
  data = NULL,
  id_col = NULL,
  alt_col = NULL,
  choice_col = NULL,
  covariate_cols = NULL,
  input_data = NULL,
  optimizer = NULL,
  control = list(),
  weights = NULL,
  weights_col = NULL,
  outside_opt_label = NULL,
  include_outside_option = FALSE,
  use_asc = TRUE,
  keep_data = TRUE,
  scale_vars = c("bhhh", "none", "sd", "mad", "iqr"),
  se_method = c("hessian", "bhhh", "sandwich", "cluster"),
  cluster_col = NULL,
  nloptr_opts = NULL,
  polish = c("none", "newton", "bhhh"),
  polish_control = list()
)

Arguments

data

Data frame containing choice data (convenience workflow). Mutually exclusive with input_data.

id_col

Name of the column identifying choice situations (individuals).

alt_col

Name of the column identifying alternatives.

choice_col

Name of the column indicating chosen alternative (1 = chosen, 0 = not chosen).

covariate_cols

Vector of names of columns to be used as covariates. With use_asc = TRUE, none may be named like a constant, ASC_<label>.

input_data

List output from prepare_mnl_data (advanced workflow). Mutually exclusive with data. Its weights must be finite and strictly positive and its X finite, which prepare_mnl_data() ensures; the fit checks them again, in case the object was edited, before it first evaluates the likelihood (integer64 weights are converted to double).

optimizer

Optimizer to use: "nloptr" (default), "optim", or a custom function with signature f(theta_init, eval_f, lower, upper, control) where eval_f(theta) returns list(objective, gradient). Must return a list with par/value (or solution/objective). If the custom function accepts control or ..., the control argument is forwarded; otherwise it is silently ignored. With scale_vars other than "none", it receives theta_init, lower, upper and eval_f in the scaled coordinates, and its par is mapped back.

control

List of optimizer-specific control parameters passed to the chosen optimizer (e.g., list(maxeval = 2000) for nloptr). With scale_vars other than "none", settings in parameter units apply to the scaled coordinates.

weights

Optional vector of weights for each choice situation. If NULL, equal weights are used. All weights must be finite and strictly positive.

weights_col

Optional name of a column in data holding per-row weights (convenience workflow only). The column must be constant within each id_col (one weight per choice situation) and is collapsed accordingly. Mutually exclusive with weights. All weights must be finite and strictly positive. Used for choice-based / WESML weighting; pair with se_method = "sandwich" for valid inference.

outside_opt_label

The label of the outside option or of the base alternative, given in the class of the alternative column. With include_outside_option = TRUE it labels the outside option, which the model adds: rows of the data that carry it are dropped, and it names the outside option's row of alt_mapping. Without the outside option it names the base alternative, coded first (with constants, its constant is normalized to 0); it must be one of the alternatives, and NULL (the default) makes the first alternative in sort order the base.

include_outside_option

Logical indicating whether to include an outside option in the model.

use_asc

Logical indicating whether to include alternative-specific constants (ASCs) in the model. An alternative's constant has no finite maximum-likelihood estimate when the alternative is never chosen in the data, or is chosen in every choice situation that offers it (the likelihood keeps rising as the constant falls, or grows); when the outside option or the base alternative is never chosen, or chosen wherever it is offered, the other constants have none together. The fit then warns (class choicer_unbounded_constants) before the optimizer runs, naming them. It counts rows (alt_mapping), so an alternative listed twice in a choice situation can hide being chosen wherever it is offered.

keep_data

Logical. If TRUE (default), stores prepared data in the returned object for predict() and post-estimation functions.

scale_vars

How the optimizer's coordinates are scaled. The choice does not change the estimator; it changes how fast, and whether, the optimizer reaches the maximum. "bhhh" (default) scales each parameter by the inverse square root of the diagonal of the BHHH (outer product of gradients) matrix at the start values (all zeros), rounded to a power of two, from one extra gradient pass, so that the BHHH diagonal, an estimate of the curvature, is near one at the start values in every coordinate, the constants' included (theirs follow the alternatives' shares). Where the diagonal is unusable (no finite, positive entry), the parameters are left unscaled, with a message. "none" hands the optimizer the parameters as they are (the default in choicer 0.2.x). With "sd" (sample standard deviation), "mad" (stats::mad) or "iqr" (stats::IQR(x) / 1.349), the optimizer works on the coefficients X would have with every column divided by the chosen scale (each coefficient times its column's scale); the constants keep their scale, the data are not divided, and the fit holds no scaled copy of them. With any choice but "none" (so by default), the optimizer, a custom one included, works in the scaled coordinates, so settings in parameter units in control (nloptr's xtol_abs, optim's parscale) apply to the scaled coordinates; "none" keeps the parameters' units. Coefficients are reported in natural units and the standard errors computed in them, from the information matrix equilibrated before it is inverted, whatever the choice, so, where the likelihood is well identified and every choice reaches its maximum, reported quantities do not depend on this choice beyond the optimizer's tolerance.

se_method

Method for computing standard errors: "hessian" (default, analytical Hessian), "bhhh" (outer product of gradients), "sandwich" (robust Huber–White / WESML variance \(A^{-1} B A^{-1}\)), or "cluster" (cluster-robust sandwich; requires cluster_col or a prepared input_data with a cluster field). Use "sandwich" under choice-based / WESML weighting. Any of these can also be recomputed post hoc via vcov(fit, type = ).

cluster_col

Optional name of a column in data holding cluster labels for cluster-robust standard errors (e.g. a person id when the same decision maker contributes several choice situations). Must be constant within each id_col. Supplying cluster_col without an explicit se_method selects se_method = "cluster".

nloptr_opts

Deprecated. Use optimizer and control instead.

polish

A Newton phase after the optimizer (see the section below): "none" (default) has none; "newton" steps on the analytic Hessian of the log-likelihood, "bhhh" on the BHHH (outer product of the gradients) matrix, both with the fit's weights.

polish_control

List of settings of the Newton phase, any of max_iter (20; 0 evaluates the decrement at the optimizer's solution without stepping), tol_decrement (\(10^{-8}\), in log-likelihood units of weights with mean one: the decrement is taken over the mean weight, so that the rule does not depend on the weights' scale) and tol_step (\(10^{-12}\): a step shorter than this, relative to the parameters' magnitudes plus their scales, stops the polish unconverged).

Value

A choicer_mnl object (inherits from choicer_fit). Standard S3 methods available: summary(), coef(), vcov(), logLik(), AIC(), BIC(), nobs(), predict(). The variance is computed once the optimizer has stopped. If that step fails with an error (an allocation in R, say), the fit returns its estimates without it (vcov NULL, NA standard errors, the error's message in variance_error) and warns, naming the vcov(fit, type = ) that computes the variance later from a fit that keeps its data (keep_data = TRUE). The compiled Hessian, BHHH and cluster-meat routines check their memory before allocating (see set_num_threads); only small per-situation buffers are allocated inside their parallel loops, where an allocation that fails still terminates R. With polish, fit$optimizer$polish records the Newton phase, as run_mxlogit's does: method, converged, iterations and damped, decrement (over the mean weight; NA where the curvature on the parameters moved was not positive definite, or none was evaluated) and lambda, message, evaluations, active, held, flat, improvement, error, elapsed_time (which fit$optimizer$elapsed_time includes) and optimizer, the optimizer's own convergence, message, iterations and elapsed_time. A converged polish sets convergence to the optimizer's success code (1 for nloptr, 0 for optim and a custom optimizer) and leads message with its verdict; otherwise both keep the optimizer's, the polish's reason appended to the message. A polish that stops with an error (the Hessian's memory check, say) keeps its last accepted point and warns (class choicer_polish_failed). An optimizer that reports a failure (nloptr's negative codes, such as NLOPT_FAILURE where L-BFGS's line search gives up, and optim's 10, 51 and 52) may have returned its start values or stopped at the maximum, which its code cannot tell. Without a polish, a Newton phase of no steps then checks its point: fit$optimizer$polish records it (iterations 0), and a point it certifies takes the success code as above. A point no polish certified keeps the failure code and warns (class choicer_optimizer_failed). So too an optimizer stopped at its cap, whoever set it (nloptr's 5, maxeval, and 6, maxtime; optim's 1, maxit; no custom optimizer): the same phase checks its point, one pass of the Hessian that the variance reuses except under se_method = "bhhh", and a point nothing certified keeps its code and warns (class choicer_optimizer_capped), giving the Newton decrement where the Hessian is positive definite, roughly the log-likelihood still to gain under weights of mean one. suppressWarnings(classes = "choicer_optimizer_capped") silences a deliberate short run.

Details

Two workflows are supported:

Convenience (default)

Supply data and column names. Data preparation (prepare_mnl_data) is handled automatically.

Advanced

Call prepare_mnl_data yourself and pass the result via input_data.

Newton phase (polish)

The polish does not change the estimator. From wherever the optimizer stopped, it takes Newton steps on the curvature of the log-likelihood and certifies the maximum: it converges when the Newton decrement \(\nu = \frac{1}{2} g^\top C^{-1} g\), with \(g\) the gradient and \(C\) the curvature on the parameters it moves, over the mean weight, falls below tol_decrement. On a positive definite Hessian (of the weights scaled to mean one) every linear combination of the estimates then lies within \(\sqrt{2\nu}\) of its standard error of the maximum of the log-likelihood's quadratic model (\(1.4 \times 10^{-4}\) at the default). "bhhh" is cheaper per step but converges linearly, its decrement on its own shows only a stationary point, and it needs at least as many choice situations as the parameters it moves. A trial point at which the likelihood kernel replaces a value (a log probability clamped to \(-10^{10}\), which it warns of, a total or gradient entry that is not finite) is rejected.

The multinomial logit's log-likelihood is concave, so the maximum the polish certifies is the global one wherever the model is identified (a positive definite Hessian). In a study on census- and claims-style designs (about 2,500 and 500 alternatives, 17,000 to 245,000 choice situations) the default fit stopped at the maximum and polish = "newton" certified it at once, its Hessian then serving the variance; with scale_vars = "none" the optimizer stopped up to 9 log-likelihood units short (at its 1,000-evaluation limit on three of the four designs), a gap the polish closed in 1 to 6 steps.

The constants of inside alternatives without a finite estimate (see use_asc) are held at the optimizer's values, and the certificate concerns the other parameters given them. Where the base alternative or the outside option is the one without, the other constants diverge together and the polish does not report convergence, unless the reference is never chosen and the held constants (of alternatives chosen wherever offered) anchor all the others, which stops their rise: each other constant's alternative is offered in a choice situation that chooses a held alternative, or one anchored so. Under scale_vars = "none", whose coordinates are the parameters' own units, the steps' scales come from one BHHH pass at the optimizer's solution (one gradient evaluation). The polish's last curvature serves the variance where se_method needs it, unless the polish fell back to the optimizer's solution, its curvature failed, or its start could not be evaluated: after "newton", the Hessian of "hessian" and the bread of "sandwich" and "cluster"; after "bhhh", the BHHH matrix of "bhhh". run_mxlogit describes the steps, the damping, the bounds and the line search.

Column names

No column the model uses (the id, alternative, choice, covariate, weight, cluster, nest or decision-maker column) may have a name starting with .choicer_, which choicer reserves for its working columns, and the alternative column may not be named alt_int, N_OBS, N_CHOICES, TAKE_RATE or MKT_SHARE, the names of the other columns of alt_mapping. Either is an error that names the column. Columns the model does not use may have any name.

Missing and non-finite values

A choice situation with a missing value (NA or NaN) in a covariate is dropped, with a warning that counts the situations dropped, and so is one with an infinite value (Inf or -Inf), with a warning that also names the covariates that hold such values. A preparation that would drop every choice situation stops instead. Infinite values usually come from a transformation such as log(0); dropping their situations selects the sample on that covariate, so they are often better fixed before the data are prepared. Prepared data passed to a fitting function as input_data are not filtered again: such values in them are an error.

Examples

# \donttest{
library(data.table)
set.seed(42)
N <- 100; J <- 3; beta_true <- c(1.0, -0.5)
dt <- data.table(id = rep(1:N, each = J), alt = rep(1:J, N))
dt[, `:=`(x1 = rnorm(.N), x2 = rnorm(.N))]
#>         id   alt          x1           x2
#>      <int> <int>       <num>        <num>
#>   1:     1     1  1.37095845 -0.004620768
#>   2:     1     2 -0.56469817  0.760242168
#>   3:     1     3  0.36312841  0.038990913
#>   4:     2     1  0.63286260  0.735072142
#>   5:     2     2  0.40426832 -0.146472627
#>  ---                                     
#> 296:    99     2 -0.47733551  0.160327395
#> 297:    99     3 -0.16626149 -0.433641942
#> 298:   100     1  0.86256338  1.537412419
#> 299:   100     2  0.09734049 -2.170246577
#> 300:   100     3 -1.62561674  1.027004619
dt[, V := drop(as.matrix(.SD) %*% beta_true), .SDcols = c("x1","x2")]
#>         id   alt          x1           x2           V
#>      <int> <int>       <num>        <num>       <num>
#>   1:     1     1  1.37095845 -0.004620768  1.37326883
#>   2:     1     2 -0.56469817  0.760242168 -0.94481926
#>   3:     1     3  0.36312841  0.038990913  0.34363295
#>   4:     2     1  0.63286260  0.735072142  0.26532653
#>   5:     2     2  0.40426832 -0.146472627  0.47750464
#>  ---                                                 
#> 296:    99     2 -0.47733551  0.160327395 -0.55749920
#> 297:    99     3 -0.16626149 -0.433641942  0.05055948
#> 298:   100     1  0.86256338  1.537412419  0.09385717
#> 299:   100     2  0.09734049 -2.170246577  1.18246377
#> 300:   100     3 -1.62561674  1.027004619 -2.13911905
dt[, prob := exp(V) / sum(exp(V)), by = id]
#>         id   alt          x1           x2           V       prob
#>      <int> <int>       <num>        <num>       <num>      <num>
#>   1:     1     1  1.37095845 -0.004620768  1.37326883 0.68700257
#>   2:     1     2 -0.56469817  0.760242168 -0.94481926 0.06764341
#>   3:     1     3  0.36312841  0.038990913  0.34363295 0.24535402
#>   4:     2     1  0.63286260  0.735072142  0.26532653 0.33940231
#>   5:     2     2  0.40426832 -0.146472627  0.47750464 0.41962617
#>  ---                                                            
#> 296:    99     2 -0.47733551  0.160327395 -0.55749920 0.21814327
#> 297:    99     3 -0.16626149 -0.433641942  0.05055948 0.40069908
#> 298:   100     1  0.86256338  1.537412419  0.09385717 0.24525785
#> 299:   100     2  0.09734049 -2.170246577  1.18246377 0.72844833
#> 300:   100     3 -1.62561674  1.027004619 -2.13911905 0.02629382
dt[, choice := as.integer(alt == sample(alt, 1, prob = prob)), by = id]
#>         id   alt          x1           x2           V       prob choice
#>      <int> <int>       <num>        <num>       <num>      <num>  <int>
#>   1:     1     1  1.37095845 -0.004620768  1.37326883 0.68700257      1
#>   2:     1     2 -0.56469817  0.760242168 -0.94481926 0.06764341      0
#>   3:     1     3  0.36312841  0.038990913  0.34363295 0.24535402      0
#>   4:     2     1  0.63286260  0.735072142  0.26532653 0.33940231      1
#>   5:     2     2  0.40426832 -0.146472627  0.47750464 0.41962617      0
#>  ---                                                                   
#> 296:    99     2 -0.47733551  0.160327395 -0.55749920 0.21814327      0
#> 297:    99     3 -0.16626149 -0.433641942  0.05055948 0.40069908      0
#> 298:   100     1  0.86256338  1.537412419  0.09385717 0.24525785      0
#> 299:   100     2  0.09734049 -2.170246577  1.18246377 0.72844833      1
#> 300:   100     3 -1.62561674  1.027004619 -2.13911905 0.02629382      0

fit <- run_mnlogit(dt, "id", "alt", "choice", c("x1", "x2"))
#> Optimization run time 0h:0m:0s
summary(fit)
#> Multinomial Logit (MNL) model
#> 
#> Parameter    Estimate  Std.Error  z-value  Pr(>|z|)  
#> x1           1.091325   0.192975   5.6553  1.56e-08  ***
#> x2          -0.511225   0.152893  -3.3437  8.27e-04  ***
#> ASC_2       -0.061376   0.288667  -0.2126  8.32e-01  
#> ASC_3       -0.074843   0.289651  -0.2584  7.96e-01  
#> ---
#> Signif. codes:  '***' 0.001 '**' 0.01 '*' 0.05
#> 
#> Std. Errors: Analytical Hessian 
#> Log-likelihood: -81.2408 
#> AIC: 170.482  | BIC: 180.902 
#> McFadden R2: 0.261 (adj: 0.224) | Hit rate: 0.590 
#> N: 100  | Parameters: 4 
#> Optimization time: 0 s
#> Convergence: 1 ( NLOPT_SUCCESS: Generic success return value. )
coef(fit)
#>          x1          x2       ASC_2       ASC_3 
#>  1.09132487 -0.51122457 -0.06137617 -0.07484335 
AIC(fit)
#> [1] 170.4816
predict(fit, type = "shares")
#>      [,1]
#> [1,] 0.34
#> [2,] 0.32
#> [3,] 0.34
# }