我在哪里错了?
我试图使用roc
在同一个地块上绘制两条ggplot
曲线。
运行以下代码时出现此错误
Error: Don't know how to add o to a plot
ggplot不起作用
ggroc(roc_1) +
ggroc(roc_2) +
labs(title = "ROC curve", y = "Sensitivity", x = "Specificity")
基础包工作
plot(roc_1, col = 1, lty = 2, main = "ROC")
plot(roc_2, col = 4, lty = 3, add = TRUE)
dput太大而无法在stackoverflow上发布,所以这里是ROC
计算之一的结构。
我试图绘制两条类似于下面的ROC曲线。
List of 15
$ percent : logi FALSE
$ sensitivities : num [1:26455] 1 1 1 1 1 1 1 1 1 1 ...
$ specificities : num [1:26455] 0.00 4.00e-05 8.01e-05 1.20e-04 1.60e-04 ...
$ thresholds : num [1:26455] -Inf 0.0017 0.00189 0.00201 0.00214 ...
$ direction : chr "<"
$ cases : num [1:1540] 0.958 0.919 0.973 0.785 0.933 ...
$ controls : num [1:24975] 0.6604 0.026 0.1389 0.0558 0.0594 ...
$ fun.sesp :function (thresholds, controls, cases, direction)
$ auc :Classes 'auc', 'numeric' atomic [1:1] 0.942
.. ..- attr(*, "partial.auc")= logi FALSE
.. ..- attr(*, "percent")= logi FALSE
.. ..- attr(*, "roc")=List of 15
.. .. ..$ percent : logi FALSE
.. .. ..$ sensitivities : num [1:26455] 1 1 1 1 1 1 1 1 1 1 ...
.. .. ..$ specificities : num [1:26455] 0.00 4.00e-05 8.01e-05 1.20e-04 1.60e-04 ...
.. .. ..$ thresholds : num [1:26455] -Inf 0.0017 0.00189 0.00201 0.00214 ...
.. .. ..$ direction : chr "<"
.. .. ..$ cases : num [1:1540] 0.958 0.919 0.973 0.785 0.933 ...
.. .. ..$ controls : num [1:24975] 0.6604 0.026 0.1389 0.0558 0.0594 ...
.. .. ..$ fun.sesp :function (thresholds, controls, cases, direction)
.. .. ..$ auc :Classes 'auc', 'numeric' atomic [1:1] 0.942
.. .. .. .. ..- attr(*, "partial.auc")= logi FALSE
.. .. .. .. ..- attr(*, "percent")= logi FALSE
.. .. .. .. ..- attr(*, "roc")=List of 8
.. .. .. .. .. ..$ percent : logi FALSE
.. .. .. .. .. ..$ sensitivities: num [1:26455] 1 1 1 1 1 1 1 1 1 1 ...
.. .. .. .. .. ..$ specificities: num [1:26455] 0.00 4.00e-05 8.01e-05 1.20e-04 1.60e-04 ...
.. .. .. .. .. ..$ thresholds : num [1:26455] -Inf 0.0017 0.00189 0.00201 0.00214 ...
.. .. .. .. .. ..$ direction : chr "<"
.. .. .. .. .. ..$ cases : num [1:1540] 0.958 0.919 0.973 0.785 0.933 ...
.. .. .. .. .. ..$ controls : num [1:24975] 0.6604 0.026 0.1389 0.0558 0.0594 ...
.. .. .. .. .. ..$ fun.sesp :function (thresholds, controls, cases, direction)
.. .. .. .. .. ..- attr(*, "class")= chr "roc"
.. .. ..$ call : language roc.default(response = results$testactual, predictor = results$pred)
.. .. ..$ original.predictor: num [1:26515] 0.6604 0.026 0.1389 0.0558 0.0594 ...
.. .. ..$ original.response : int [1:26515] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..$ predictor : num [1:26515] 0.6604 0.026 0.1389 0.0558 0.0594 ...
.. .. ..$ response : int [1:26515] 0 0 0 0 0 0 0 0 0 0 ...
.. .. ..$ levels : chr [1:2] "0" "1"
.. .. ..- attr(*, "class")= chr "roc"
$ call : language roc.default(response = results$testactual, predictor = results$pred)
$ original.predictor: num [1:26515] 0.6604 0.026 0.1389 0.0558 0.0594 ...
$ original.response : int [1:26515] 0 0 0 0 0 0 0 0 0 0 ...
$ predictor : num [1:26515] 0.6604 0.026 0.1389 0.0558 0.0594 ...
$ response : int [1:26515] 0 0 0 0 0 0 0 0 0 0 ...
$ levels : chr [1:2] "0" "1"
- attr(*, "class")= chr "roc"
以下是基本包的外观,只是尝试在ggplot中执行此操作
编辑:临时解决问题的方法。
rocy <- cbind(roc_1$sensitivities, roc_1$specificities, roc_2$sensitivities, roc_2$specificities)
rocy <- as.data.frame(rocy)
ggplot(rocy) +
geom_line(aes(y = V1, x = V2)) +
geom_line(aes(y = V3, x = V4))
编辑:解决方案
library(pROC)
rocobj1 <- roc(df$actualoutcome1, data$prediction1)
rocobj2 <- roc(df$actualoutcome1, data$prediction2)
ggroc(list(call_roc_name_1 = rocobj1, call_roc_name_2 = rocobj2))