beld
beld

Reputation: 81

Plot multiple RFE results from caret in one plot

I used caret's RFE algorithm with ROC as metric and want to plot the result. This works fine, but now I want to put two results in one plot and I'm not sure if there's an easy solution for this or if this is even possible. I'm sorry if this is a silly question, not too advanced in R. Is there a solution for this?

Here's my code:

# 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"))

Normal plot generated from code: Plot generated from code

Upvotes: 1

Views: 470

Answers (1)

beld
beld

Reputation: 81

For anyone wondering how to easily do this, extract the results from the objects and plot it via ggplot package. My solution is a bit more complicated but you can easily modify it to you needs. Code is pretty self-explanatory. This is how I did it:


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))

This creates following plot: rfe plot

Upvotes: 1

Related Questions