Learn R Programming

kardl (version 0.1.1)

modelCriterion: Model Selection Criterion

Description

Computes a model selection criterion (AIC, BIC, AICc, or HQ) or applies a user-defined function to evaluate a statistical model.

Usage

modelCriterion(cr, model, ...)

Value

A numeric value representing the selected criterion, normalized by the sample size if one of the predefined options is used.

Arguments

cr

A character string specifying the criterion to compute. Options are "AIC", "BIC", "AICc", and "HQ". Alternatively, a user-defined function can be provided.

model

An object containing the fitted model. The object should include at least:

  • model$model – the actual fitted model object (e.g., from lm, glm).

  • k – the number of estimated parameters.

  • n – the sample size.

...

Additional arguments passed to the user-defined criterion function if cr is a function.

Details

This function returns model selection criteria used to compare the quality of different models. All criteria are defined such that lower values indicate better models (i.e., the goal is minimization).

If you wish to compare models using a maximization approach (e.g., log-likelihood), you can multiply the result by -1.

Note: The predefined string options (e.g., "AIC") are not the same as the built-in R functions AIC() or BIC(). In particular, the values returned by this function are adjusted by dividing by the sample size n (i.e., normalized AIC/BIC), which makes it more comparable across datasets of different sizes.

The function returns:

  • "AIC": \( \frac{2k - 2\ell}{n} \) Akaike Information Criterion divided by n.

  • "BIC": \( \frac{\log(n) \cdot k - 2\ell}{n} \) Bayesian Information Criterion divided by n.

  • "AICc": \( \frac{2k(k+1)}{n - k - 1} + \frac{2k - 2\ell}{n} \) Corrected Akaike Information Criterion divided by n.

  • "HQ": \( \frac{2 \log(\log(n)) \cdot k - 2\ell}{n} \) Hannan–Quinn Criterion divided by n.

where:

  • \(k\) is the number of parameters,

  • \(n\) is the sample size,

  • \(\ell\) is the log-likelihood of the model.

If cr is a function, it is called with the fitted model and any additional arguments passed through ....

See Also

kardl

Examples

Run this code
model <- list(model = lm(mpg ~ wt + hp, data = mtcars), k = 3, n = nrow(mtcars))
modelCriterion(AIC, model)
#
modelCriterion("AIC", model)
modelCriterion("BIC", model)

# Using a custom criterion function
my_cr_fun <- function(mod, ...) { AIC(mod) / length(mod$model[[1]]) }
modelCriterion(my_cr_fun, model)

Run the code above in your browser using DataLab