\HeaderA{logLik}{Extract Log-Likelihood}{logLik}
\methaliasA{logLik.lm}{logLik}{logLik.lm}
\aliasA{print.logLik}{logLik}{print.logLik}
\aliasA{str.logLik}{logLik}{str.logLik}
\keyword{models}{logLik}
\begin{Description}\relax
This function is generic; method functions can be written to handle
specific classes of objects.  Classes which already have methods for
this function include: \code{glm}, \code{lm}, \code{nls}, \code{Arima}
and \code{gls}, \code{lme} and others in package \pkg{nlme}.
\end{Description}
\begin{Usage}
\begin{verbatim}
logLik(object, ...)

## S3 method for class 'lm':
logLik(object, REML = FALSE, ...)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{object}] any object from which a log-likelihood value, or a
contribution to a log-likelihood value, can be extracted.
\item[\code{...}] some methods for this generic function require additional
arguments.
\item[\code{REML}] an optional logical value.  If \code{TRUE} the restricted
log-likelihood is returned, else, if \code{FALSE}, the
log-likelihood is returned.  Defaults to \code{FALSE}.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
For a \code{"glm"} fit the \code{\LinkA{family}{family}} does not have to specify
how to calculate the log-likelihood, so this is based on using the
family's \code{aic()} function to compute the AIC.  For the
\code{\LinkA{gaussian}{gaussian}}, \code{\LinkA{Gamma}{Gamma}} and
\code{\LinkA{inverse.gaussian}{inverse.gaussian}} families it assumed that the dispersion
of the GLM is estimated has been counted as a parameter in the AIC
value, and for all other families it is assumed that the dispersion is
known.

Note that this procedure does not give the maximized likelihood for
\code{"glm"} fits from the Gamma and inverse gaussian families, as the
estimate of dispersion used is not the MLE.

For \code{"lm"} fits it is assumed that the scale has been estimated
(by maximum likelihood or REML), and all the constants in the
log-likelihood are included.
\end{Details}
\begin{Value}
Returns an object, say \code{r}, of class \code{logLik} which is a
number with attributes, \code{attr(r, "df")} (\bold{d}egrees of
\bold{f}reedom) giving the number of (estimated) parameters in the model.
There is a simple \code{print} method for \code{logLik} objects.

The details depend on the method function used; see the appropriate
documentation.
\end{Value}
\begin{Author}\relax
Jose Pinheiro and Douglas Bates
\end{Author}
\begin{References}\relax
For \code{logLik.lm}:

Harville, D.A. (1974).
Bayesian inference for variance components using only error contrasts.
\emph{Biometrika}, \bold{61}, 383--385.
\end{References}
\begin{SeeAlso}\relax
\code{\LinkA{logLik.gls}{logLik.gls}}, \code{\LinkA{logLik.lme}{logLik.lme}}, in
package \pkg{nlme}, etc.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
x <- 1:5
lmx <- lm(x ~ 1)
logLik(lmx) # using print.logLik() method
utils::str(logLik(lmx))

## lm method
(fm1 <- lm(rating ~ ., data = attitude))
logLik(fm1)
logLik(fm1, REML = TRUE)

res <- try(utils::data(Orthodont, package="nlme"))
if(!inherits(res, "try-error")) {
  fm1 <- lm(distance ~ Sex * age, Orthodont)
  print(logLik(fm1))
  print(logLik(fm1, REML = TRUE))
}
\end{ExampleCode}
\end{Examples}

