在一个图中绘制插入符号的多个 RFE 结果

Plot multiple RFE results from caret in one plot

我使用了以 ROC 作为度量的 caret 的 RFE 算法,并想绘制结果。这工作正常,但现在我想将两个结果放在一个图中,我不确定是否有一个简单的解决方案,或者这是否可能。如果这是一个愚蠢的问题,我很抱歉,在 R 中不是太高级。有解决方案吗?

这是我的代码:

# define the control using a random forest selection function
rfFuncs$summary <- twoClassSummary
control <- rfeControl(functions=rfFuncs, verbose = TRUE, method="cv", number=10)
# run the RFE algorithm
results_rfe_roc_deliv <- rfe(data_deliverable[,1:91], data_deliverable[,92], sizes=c(1:91), rfeControl=control ,metric = 'ROC')
# summarize the results
print(results_rfe_roc_deliv)
# list the chosen features
predictors(results_rfe_roc_deliv)


results_rfe_roc_non_deliv <- rfe(data_non_deliverable[,1:91], data_non_deliverable[,92], sizes=c(1:91), rfeControl=control ,metric = 'ROC')
# summarize the results
print(results_rfe_roc_non_deliv)
# list the chosen features
predictors(results_rfe_roc_non_deliv)
# plot the results

plot(results_rfe_roc_deliv, type=c("g", "o"))
plot(results_rfe_roc_non_deliv, type=c("g", "o"))

从代码生成的正常图:

对于任何想知道如何轻松做到这一点的人,请从对象中提取结果并通过 ggplot 包绘制它。我的解决方案有点复杂,但您可以根据需要轻松修改它。代码是不言自明的。我是这样做的:


rfe_result_export <- results_rfe_roc_deliv$results
names(rfe_result_export)[names(rfe_result_export) == 'ROC'] <- 'ROC.deliv'
rfe_result_export <- rfe_result_export[,-c(3:7)]
rfe_result_export$ROC.non.deliv <- results_rfe_roc_non_deliv$results$ROC
#flag data
rfe_result_export$highlight.deliv <- FALSE
rfe_result_export$highlight.non.deliv <- FALSE
#points of interest
rfe_result_export$highlight.deliv[c(40,66)] <- TRUE
rfe_result_export$highlight.non.deliv[c(48,86)] <- TRUE

ggplot(rfe_result_export, aes(Variables)) +
  geom_point(aes(y=ROC.non.deliv, colour="blue")) +
  geom_point(aes(y=ROC.deliv, colour="black")) + 
  #points of interest
  geom_point(data = subset(rfe_result_export, highlight.deliv == TRUE), aes(y=ROC.deliv, colour="red")) +
  geom_point(data = subset(rfe_result_export, highlight.non.deliv == TRUE), aes(y=ROC.non.deliv, colour="red")) +
  scale_color_manual(values =c("blue","black","red"), labels = c("Deliverable",'Non-Delivarble', "Points of interest")) +
  ylab("ROC value") + ggtitle("Results of Recursive Feature Elimination") +
  scale_x_continuous(breaks = scales::pretty_breaks(n = 10))

这将创建以下情节: