R/glmnet_ridge_lambda_best.R

Defines functions glmnet_ridge_lambda_best

Documented in glmnet_ridge_lambda_best

#'choose the best lambda in ridge regression (standard)
#'
#'This function is used to select the optimal lambda in ridge regression by function cv.glmnet in the glmnet package.It's used to check whether the self-defined method works.
#'
#'@param formula a symbolic description of the model to be fitted.
#'@param data a dataframe containing the variables in the model.
#'
#'@return the best lambda in ridge regression given by the function cv.glmnet.
#'
#'@examples
#'glmnet_ridge_lambda_best(Sepal.Width~.,iris)
#'
#'@export

glmnet_ridge_lambda_best<-function(formula,data)
 {
  mf <- model.frame(formula, data)
  mt <- attr(mf, "terms")
  X <- Matrix::sparse.model.matrix(mt, mf)
  y <- model.response(mf)
  set.seed(1)
  ridge <- glmnet::cv.glmnet(X, y,alpha=0,nfolds=3,lambda.min.ratio = 0)
  ridge$lambda.min
}
Jiachen1027/bis557 documentation built on Oct. 30, 2019, 7:41 p.m.