R/lm_ridge_beta.R

Defines functions lm_ridge_beta

Documented in lm_ridge_beta

#'ridge regression (self-defined)
#'
#'This function is used to return ridge regression estimators.
#'
#'@param X a design matrix containing all predictors.
#'@param Y a response vector.
#'@param lambda a lambda value specified, serving as the coefficient of the penalty terms.
#'
#'@return ridge regression estimators.
#'
#'@examples
#'
#' #If do not have a design matrix and a response vector,
#' #firstly turn the formula and dataset into the required matrix
#' mf<-model.frame(Sepal.Width~.,iris)
#' mt<-attr(mf,"terms")
#' X <- model.matrix(mt, mf)
#' Y <- as.matrix(model.response(mf))
#' #call the function
#' lm_ridge_beta(X,Y,10)
#'
#'@export

lm_ridge_beta<-function(X,Y,lambda)
  {
    p<-ncol(X)
    beta1<-solve(t(X) %*% X + lambda * diag(p)) %*% t(X) %*% Y
    beta1
  }
Jiachen1027/bis557 documentation built on Oct. 30, 2019, 7:41 p.m.