logLik.lgspline: Extract Log-Likelihood from a Fitted lgspline

View source: R/methods.R

logLik.lgsplineR Documentation

Extract Log-Likelihood from a Fitted lgspline

Description

Returns the log-likelihood as a "logLik" object for use with AIC, BIC, and other model comparison tools.

Usage

## S3 method for class 'lgspline'
logLik(
  object,
  include_prior = TRUE,
  new_weights = NULL,
  B_predict = NULL,
  sigmasq_predict = NULL,
  ...
)

Arguments

object

A fitted lgspline model object.

include_prior

Logical; add the log-prior penalty term. Default TRUE.

new_weights

Numeric scalar or N-vector; optional observation weights overriding object$weights.

B_predict

List; optional coefficient list at which to evaluate the likelihood. Default NULL uses object$B.

sigmasq_predict

Numeric scalar; optional dispersion at which to evaluate the likelihood. Default NULL uses object$sigmasq_tilde.

...

Not used.

Details

Gaussian identity, no correlation.

\ell = -\frac{N}{2}\log(2\pi\tilde{\sigma}^2) - \frac{1}{2\tilde{\sigma}^2}\sum_{i}w_i(y_i - \hat{y}_i)^2 + \frac{1}{2}\sum_i\log w_i

Gaussian identity, with correlation. GLS log-likelihood:

\ell = -\frac{N}{2}\log(2\pi\tilde{\sigma}^2) + \log|\mathbf{V}^{-1/2}| - \frac{1}{2\tilde{\sigma}^2} \sum_i w_i[\mathbf{V}^{-1/2}(\mathbf{y} - \hat{\mathbf{y}})]_i^2 + \frac{1}{2}\sum_i\log w_i

\log|\mathbf{V}^{-1/2}| is obtained from VhalfInv_logdet when available, or computed directly from VhalfInv.

Prior contribution. When include_prior = TRUE (default), the log-prior

-\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 include_prior = FALSE for the unpenalised marginal likelihood, which is more appropriate when comparing models with different penalty structures or numbers of knots.

Other GLM families. Uses family$aic() when available. For correlated non-Gaussian models, the family contribution is evaluated on the raw response scale and the correlation log-determinant correction is added when available. When 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 REML = FALSE in lme and gls, and is the conventional choice for AIC/BIC comparisons of fixed-effects structure.

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

Value

A "logLik" object with attributes df (effective degrees of freedom) and nobs (number of observations).

See Also

lgspline, prior_loglik, logLik, AIC, BIC

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)
logLik(model_fit, B_predict = model_fit$B,
       sigmasq_predict = model_fit$sigmasq_tilde)

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)


lgspline documentation built on Aug. 5, 2026, 1:10 a.m.