我首先将预测分数和类别转换为矩阵中的向量。
有一个“PRROC”包提供了与“ROCR”类似的生成ROC和PRC的功能,并且它还给出了PRC的AUC。
具体来说,我正在使用数据ROCR.simple
以“ROCR”包为例。
library(PRROC)
library(ROCR)
data("ROCR.simple")
scores <- data.frame(ROCR.simple$predictions, ROCR.simple$labels)
pr <- pr.curve(scores.class0=scores[scores$ROCR.simple.labels=="1",]$ROCR.simple.predictions,
scores.class1=scores[scores$ROCR.simple.labels=="0",]$ROCR.simple.predictions,
curve=T)
请注意,在此函数中,“分数.class0“ 需要是分数positive类(这有点令人困惑,因为我个人认为 0 为负数,1 为正数)。所以我调换了0和1的顺序。
这样PR曲线和AUC就全部保存在pr
多变的。
pr
Precision-recall curve
Area under curve (Integral):
0.7815038
Area under curve (Davis & Goadrich):
0.7814246
Curve for scores from 0.005422562 to 0.9910964
( can be plotted with plot(x) )
然后,您可以使用以下命令绘制 PRCplot(pr)
或使用 ggplot:
y <- as.data.frame(pr$curve)
ggplot(y, aes(y$V1, y$V2))+geom_path()+ylim(0,1)
得到的曲线与ROCR包制作的曲线相同。