简体   繁体   English

R 中的 ROC 曲线与决策树的 rpart

[英]ROC Curve in R with rpart for a decision tree

I have an issue with creating a ROC Curve for my decision tree created by the rpart package.我在为 rpart package 创建的决策树创建 ROC 曲线时遇到问题。 My goal was to predict "y" the success of the bank's marketing campaign.我的目标是预测“y”银行营销活动的成功。 In the end, you can get a "yes" or a "no" as a possible answer.最后,您可以得到“是”或“否”作为可能的答案。 How can I approach my next step the ROC curve plot?如何接近我的下一步 ROC 曲线 plot?

Here is the R code I have so far:这是我到目前为止的 R 代码:

library(caTools) 
library(rpart) 
library(rpart.plot) 

set.seed(1234) 
sample = sample.split(bank$y, SplitRatio = .75) 
train = subset(bank, sample==TRUE) 
test = subset(bank, sample==FALSE)

tree <-rpart(y ~.,method="class",data=train) 
tree.preds<-predict(tree, test)
tree.preds<-as.data.frame(tree.preds) 
joiner <- function(x) {if (x >= 0.5) {return("Yes") } else {return("No")}}
tree.preds$y <- sapply(tree.preds$yes, joiner) 
table(tree.preds$y, test$y) 
prp(tree) 

First for ROC analysis you will want to get numeric predictions, such as probabilities:首先对于 ROC 分析,您需要获得数字预测,例如概率:

predict(tree, test, type="prob")

If your variable had yes and no as answers, you will get two columns, labeled accordingly.如果您的变量有“是”和“否”作为答案,您将获得相应标记的两列。 I will assume that "yes" is the second one, and save that as predictions:我将假设“是”是第二个,并将其保存为预测:

tree.preds <- predict(tree, test, type="prob")[, 2]

Then you can plug it this directly into a ROC function, such as the one provided by pROC:然后你可以把它直接插入 ROC function,比如 pROC 提供的那个:

library(pROC)
tree.roc <- roc(test$y, tree.preds)
print(tree.roc)
plot(tree.roc)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM