提问人:Debajyoti Kabiraj 提问时间:10/29/2023 最后编辑:desertnautDebajyoti Kabiraj 更新时间:10/29/2023 访问量:23
在R中使用套索训练模型时,如何解决“错误:优化参数网格应具有列alpha,lambda”?
How to resolve "Error: The tuning parameter grid should have columns alpha, lambda" when training model by lasso in R?
问:
我正在尝试按照 YouTube 上的本教程学习套索模型:https://www.youtube.com/watch?v=5GZ5BHOugBQ&ab_channel=DavidCaughlin。但是,当我尝试训练模型时,我遇到了一个错误。
Error: The tuning parameter grid should have columns alpha, lambda
数据集可以在这里找到: https://github.com/davidcaughlin/R-Tutorial-Data-Files/blob/master/lasso.csv
library(readr)
library(caret)
library(glmnet)
df<- read_csv("/Path/lasso.csv")
set.seed(1001)
#partition (split) and create index matrix of selected values
index<- createDataPartition(df$y, p = 0.8, list = FALSE, times = 1)
#create test and training df
train_df <- df[index, ]
trest_df <- df[-index, ]
# specifying k-fold cross validation (10 fold) framework
crlspecs <- trainControl(method = "cv", number = 10,
savePredictions = "all")
#specify and train lasso reg model
#create vector of potential lambda values
lambda_vec <- 10^seq(5, -5, length = 500)
set.seed(1001)
#specify lasso regression model to be estimated using traing data
# and 10-fold cross-validation framework
TunGrid<-expand.grid(alpha=1,labda=lambda_vec)
#installing latest catet package
devtools::install_github('topepo/caret/pkg/caret')
library("caret")
getNamespaceVersion("caret")
version
"6.0-94"
#model training
model1 <- train( y ~ . ,
data = train_df,
preProcess=c("center","scale"),
method="glmnet",
tuneGrid=TunGrid,
trControl=crlspecs,
na.action = na.omit )
如果有人能在这方面提供任何建议,那将是非常有帮助的。
答: 暂无答案
评论
TunGrid<-expand.grid(alpha=1,labda=lambda_vec)
lambda=lambda_vec