0

I'm running LASSO using the glmnet package using the following commands:

x_ss <- model.matrix(y_variable ~ X_variables, data="data")
y_ss <- c(y_variable)
cv.output_ss <- cv.glmnet(x_ss,y_ss, alpha=1, family="gaussian", type.measure="mse")

lambda.min_ss <- cv.output_ss$lambda.min

coef(cv.output_ss,s=lambda.min_ss)

From my understanding of LASSO regression, the estimates generated varies slightly every time I run it. As such, I am thinking of maybe generating 1000 simulations, and collecting the value of the estimates for my X-variable in question, so that I can report more meaningful stuff, like the mean and variance. Is there any way I can run this multiple times & 'save' the output so that I can get my mean and variance of the estimates?

zx8754
  • 52,746
  • 12
  • 114
  • 209
Hisham
  • 43
  • 1
  • 4

1 Answers1

1

Naturally, you can use sapply, lapply or even replicate.

E.g.

xy <- replicate(1000, {
  # ...
  coef(...)
}, simplify = FALSE)

would run the same code 1000 times and output the result of coef in a list. After the function has finished, you can manipulate xy in any way you want, e.g. extract desired statistics, bind it into a data.frame or a matrix and report on means, variances, distributions...

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197