#'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
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.