[英]ROC curve in ggplot calculation [r]
I am trying to create a ROC curve in ggplot
我正在尝试在ggplot
中创建 ROC 曲线
I wrote function myself, however when I compare my results to results from roc_curve
function from community (that I believe more) I get different results.我自己写了 function,但是当我将我的结果与来自社区的roc_curve
function 的结果进行比较时(我相信更多),我得到了不同的结果。
I would like to ask where is mistake in the function below?请问下面的function哪里出错了?
library(ggplot2)
library(dplyr)
library(yardstick)
n <- 300 # sample size
data <-
data.frame(
real = sample(c(0,1), replace=TRUE, size=n),
pred = sample(runif(n), replace=TRUE, size=n)
)
simple_roc <- function(labels, scores){
labels <- labels[order(scores, decreasing=TRUE)]
data.frame(TPR=cumsum(labels)/sum(labels), FPR=cumsum(!labels)/sum(!labels), labels)
}
simple_roc(data$real, data$pred) %>%
ggplot(aes(TPR, FPR)) +
geom_line()
yardstick::roc_curve(data, factor(real), pred) %>%
ggplot(aes(1 - specificity, sensitivity)) +
geom_line()
First you need to anchor your ROC curve in the points (0, 0) and (1, 1).首先,您需要将 ROC 曲线锚定在 (0, 0) 和 (1, 1) 点。
simple_roc <- function(labels, scores){
labels <- labels[order(scores, decreasing=TRUE)]
data.frame(
TPR = c(0, cumsum(labels)/sum(labels), 1),
FPR = c(0, cumsum(!labels)/sum(!labels), 1)
)
}
Then the order in which your data is presented matters in ggplot2.然后在 ggplot2 中呈现数据的顺序很重要。 Reversing the line direction should get you a bit closer:反转线的方向应该让你更接近一点:
yardstick::roc_curve(data, factor(real), pred) %>%
ggplot(aes(rev(1 - specificity), rev(sensitivity))) +
geom_line()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.