如何根据给定值在 rStudios 中绘制 ROC 曲线?
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
产生以下情节:
如果您想用标签标记 cut-off 点,您需要在 abline(...
行之后添加以下行
text(df$FPR, df$TPR+.05, df$Cut_off, col = "blue", cex = .7)
生成此图:
这是一种使用 ggplot
和 dplyr
绘制 ROC 图的方法。
首先是您的数据:
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,您需要假阳性率 (FPR) 和真阳性率 (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)
结果如下:
如果你想在图上有点,你可以在 geom_point
中更改大小,这将是结果:
混淆矩阵值
截止/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
产生以下情节:
如果您想用标签标记 cut-off 点,您需要在 abline(...
text(df$FPR, df$TPR+.05, df$Cut_off, col = "blue", cex = .7)
生成此图:
这是一种使用 ggplot
和 dplyr
绘制 ROC 图的方法。
首先是您的数据:
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,您需要假阳性率 (FPR) 和真阳性率 (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)
结果如下:
如果你想在图上有点,你可以在 geom_point
中更改大小,这将是结果: