我目前正在学习如何在 R 中实现逻辑回归
我已经获取了一个数据集并将其分成训练集和测试集,并希望实现前向选择
、后向选择
和最佳子集选择
使用交叉验证来选择最佳功能。
我正在使用 caret
对训练数据集实现交叉验证
,然后测试对测试数据的预测。
我在 caret 中看到了 rfe
控件,还查看了关于 caret
的文档 website以及关注问题 How to use wrapper feature selection with algorithms in R? 的链接.我不清楚如何更改特征选择的类型,因为它似乎默认为向后选择。任何人都可以帮助我完成我的工作流程。下面是一个可重现的例子
library("caret")
# Create an Example Dataset from German Credit Card Dataset
mydf <- GermanCredit
# Create Train and Test Sets 80/20 split
trainIndex <- createDataPartition(mydf$Class, p = .8,
list = FALSE,
times = 1)
train <- mydf[ trainIndex,]
test <- mydf[-trainIndex,]
ctrl <- trainControl(method = "repeatedcv",
number = 10,
savePredictions = TRUE)
mod_fit <- train(Class~., data=train,
method="glm",
family="binomial",
trControl = ctrl,
tuneLength = 5)
# Check out Variable Importance
varImp(mod_fit)
summary(mod_fit)
# Test the new model on new and unseen Data for reproducibility
pred = predict(mod_fit, newdata=test)
accuracy <- table(pred, test$Class)
sum(diag(accuracy))/sum(accuracy)
最佳答案
您可以简单地在 mod_fit 中调用它。当涉及到逐步向后时,下面的代码就足够了
trControl <- trainControl(method="cv",
number = 5,
savePredictions = T,
classProbs = T,
summaryFunction = twoClassSummary)
caret_model <- train(Class~.,
train,
method="glmStepAIC", # This method fits best model stepwise.
family="binomial",
direction="backward", # Direction
trControl=trControl)
注意在trControl中
method= "cv", # No need to call repeated here, the number defined afterward defines the k-fold.
classProbs = T,
summaryFunction = twoClassSummary # Gives back ROC, sensitivity and specifity of the chosen model.
关于R:在逻辑回归上使用 Caret 进行交叉验证的特征选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42314851/