gof: Goodness of fit for a fitted choice model

View source: R/gof.R

gofR Documentation

Goodness of fit for a fitted choice model

Description

Computes McFadden's pseudo R-squared (plain and adjusted) and the in-sample hit rate for a fitted model.

Usage

gof(object, null = c("equal_shares", "market_shares"), ...)

## S3 method for class 'choicer_fit'
gof(object, null = c("equal_shares", "market_shares"), ...)

Arguments

object

A fitted model object (choicer_mnl, choicer_mxl, or choicer_nl).

null

Null model for the pseudo R-squared: "equal_shares" (default) or "market_shares".

...

Additional arguments passed to methods.

Details

Two null models are available for the pseudo R-squared R^2 = 1 - LL / LL_0 (adjusted: R^2_{adj} = 1 - (LL - K) / LL_0 with K the number of estimated parameters):

  • "equal_shares" (default): every alternative in individual i's choice set is equally likely, so LL_0 = -\sum_i w_i \log(M_i + 1_{outside}). This is exact for unbalanced choice sets and arbitrary weights.

  • "market_shares": the maximized log-likelihood of an ASC-only model, LL_0 = \sum_j N_j \log(s_j) with N_j the choice counts and s_j the observed market shares (including the outside option when present). This closed form is valid only for balanced choice sets and uniform weights; otherwise an error suggests refitting an ASC-only model.

The hit rate is the weighted share of individuals whose observed choice has the highest predicted probability. When the model includes an outside option, the outside good competes for the predicted maximum (its probability is 1 - \sum_j p_{ij}), and an individual predicted to choose the outside good is a hit when they actually did.

Both the null log-likelihood and the hit rate require the stored estimation data; models fitted with keep_data = FALSE return NA fields with a message.

Value

A choicer_gof object: a list with loglik, loglik_null, null, mcfadden_r2, mcfadden_r2_adj, hit_rate, nobs, and n_params.

Examples


library(data.table)
set.seed(42)
N <- 50; J <- 3
dt <- data.table(id = rep(1:N, each = J), alt = rep(1:J, N))
dt[, `:=`(x1 = rnorm(.N), x2 = rnorm(.N))]
dt[, choice := 0L]
dt[, choice := sample(c(1L, rep(0L, J - 1))), by = id]
fit <- run_mnlogit(dt, "id", "alt", "choice", c("x1", "x2"))
gof(fit)
gof(fit, null = "market_shares")


choicer documentation built on Sept. 5, 2026, 1:07 a.m.