R/my-lm-ridge.R

Defines functions my_lm_ridge

Documented in my_lm_ridge

#' @title Ridge Regression
#' @description Perform ridge regression, which is able to deal with collinearity among the columns of the design
#' @param form a formula object, e.g. y ~ x1 + x2
#' @param data a data frame
#' @param lambda a scalar (tuning parameter) from 0 to infinity, also called the penalty parameter
#'
#' @examples
#' data(iris)
#' ridge_fit <- my_lm_ridge(form = Sepal.Length ~ ., data = iris, lambda = 0.5)
#' ridge_fit$coefficients
#' @export

my_lm_ridge <- function(form, data, lambda){
  mms <- make_model_matrices(form, data)
  X <- mms$X
  Y <- mms$Y
  svd_x <- svd(X)

  Sigma <- diag(svd_x$d)
  lambda_I <- diag(rep(lambda, length(svd_x$d)))
  beta <- svd_x$v %*% solve(Sigma^2 + lambda_I, tol = 1e-33) %*% Sigma %*% t(svd_x$u) %*% Y
  ret <- list(form = form, coefficients = beta)
  class(ret) <- "my_lm_ridge"
  ret
}
tqchen07/bis557 documentation built on Dec. 21, 2020, 3:06 a.m.