Learn R Programming

SFSI (version 1.2.0)

LARS: Least Angle Regression to solve LASSO-type problems

Description

Computes the entire LASSO solution for the regression coefficients, starting from zero, to the least-squares estimates, via the Least Angle Regression (LARS) algorithm (Efron, 2004). It uses as inputs a variance matrix among predictors and a covariance vector between response and predictors.

Usage

LARS(Sigma, Gamma, X =NULL, method=c("LAR","LAR-LASSO"),
      dfmax = NULL, eps = .Machine$double.eps, 
      scale = TRUE, mc.cores = 1L, return.beta = TRUE, 
      save.beta = FALSE, verbose = FALSE)

Value

Returns a list object with the following elements:

  • lambda: (vector) all the sequence of values of the LASSO penalty.

  • beta: (matrix) regression coefficients for each predictor (in rows) associated to each value of the penalization parameter lambda (in columns).

  • df: (vector) degrees of freedom, number of non-zero predictors associated to each value of lambda.

  • yHat: (matrix) fitted values calculated using a matrix of predictors (when argument X is not NULL), associated to each value of lambda (in columns).

The returned object is of the class 'LASSO' for which methods fitted exist. Function path.plot can be also used

Arguments

Sigma

(numeric matrix) Variance-covariance matrix of predictors. It can be of the "float32" type as per the 'float' R-package

Gamma

(numeric matrix) Covariance between response variable and predictors. If it contains more than one column, the algorithm is applied to each column separately as different response variables

X

(numeric matrix) Optional matrix of predictors to obtain fitted values

method

(character) Either:

  • 'LAR': Computes the entire sequence of all coefficients. Values of lambdas are calculated at each step.

  • 'LAR-LASSO': Similar to 'LAR' but solutions when a predictor leaves the solution are also returned.

Default is method='LAR'

dfmax

(integer) Maximum number of non-zero coefficients in the last LARS solution. Default dfmax=NULL will calculate solutions for the entire lambda sequence

eps

(numeric) An effective zero. Default is the machine precision

scale

TRUE or FALSE to scale matrix Sigma for variables with unit variance and scale Gamma by the standard deviation of the corresponding predictor taken from the diagonal of Sigma

mc.cores

(integer) Number of cores used. The analysis is run in parallel when mc.cores is greater than 1. Default is mc.cores=1

return.beta

TRUE or FALSE to whether return regression coefficients in the output object

save.beta

TRUE or FALSE to whether save regression coefficients (in a temporary folder). When TRUE coefficients are not returned in the output object and instead the path where coefficients were saved is returned. They can be retrieved using coef method if at least one return.beta or save.beta is TRUE

verbose

TRUE or FALSE to whether printing each LARS step

Author

Marco Lopez-Cruz (maraloc@gmail.com) and Gustavo de los Campos. Adapted from the 'lars' function in package 'lars' (Hastie & Efron, 2013)

Details

Finds solutions for the regression coefficients in a linear model

yi = x'i β + ei

where yi is the response for the ith observation, xi=(xi1,...,xip)' is a vector of \(p\) predictors assumed to have unit variance, β=(β1,...,βp)' is a vector of regression coefficients, and ei is a residual.

The regression coefficients β are estimated as function of the variance matrix among predictors (Σ) and the covariance vector between response and predictors (Γ) by minimizing the penalized mean squared error function

-Γ' β + 1/2 β'Σβ + 1/2 λ ||β||1

where λ is the penalization parameter and ||β||1 = ∑j=1j| is the L1-norm.

The algorithm to find solutions for each βj is fully described in Efron (2004) in which the "current correlation" between the predictor xij and the residual ei = yi - x'i β is expressed (up-to a constant) as

rj = Γj - Σ'j β

where Γj is the jth element of Γ and Σj is the jth column of the matrix Σ

References

Efron B, Hastie T, Johnstone I, Tibshirani R (2004). Least angle regression. The Annals of Statistics, 32(2), 407–499.

Friedman J, Hastie T, Tibshirani R(2010). Regularization paths for generalized linear models via coordinate descent. Journal of Statistical Software, 33(1), 1–22.

Hastie T, Efron B (2013). lars: least angle regression, Lasso and forward stagewise. https://cran.r-project.org/package=lars.

Tibshirani R (1996). Regression shrinkage and selection via the LASSO. Journal of the Royal Statistical Society B, 58(1), 267–288.

Examples

Run this code
  require(SFSI)
  data(wheatHTP)
  
  y = as.vector(Y[,"E1"])   # Response variable
  X = scale(X_E1)           # Predictors

  # Training and testing sets
  tst = which(Y$trial %in% 1:10)
  trn = seq_along(y)[-tst]

  # Calculate covariances in training set
  XtX = var(X[trn,])
  Xty = cov(X[trn,],y[trn])
  
  # Run the penalized regression
  fm1 = LARS(XtX,Xty,method="LAR-LASSO")  
  
  # Predicted values
  yHat1 = fitted(fm1, X=X[trn,])  # training data
  yHat2 = fitted(fm1, X=X[tst,])  # testing data
  
  # Penalization vs correlation
  plot(-log(fm1$lambda[-1]),cor(y[trn],yHat1[,-1]), main="training")
  plot(-log(fm1$lambda[-1]),cor(y[tst],yHat2[,-1]), main="testing")
  # \donttest{
  if(requireNamespace("float")){
   # Using a 'float' type variable
   XtX2 = float::fl(XtX)
   fm2 = LARS(XtX2,Xty,method="LAR-LASSO")  
   max(abs(fm1$beta-fm2$beta))      # Check for discrepances in beta
   max(abs(fm1$lambda-fm2$lambda))  # Check for discrepances in lambda
  }
  # }

Run the code above in your browser using DataLab