# KeepNotes blog

Stay hungry, Stay Foolish.

0%

#### 实现方式

`timeROC`包相比`survivalROC`包会多计算个AUC的置信区间

``````library(timeROC)
library(survival)

data(mayo)

time_roc_res <- timeROC(
T = mayo\$time,
delta = mayo\$censor,
marker = mayo\$mayoscore5,
cause = 1,
weighting="marginal",
times = c(3 * 365, 5 * 365, 10 * 365),
ROC = TRUE,
iid = TRUE
)``````
##### 计算AUC值及其置信区间
``````> time_roc_res\$AUC
t=1095    t=1825    t=3650
0.8982790 0.9153621 0.8576153 ``````

``````> confint(time_roc_res, level = 0.95)\$CI_AUC
2.5% 97.5%
t=1095 85.01 94.64
t=1825 87.42 95.65
t=3650 79.38 92.14``````
##### 绘制time-dependent ROC曲线

``````plot(time_roc_res, time=3 * 365, col = "red", title = FALSE)
plot(time_roc_res, time=5 * 365, add=TRUE, col="blue")
plot(time_roc_res, time=10 * 365, add=TRUE, col="green")
legend("bottomright",c("3 Years" ,"5 Years", "10 Years"),
col=c("red", "blue", "green"), lty=1, lwd=2)``````

``````time_ROC_df <- data.frame(
TP_3year = time_roc_res\$TP[, 1],
FP_3year = time_roc_res\$FP[, 1],
TP_5year = time_roc_res\$TP[, 2],
FP_5year = time_roc_res\$FP[, 2],
TP_10year = time_roc_res\$TP[, 3],
FP_10year = time_roc_res\$FP[, 3]
)
library(ggplot2)
ggplot(data = time_ROC_df) +
geom_line(aes(x = FP_3year, y = TP_3year), size = 1, color = "#BC3C29FF") +
geom_line(aes(x = FP_5year, y = TP_5year), size = 1, color = "#0072B5FF") +
geom_line(aes(x = FP_10year, y = TP_10year), size = 1, color = "#E18727FF") +
geom_abline(slope = 1, intercept = 0, color = "grey", size = 1, linetype = 2) +
theme_bw() +
annotate("text",
x = 0.75, y = 0.25, size = 4.5,
label = paste0("AUC at 3 years = ", sprintf("%.3f", time_roc_res\$AUC[[1]])), color = "#BC3C29FF"
) +
annotate("text",
x = 0.75, y = 0.15, size = 4.5,
label = paste0("AUC at 5 years = ", sprintf("%.3f", time_roc_res\$AUC[[2]])), color = "#0072B5FF"
) +
annotate("text",
x = 0.75, y = 0.05, size = 4.5,
label = paste0("AUC at 10 years = ", sprintf("%.3f", time_roc_res\$AUC[[3]])), color = "#E18727FF"
) +
labs(x = "False positive rate", y = "True positive rate") +
theme(
axis.text = element_text(face = "bold", size = 11, color = "black"),
axis.title.x = element_text(face = "bold", size = 14, color = "black", margin = margin(c(15, 0, 0, 0))),
axis.title.y = element_text(face = "bold", size = 14, color = "black", margin = margin(c(0, 15, 0, 0)))
)``````
##### 比较两个time-dependent AUC

``````time_roc_res2 <- timeROC(
T = mayo\$time,
delta = mayo\$censor,
marker = mayo\$mayoscore4,
cause = 1,
weighting="marginal",
times = c(3 * 365, 5 * 365, 10 * 365),
ROC = TRUE,
iid = TRUE
)
> time_roc_res2\$AUC
t=1095    t=1825    t=3650
0.8454230 0.8285379 0.7667952 ``````

``````> compare(time_roc_res, time_roc_res2, adjusted = TRUE)
\$p_values_AUC
t=1095       t=1825      t=3650

\$Cor
[,1]      [,2]      [,3]
[1,] 1.0000000 0.6222982 0.1760154
[2,] 0.6222982 1.0000000 0.2813782
[3,] 0.1760154 0.2813782 1.0000000``````

``````plotAUCcurve(time_roc_res, conf.int=TRUE, col="red")
legend("bottomright",c("mayoscore5", "mayoscore4"), col = c("red","blue"), lty=1, lwd=2)``````
##### ROC的最佳阈值（cutoff）

``````> mayo\$mayoscore5[which.max(time_ROC_df\$TP_3year - time_ROC_df\$FP_3year)]
[1] 6.273571``````