% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/methods.R
\name{logLik.lgspline}
\alias{logLik.lgspline}
\title{Extract Log-Likelihood from a Fitted lgspline}
\usage{
\method{logLik}{lgspline}(object, include_prior = TRUE, new_weights = NULL, ...)
}
\arguments{
\item{object}{A fitted lgspline model object.}

\item{include_prior}{Logical; add the log-prior penalty term. Default TRUE.}

\item{new_weights}{Numeric scalar or N-vector; optional observation weights
overriding \code{object$weights}.}

\item{...}{Not used.}
}
\value{
A \code{"logLik"} object with attributes \code{df} (effective
  degrees of freedom) and \code{nobs} (number of observations).
}
\description{
Returns the log-likelihood as a \code{"logLik"} object for use with
\code{\link[stats]{AIC}}, \code{\link[stats]{BIC}}, and other model
comparison tools.
}
\details{
\strong{Gaussian identity, no correlation.}
\deqn{
  \ell = -\frac{N}{2}\log(2\pi\tilde{\sigma}^2) -
  \frac{1}{2\tilde{\sigma}^2}\sum_{i}(y_i - \hat{y}_i)^2
}

\strong{Gaussian identity, with correlation.}
GLS log-likelihood:
\deqn{
  \ell = -\frac{N}{2}\log(2\pi\tilde{\sigma}^2)
  + \log|\mathbf{V}^{-1/2}|
  - \frac{1}{2\tilde{\sigma}^2}
    \|\mathbf{V}^{-1/2}(\mathbf{y} - \hat{\mathbf{y}})\|^2
}
\eqn{\log|\mathbf{V}^{-1/2}|} is obtained from \code{VhalfInv_logdet}
when available, or computed directly from \code{VhalfInv}.

\strong{Prior contribution.}
When \code{include_prior = TRUE} (default), the log-prior
\deqn{
  -\frac{1}{2\tilde{\sigma}^2}
  \sum_{k}\boldsymbol{\beta}_k^\top\boldsymbol{\Lambda}_k
  \boldsymbol{\beta}_k
}
is added, giving the penalised MAP log-likelihood coherent with the
smoothing spline objective. Set \code{include_prior = FALSE} for the
unpenalised marginal likelihood, which is more appropriate when comparing
models with different penalty structures or numbers of knots.

\strong{Other GLM families.}
Uses \code{family$aic()} when available. For correlated models the
whitened residuals and fitted values are passed. When \code{family$aic()}
is unavailable, a deviance-based approximation is used (valid for
relative comparisons; a warning is emitted).

This function returns the marginal (full) GLS log-likelihood, not the
REML log-likelihood. This is consistent with \code{REML = FALSE} in
\code{lme} and \code{gls}, and is the conventional choice for AIC/BIC
comparisons of fixed-effects structure.

The \code{df} attribute is set to \eqn{N - \mathrm{trace}(\mathbf{XUGX}^\top)}.
}
\examples{

set.seed(1234)
t <- runif(1000, -10, 10)
y <- 2*sin(t) + -0.06*t^2 + rnorm(length(t))
model_fit <- lgspline(t, y)

logLik(model_fit)
logLik(model_fit, include_prior = FALSE)

AIC(model_fit)
BIC(model_fit)

## Compare models with different K using unpenalized likelihood
fit_k3 <- lgspline(t, y, K = 3)
fit_k7 <- lgspline(t, y, K = 7)
AIC(fit_k3, fit_k7)

}
\seealso{
\code{\link{lgspline}}, \code{\link{prior_loglik}},
\code{\link[stats]{logLik}}, \code{\link[stats]{AIC}},
\code{\link[stats]{BIC}}
}
