使用Caret在逻辑回归中进行交叉验证的特征选择

3

我正在学习如何在R中实现逻辑回归。

我已经拿到了一个数据集,并将其分成了训练集和测试集。希望使用交叉验证来实现前向选择、后向选择和最佳子集选择,以选择最佳特征。

我正在使用caret在训练数据集上实现交叉验证,然后在测试数据上测试预测结果。

我看过caret中的rfe控制并查看了网站上的文档,还关注了这个问题的链接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)

谷歌搜索指向此链接 https://stat.ethz.ch/pipermail/r-help/2012-March/305508.html - abhiieor
1个回答

1
您可以在 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.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接