Skip to contents

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 adgeMatrix of predictors, shape [n, p].

Y

Numeric matrix, vector, or adgeMatrix of responses, shape [n, q].

lambda

Non-negative scalar ridge penalty.

intercept

Logical; when TRUE a column of ones is prepended to X before 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

adgeMatrix of shape [p, q].

fitted.values

adgeMatrix of shape [n, q], or NULL when include_fitted = FALSE.

residuals

adgeMatrix of shape [n, q], or NULL when include_residuals = FALSE.

lambda

The penalty value used.

rank

Integer model rank.

df.residual

Residual degrees of freedom.

See also

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