logLik
Extract LogLikelihood
This function is generic; method functions can be written to handle
specific classes of objects. Classes which have methods for this
function include: "glm"
, "lm"
, "nls"
and
"Arima"
. Packages contain methods for other classes, such as
"fitdistr"
, "negbin"
and "polr"
in package
\href{https://CRAN.Rproject.org/package=#1}{\pkg{#1}}MASSMASS, "multinom"
in package \href{https://CRAN.Rproject.org/package=#1}{\pkg{#1}}nnetnnet and
"gls"
, "gnls"
"lme"
and others in package
\href{https://CRAN.Rproject.org/package=#1}{\pkg{#1}}nlmenlme.
 Keywords
 models
Usage
logLik(object, ...)
"logLik"(object, REML = FALSE, ...)
Arguments
 object
 any object from which a loglikelihood value, or a contribution to a loglikelihood value, can be extracted.
 ...
 some methods for this generic function require additional arguments.
 REML
 an optional logical value. If
TRUE
the restricted loglikelihood is returned, else, ifFALSE
, the loglikelihood is returned. Defaults toFALSE
.
Details
logLik
is most commonly used for a model fitted by maximum
likelihood, and some uses, e.g.\ifelse{latex}{\out{~}}{ } by AIC
, assume
this. So care is needed where other fit criteria have been used, for
example REML (the default for "lme"
).
For a "glm"
fit the family
does not have to
specify how to calculate the loglikelihood, so this is based on using
the family's aic()
function to compute the AIC. For the
gaussian
, Gamma
and
inverse.gaussian
families it assumed that the dispersion
of the GLM is estimated and 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 "glm"
fits from the Gamma and inverse gaussian
families, as the estimate of dispersion used is not the MLE.
For "lm"
fits it is assumed that the scale has been estimated
(by maximum likelihood or REML), and all the constants in the
loglikelihood are included. That method is only applicable to
singleresponse fits.
Value

Returns an object of class
logLik
. This is a number with at
least one attribute, "df"
(degrees of freedom),
giving the number of (estimated) parameters in the model.There is a simple print
method for "logLik"
objects.There may be other attributes depending on the method used: see the
appropriate documentation. One that is used by several methods is
"nobs"
, the number of observations used in estimation (after
the restrictions if REML = TRUE
).
References
For logLik.lm
:
Harville, D.A. (1974). Bayesian inference for variance components using only error contrasts. Biometrika, 61, 383385.
See Also
logLik.gls
, logLik.lme
, in
package \href{https://CRAN.Rproject.org/package=#1}{\pkg{#1}}nlmenlme, etc.
Examples
library(stats)
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)
utils::data(Orthodont, package = "nlme")
fm1 < lm(distance ~ Sex * age, Orthodont)
logLik(fm1)
logLik(fm1, REML = TRUE)