View source: R/leave_one_out.R
| leave_one_out | R Documentation |
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.
leave_one_out(model_fit, leverage_threshold = 100)
model_fit |
A fitted lgspline model object. |
leverage_threshold |
Numeric scalar. Observations with
|
A vector of leave-one-out cross-validated predictions
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")}
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.