R里有没有package可以画ROC,计算AUC的?能够有例子如何使用更好!
谢谢!
2个回答
可以用pROC
#加载pROC
library(pROC)
#画出ROC曲线
plot.roc(actual, prediction)
#计算ROC AUC
auc(actual, prediction)
It should be "plot.roc" function.
-
Sophia
2017-04-05 02:19
谢谢修正!我编辑一下
-
可爱多
2017-04-05 07:11
pROC比较慢,github上有人贴出了一个更快的版本。代码如下:
fastROC <- function(probs, class) {
class_sorted <- class[order(probs, decreasing=T)]
TPR <- cumsum(class_sorted) / sum(class)
FPR <- cumsum(class_sorted == 0) / sum(class == 0)
return(list(tpr=TPR, fpr=FPR))
}
# Helpful function adapted from: https://stat.ethz.ch/pipermail/r-help/2005-September/079872.html
fastAUC <- function(probs, class) {
x <- probs
y <- class
x1 = x[y==1]; n1 = length(x1);
x2 = x[y==0]; n2 = length(x2);
r = rank(c(x1,x2))
auc = (sum(r[1:n1]) - n1*(n1+1)/2) / n1 / n2
return(auc)
}