我这里哪里出错了?
roc我正在尝试使用在同一个图上绘制两条曲线ggplot。
当我运行以下代码时出现此错误
Error: Don't know how to add o to a plot
Run Code Online (Sandbox Code Playgroud)
ggplot 不起作用
ggroc(roc_1) +
ggroc(roc_2) +
labs(title = "ROC curve", y = "Sensitivity", x = "Specificity")
Run Code Online (Sandbox Code Playgroud)
有效的基础包
plot(roc_1, col = 1, lty = 2, main = "ROC")
plot(roc_2, col = 4, lty = 3, add = TRUE)
Run Code Online (Sandbox Code Playgroud)
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"
Run Code Online (Sandbox Code Playgroud)
这是基本包的外观,只是尝试在 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))
Run Code Online (Sandbox Code Playgroud)
编辑:解决方案
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))
Run Code Online (Sandbox Code Playgroud)