[英]How to Plot the ROC Curve in rStudios from the given values?
混淆矩阵值
截止 / TP / FP / TN / FN
0.1 100 50 500 450
0.2 150 100 450 400
0.3 250 150 400 300
0.4 300 200 350 250
0.5 350 250 300 200
0.6 350 300 250 200
0.7 400 350 200 150
0.8 400 400 150 150
0.9 450 450 100 100
1.0 500 500 50 50
仅使用 base-R,您可以编写以下代码:
## your data
df <- read.table(header = TRUE, text = "
Cut_off TP FP TN FN
0.1 100 50 500 450
0.2 150 100 450 400
0.3 250 150 400 300
0.4 300 200 350 250
0.5 350 250 300 200
0.6 350 300 250 200
0.7 400 350 200 150
0.8 400 400 150 150
0.9 450 450 100 100
1.0 500 500 50 50")
## calculate False Positive ratio
df$FPR <- df$FP/(df$FP + df$TN)
## calculte True Positive Ratio
df$TPR <- df$TP/(df$TP + df$FN)
## df is now:
Cut_off TP FP TN FN FPR TPR
0.1 100 50 500 450 0.09090909 0.1818182
0.2 150 100 450 400 0.18181818 0.2727273
0.3 250 150 400 300 0.27272727 0.4545455
0.4 300 200 350 250 0.36363636 0.5454545
0.5 350 250 300 200 0.45454545 0.6363636
0.6 350 300 250 200 0.54545455 0.6363636
0.7 400 350 200 150 0.63636364 0.7272727
0.8 400 400 150 150 0.72727273 0.7272727
0.9 450 450 100 100 0.81818182 0.8181818
1.0 500 500 50 50 0.90909091 0.9090909
## plot the ROC with base plot
plot(df$FPR, df$TPR, type = "b",
xlim = c(0,1), ylim = c(0,1),
main = 'ROC Curve',
xlab = "False Positive Rate (1 - Specificity)",
ylab = "True Positive Rate (Sensitivity)",
col = "blue")
abline(a = 0, b = 1, lty=2, col = "grey") ### pure chance line
产生以下 plot:
如果你想用 label 标记截止点,你需要在abline(...
text(df$FPR, df$TPR+.05, df$Cut_off, col = "blue", cex = .7)
产生这个 plot:
这是您可以使用带有ggplot
和dplyr
的 ROC plot 的一种方法。 首先是您的数据:
df = structure(list(Cutoff = c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7,
0.8, 0.9, 1), TP = c(100, 150, 250, 300, 350, 350, 400, 400,
450, 500), FP = c(50, 100, 150, 200, 250, 300, 350, 400, 450,
500), TN = c(500, 450, 400, 350, 300, 250, 200, 150, 100, 50),
FN = c(450, 400, 300, 250, 200, 200, 150, 150, 100, 50)), class =
"data.frame", row.names = c(NA,-10L))
对于 ROC,您需要 False-Positive-rate (FPR) 和 True-Positive-rate (TPR),我在这里使用mutate
计算:
df %>% mutate( FPR = FP / (FP + TN) , TPR = TP / ( TP + FN )) %>%
ggplot( aes ( x = FPR , y = TPR)) + geom_point(size = 0) +
geom_line(size = 1, alpha = 1) + theme_bw() +
xlab("1 - Specificity") + ylab("Sensitivity") +
theme(
plot.title = element_text(size = 20,hjust = 0.5),
axis.text = element_text(size =10),
axis.title = element_text(size = 20)
) + annotate('segment' , x = 0, xend = 1, y = 0, yend = 1, alpha = 0.7)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.