The task of model selection targets the question: If there are several competing models, how do I choose the most appropriate one? This vignette1 outlines the model selection tools implemented in RprobitB.
For illustration, we revisit the probit model of travelers deciding between two fictional train route alternatives from the vignette on model fitting:
form <- choice ~ price + time + change + comfort | 0
data <- prepare_data(form = form, choice_data = train_choice, id = "deciderID", idc = "occasionID")
model_train <- fit_model(
data = data,
scale = "price := -1",
R = 1000, B = 500, Q = 10
)
As a competing model, we consider explaining the choices only by the
alternative’s price, i.e. the probit model with the formula
choice ~ price | 0
:
model_train_sparse <- update(model_train, form = choice ~ price | 0)
The update()
function helps to estimate a new version of
model_train
with new specifications. Here, only
form
has been changed.
The model_selection()
function
RprobitB provides the convenience function
model_selection()
, which takes an arbitrary number of
RprobitB_fit
objects and returns a matrix of model
selection criteria:
model_selection(model_train, model_train_sparse)
#> model_train model_train_sparse
#> npar 4 1
#> LL -1727.75 -1865.86
#> AIC 3463.49 3733.73
#> BIC 3487.42 3739.71
Specifying criteria
is optional. Per default,
criteria = c("npar", "LL", "AIC", "BIC")
.2 The available model
selection criteria are explained in the following.
npar
"npar"
yields the number of model parameters, which is
computed by the npar()
method:
npar(model_train, model_train_sparse)
#> [1] 4 1
Here, model_train
has 4 parameters (a coefficient for
price, time, change, and comfort, respectively), while
model_train_sparse
has only a single price coefficient.
LL
If "LL"
is included in criteria
,
model_selection()
returns the model’s log-likelihood
values. They can also be directly accessed via the logLik()
method:3
AIC
Including "AIC"
yields the Akaike’s Information
Criterion (Akaike
1974), which is computed as
where
is the model’s log-likelihood value,
is the penalty per parameter with
per default for the classical AIC, and
is the number of parameters in the fitted model.
Alternatively, the AIC()
method also returns the AIC
values:
AIC(model_train, model_train_sparse, k = 2)
#> df AIC
#> model_train 4 3463.492
#> model_train_sparse 1 3733.729
The AIC quantifies the trade-off between over- and under-fitting,
where smaller values are preferred. Here, the increase in goodness of
fit justifies the additional 3 parameters of
model_train
.
BIC
Similar to the AIC, "BIC"
yields the Bayesian
Information Criterion (Schwarz 1978), which is defined as
where
is the model’s log-likelihood value,
is the number of data points (which can be accessed via the
nobs()
method), and
is the number of parameters in the fitted model. The
interpretation is analogue to AIC.
RprobitB also provided a method for the
BIC
value:
BIC(model_train, model_train_sparse)
#> df BIC
#> model_train 4 3487.422
#> model_train_sparse 1 3739.712
WAIC
(with se(WAIC)
and
pWAIC
)
WAIC is short for Widely Applicable (or Watanabe-Akaike) Information
Criterion (Watanabe
and Opper 2010). As for AIC and BIC, the smaller the WAIC
value the better the model. Including "WAIC"
in
criteria
yields the WAIC value, its standard error
se(WAIC)
, and the effective number of parameters
pWAIC
, see below.
The WAIC is defined as
where stands for log pointwise predictive density and is a penalty term proportional to the variance in the posterior distribution that is sometimes called effective number of parameters, see McElreath (2020) p. 220 for a reference.
The is approximated as follows. Let be the probability of observation (here the single choices) given the -th set of parameter samples from the posterior. Then
The penalty term is computed as the sum over the variances in log-probability for each observation: The has a standard error of where is the number of choices.
Before computing the WAIC of an object, the probabilities
must be computed via the compute_p_si()
function: 4
model_train <- compute_p_si(model_train)
Afterwards, the WAIC can be accessed as follows:
MMLL
"MMLL"
in criteria
stands for marginal
model log-likelihood. The model’s marginal likelihood
for a model
and data
is required for the computation of Bayes factors, see below. In general,
the term has no closed form and must be approximated numerically.
RprobitB uses the posterior Gibbs samples derived from
the mcmc()
function to approximate the model’s marginal
likelihood via the posterior harmonic mean estimator (Newton and Raftery
1994): Let
denote the number of available posterior samples
.
Then,
By the law of large numbers, almost surely as .
As for the WAIC, computing the MMLL relies on the
probabilities
,
which must first be computed via the compute_p_si()
function. Afterwards, the mml()
function can be called with
an RprobitB_fit
object as input. The function returns the
RprobitB_fit
object, where the marginal likelihood value is
saved as the entry "mml"
and the marginal log-likelihood
value as the attribute "mmll"
:5
There are two options for improving the approximation: As for the WAIC, you can use more posterior samples. Alternatively, you can combine the posterior harmonic mean estimate with the prior arithmetic mean estimator (Hammersley and Handscomb 1964): For this approach, samples are drawn from the model’s prior distribution. Then,
Again, it hols by the law of large numbers, that almost surely as . The final approximation of the model’s marginal likelihood than is a weighted sum of the posterior harmonic mean estimate and the prior arithmetic mean estimate, where the weights are determined by the sample sizes.
To use the prior arithmetic mean estimator, call the
mml()
function with a specification of the number of prior
draws S
and set recompute = TRUE
:6
model_train <- mml(model_train, S = 1000, recompute = TRUE)
Note that the prior arithmetic mean estimator works well if the prior
and posterior distribution have a similar shape and strong overlap, as
Gronau et al. (2017) points out. Otherwise, most of
the sampled prior values result in a likelihood value close to zero,
thereby contributing only marginally to the approximation. In this case,
a very large number S
of prior samples is required.
BF
The Bayes factor is an index of relative posterior model plausibility of one model over another (Marin and Robert 2014). Given data and two models and , it is defined as
The ratio expresses the factor by which a priori is assumed to be the correct model. Per default, RprobitB sets . The front part is the ratio of the marginal model likelihoods. A value of means that the model is more strongly supported by the data under consideration than .
Adding "BF"
to the criteria
argument of
model_selection
yields the Bayes factors. We find a
decisive evidence (Jeffreys 1998) in favor of
model_train
.
model_selection(model_train, model_train_sparse, criteria = c("BF"))
pred_acc
Finally, adding "pred_acc"
to the criteria
argument for the model_selection()
function returns the
share of correctly predicted choices. From the output of
model_selection()
above (or alternatively the one in the
following) we deduce that model_train
correctly predicts
about 6% of the choices more than model_train_sparse
:7
pred_acc(model_train, model_train_sparse)
#> [1] 0.6951178 0.6340048