ROC curve in R using rpart package?

♀尐吖头ヾ 提交于 2019-12-05 18:33:50

The prediction() function from the ROCR package expects the predicted "success" probabilities and the observed factor of failures vs. successes. In order to obtain the former you need to apply predict(..., type = "prob") to the rpart object (i.e., not "class"). However, as this returns a matrix of probabilities with one column per response class you need to select the "success" class column.

As your example, unfortunately, is not reproducible I'm using the kyphosis data from the rpart package for illustration:

library("rpart")
data("kyphosis", package = "rpart")
rp <- rpart(Kyphosis ~ ., data = kyphosis)

Then you can apply the prediction() function from ROCR. Here, I'm using the in-sample (training) data but the same can be applied out of sample (test data):

library("ROCR")
pred <- prediction(predict(rp, type = "prob")[, 2], kyphosis$Kyphosis)

And you can visualize the ROC curve:

plot(performance(pred, "tpr", "fpr"))
abline(0, 1, lty = 2)

Or the accuracy across cutoffs:

plot(performance(pred, "acc"))

Or any of the other plots and summaries supported by ROCR.

library("ROCR")
Pred.cart = predict(train.cart, newdata = Test, type = "prob")[,2] 
Pred2 = prediction(Pred.cart, Test$n.use) 
plot(performance(Pred2, "tpr", "fpr"))
abline(0, 1, lty = 2)

The above code snippet will work for you.

for more details refer to link : Classification Trees (R)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!