I have chosen a lambda by running the LASSO multiple times and taking the mean lambda, I have used glmnet
. I know want to run a 10-fold cross validation for this LASSO with this Lambda.
This is an example of the code I have tried so far:
library(caret)
library(glmnet)
train.control = trainControl(method = "cv", number = 10)
lm.out = lm(outcome ~ 0 +., data = df)
x = model.matrix(lm.out)
y = df$outcome
model = train(glmnet(x, y, lambda = mean(Lambda_LASSO)),
data = df, trControl = train.control)
Here Lambda_LASSO is a vector of Lambdas taken out from iterative runs of cv.glmnet.
First of all, I have to say this sounds really odd:
I have chosen a lambda by running the LASSO multiple times and taking the mean lambda
What would be the purpose of taking the mean of your lambda values?
Next time provide an example dataset, and also specify whether it's classification or regression. let's say your df
is something like this and we get the lambdas from glmnet:
df = data.frame(matrix(runif(50*30),ncol=30))
df$outcome = rnorm(50)
x = model.matrix(outcome ~ 0 +., data = df)
y = df$outcome
Lambda_LASSO = glmnet(x,y)$lambda
You can feed it into caret using tuneGrid =
and fix alpha at 1 since you are doing lasso:
train.control = trainControl(method = "cv", number = 10)
model = train(x=x,y=y,
tuneGrid = data.frame(alpha=1,lambda = mean(Lambda_LASSO)),
trControl = train.control,
method = "glmnet")
glmnet
50 samples
30 predictors
No pre-processing
Resampling: Cross-Validated (10 fold)
Summary of sample sizes: 43, 46, 46, 45, 46, 45, ...
Resampling results:
RMSE Rsquared MAE
1.519513 0.3486916 1.286363
Tuning parameter 'alpha' was held constant at a value of 1
Tuning
parameter 'lambda' was held constant at a value of 0.03752899