【发布时间】:2020-11-03 12:34:11
【问题描述】:
我有一个小数据集(37 个观察 x 23 个特征),并希望使用 LASSO 回归执行特征选择,以降低其维度。为此,我根据在线教程设计了以下代码
#Load the libraries
library(mlbench)
library(elasticnet)
library(caret)
#Initialize cross validation and train LASSO
cv_5 <- trainControl(method="cv", number=5)
lasso <- train( ColumnY ~., data=My_Data_Frame, method='lasso', trControl=cv_5)
#Filter out the variables whose coefficients have squeezed to 0
drop <-predict.enet(lasso$finalModel, type='coefficients', s=lasso$bestTune$fraction, mode='fraction')$coefficients
drop<-drop[drop==0]%>%names()
My_Data_Frame<- My_Data_Frame%>%select(-drop)
在大多数情况下,代码运行时没有错误,但偶尔会抛出以下错误:
Warning messages:
1: model fit failed for Fold2: fraction=0.9 Error in if (zmin < gamhat) { : missing value where TRUE/FALSE needed
2: In nominalTrainWorkflow(x = x, y = y, wts = weights, info = trainInfo, :
There were missing values in resampled performance measures.
我感觉会发生这种情况是因为我的数据行数很少,而且一些变量的方差很小。 有什么方法可以绕过或解决此问题(例如,在流程中设置参数)?
【问题讨论】:
标签: r feature-selection lasso-regression