tsutils (version 0.9.0)

lambdaseq: Generate sequence of lambda for LASSO regression

Description

Calculates the lambdaMax value, which is the penalty term (lambda) beyond which coefficients are guaranteed to be all zero and provides a sequence of nLambda values to lambdaMin in logarithmic descent.

Usage

lambdaseq(x, y, weight = NA, alpha = 1, standardise = TRUE,
  lambdaRatio = 1e-04, nLambda = 100, addZeroLambda = FALSE)

Arguments

x

matrix of regressors. See glmnet.

y

response variable. See glmnet.

weight

vector of length(nrow(y)) for weighted LASSO estimation. See glmnet.

alpha

elastic net mixing value. See glmnet.

standardise

if TRUE, then variables are standardised.

lambdaRatio

ratio between lambdaMax and lambdaMin. That is, lambdaMin <- lambdaMax * lambdaRatio.

nLambda

length of the lambda sequence.

addZeroLambda

if TRUE, then set the last value in the lambda sequence to 0, which is the OLS solution.

Value

A list that contains:

  • lambda: sequence of lambda values, from lambdaMax to lambdaMin.

  • lambdaMin: minimal lambda value.

  • lambdaMax: maximal lambda value.

  • nullMSE: MSE of the fit using just a constant term.

References

Hastie, T., Tibshirani, R., & Wainwright, M. (2015). Statistical learning with sparsity: the lasso and generalizations. CRC press.

Examples

Run this code
# NOT RUN {
y <- mtcars[,1]
x <- as.matrix(mtcars[,2:11])
lambda <- lambdaseq(x, y)$lambda

# }
# NOT RUN {
  library(glmnet)
  fit.lasso <- cv.glmnet(y, x, lambda = lambda)
  coef.lasso <- coef(fit.lasso, s = "lambda.1se")
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab