\HeaderA{extractAIC}{Extract AIC from a Fitted Model}{extractAIC}
\keyword{models}{extractAIC}
\begin{Description}\relax
Computes the (generalized) Akaike \bold{A}n \bold{I}nformation
\bold{C}riterion for a fitted parametric model.
\end{Description}
\begin{Usage}
\begin{verbatim}
extractAIC(fit, scale, k = 2, ...)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{fit}] fitted model, usually the result of a fitter like
\code{\LinkA{lm}{lm}}.
\item[\code{scale}] optional numeric specifying the scale parameter of the
model, see \code{scale} in \code{\LinkA{step}{step}}.  Currently only used
in the \code{"lm"} method, where \code{scale} specifies the estimate
of the error variance, and \code{scale = 0} indicates that it is to
be estimated by maximum likelihood.

\item[\code{k}] numeric specifying the \sQuote{weight} of the
\emph{equivalent degrees of freedom} (\eqn{\equiv}{=:} \code{edf})
part in the AIC formula.
\item[\code{...}] further arguments (currently unused in base \R).
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
This is a generic function, with methods in base \R{} for \code{"aov"},
\code{"coxph"}, \code{"glm"}, \code{"lm"}, \code{"negbin"}
and \code{"survreg"} classes.

The criterion used is
\deqn{AIC = - 2\log L +  k \times \mbox{edf},}{AIC = - 2*log L +  k * edf,}
where \eqn{L}{} is the likelihood and \code{edf} the equivalent degrees
of freedom (i.e., the number of free parameters for usual parametric
models) of \code{fit}.

For linear models with unknown scale (i.e., for \code{\LinkA{lm}{lm}} and
\code{\LinkA{aov}{aov}}), \eqn{-2\log L}{} is computed from the
\emph{deviance} and uses a different additive constant to
\code{\LinkA{logLik}{logLik}} and hence \code{\LinkA{AIC}{AIC}}.  If \eqn{RSS}{}
denotes the (weighted) residual sum of squares then \code{extractAIC}
uses for \eqn{- 2\log L}{} the formulae \eqn{RSS/s - n}{} (corresponding
to Mallows' \eqn{C_p}{Cp}) in the case of known scale \eqn{s}{} and
\eqn{n \log (RSS/n)}{} for unknown scale. \code{\LinkA{AIC}{AIC}} only handles
unknown scale and uses the formula
\eqn{n \log (RSS/n) - n + n \log 2\pi - \sum \log w}{}
where \eqn{w}{} are the weights.

For \code{glm} fits the family's \code{aic()} function to compute the
AIC: see the note under \code{logLik} about the assumptions this makes.

\code{k = 2} corresponds to the traditional AIC, using \code{k =
    log(n)} provides the BIC (Bayesian IC) instead.
\end{Details}
\begin{Value}
A numeric vector of length 2, giving

\begin{ldescription}
\item[\code{edf}] the \sQuote{\bold{e}quivalent \bold{d}egrees of \bold{f}reedom}
for the fitted model \code{fit}.
\item[\code{AIC}] the (generalized) Akaike Information Criterion for \code{fit}.
\end{ldescription}
\end{Value}
\begin{Note}\relax
This function is used in \code{\LinkA{add1}{add1}}, \code{\LinkA{drop1}{drop1}}
and \code{\LinkA{step}{step}} and similar functions in package \pkg{MASS}
from which it was adopted.
\end{Note}
\begin{Author}\relax
B. D. Ripley
\end{Author}
\begin{References}\relax
Venables, W. N. and Ripley, B. D. (2002)
\emph{Modern Applied Statistics with S.}
New York: Springer (4th ed).
\end{References}
\begin{SeeAlso}\relax
\code{\LinkA{AIC}{AIC}}, \code{\LinkA{deviance}{deviance}}, \code{\LinkA{add1}{add1}},
\code{\LinkA{step}{step}}
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
utils::example(glm)
extractAIC(glm.D93)#>>  5  15.129
\end{ExampleCode}
\end{Examples}

