Solves the penalized least-squares problem
\(\min_\beta \|Y - X\beta\|^2 + \lambda \|\beta\|^2\) for a
single penalty value lambda.
Usage
ridge_fit(
X,
Y,
lambda,
intercept = FALSE,
penalize_intercept = FALSE,
include_fitted = TRUE,
include_residuals = TRUE,
cache = TRUE
)Arguments
- X
Numeric matrix or
adgeMatrixof predictors, shape[n, p].- Y
Numeric matrix, vector, or
adgeMatrixof responses, shape[n, q].- lambda
Non-negative scalar ridge penalty.
- intercept
Logical; when
TRUEa column of ones is prepended toXbefore fitting.- penalize_intercept
Logical; when
FALSE(default) the intercept coefficient is excluded from the penalty.- include_fitted
Logical; include fitted values in the result.
- include_residuals
Logical; include residuals in the result.
- cache
Logical; cache \(X^T X\) for reuse across calls sharing the same
X.
Value
An object of class "ridge_fit", a named list
containing:
- coefficients
adgeMatrixof shape[p, q].- fitted.values
adgeMatrixof shape[n, q], orNULLwheninclude_fitted = FALSE.- residuals
adgeMatrixof shape[n, q], orNULLwheninclude_residuals = FALSE.- lambda
The penalty value used.
- rank
Integer model rank.
- df.residual
Residual degrees of freedom.
Examples
X <- matrix(rnorm(50), nrow = 10)
y <- rnorm(10)
fit <- ridge_fit(X, y, lambda = 1)
coef(fit)
#> An amatrix dense matrix [cpu|policy=auto|precision=strict]
#> 5 x 1 Matrix of class "adgeMatrix"
#> [,1]
#> [1,] 0.002400241
#> [2,] 0.473646815
#> [3,] -0.139202105
#> [4,] -0.223819711
#> [5,] -0.375631527