leave_one_out: Compute Leave-One-Out Cross-Validated Predictions for...

View source: R/leave_one_out.R

leave_one_outR Documentation

Compute Leave-One-Out Cross-Validated Predictions for Gaussian Response/Identity Link under Constraint

Description

Computes the leave-one-out cross-validated predictions from a model fit, assuming Gaussian-distributed response with identity link.

The LOO closed-formula for observation i is \hat{y}_{(-i)} = y_i - \frac{1}{1 - H_{ii}}(y_i - \hat{y}_i) where \mathbf{H} is the effective hat matrix under smoothing constraints, adjusted for weights and correlation structure if present.

Observations with leverage at or above leverage_threshold are flagged in a warning, since extreme hat values can make the shortcut numerically unreliable. The default leverage_threshold = 100 is intentionally permissive, so users who want diagnostic warnings for large H_{ii} should set a smaller threshold explicitly.

For related discussion of prediction-sum-of-squares calculations under linear restrictions, see Tarpey (2000), who studies the PRESS statistic for restricted least squares. That setting is closely related to the constraint-adjusted hat-matrix shortcut used here.

Usage

leave_one_out(model_fit, leverage_threshold = 100)

Arguments

model_fit

A fitted lgspline model object.

leverage_threshold

Numeric scalar. Observations with H_{ii} \geq leverage_threshold are treated as high-leverage for the warning below. Default 100.

Value

A vector of leave-one-out cross-validated predictions

References

Tarpey, T. (2000). A note on the prediction sum of squares statistic for restricted least squares. The American Statistician, 54(2), 116–118. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.2307/2686028")}

Examples


## Basic usage with Gaussian response, computing PRESS
set.seed(1234)
t <- rnorm(50)
y <- sin(t) + rnorm(50, 0, .25)
model_fit <- lgspline(t, y)
loo <- leave_one_out(model_fit)
press <- mean((y - loo)^2, na.rm = TRUE)

plot(loo, y,
   main = "LOO Cross-Validation Prediction vs. Observed Response",
   xlab = 'Prediction', ylab = 'Response')
abline(0, 1)


lgspline documentation built on May 8, 2026, 5:07 p.m.