2

In the regularization problem, for example, LASSO, the regularization parameter, \lambda, is determined by cross validation (CV).

When writing the code in R, I usually generate a sequence of values of \lambda. Then, for each one, I conduct the CV. The whole process is coded in the way of for-loop.

Since the for loop is not efficient, is there any better way to realize the idea of choosing regularization parameter in code?

vtshen
  • 202
  • 1
  • 11
  • The `cv.glmnet` function from the `glmnet` package streamlines the process of cross validation with a range of lambda values. The `caret` package also has some additional tools for streamlining cross validation. (Although I don't think a loop is slowing the process down here, since the loop is only over the values of lambda and the folds. The computation-heavy model fitting is being done by a function that's presumably been optimized.) – eipi10 Sep 06 '17 at 23:01
  • Check the `glmnet` package and the papers referenced in its documentation. It uses an algorithm that exploits the work done at neighboring lambda values to compute the solutions down the entire lambda path, which is much more efficient than computing them at each lambda separately (in a loop or whatever). You should probably just use that package if you can. – Chris Haug Sep 06 '17 at 23:27

0 Answers0