编辑代码

library(timeROC)
library(survival)
library(survivalROC)


#读取数据集
library(readxl)
dt <- read_excel("data_random_20241026.xlsx", sheet = "Sheet1")
#dt <- read_excel("clincialData_20240312_clinical_English_and_radio_score_val.xlsx", sheet = "Sheet1")
#dt <- read_excel("clincialData_20240312_clinical_English_and_radio_score_test.xlsx", sheet = "Sheet1")
str(dt)  ##查看每个变量结构
dt <- na.omit(dt) #按行删除缺失值

#fit <- survfit(Surv(dt$无进展时间, dt$病情转归)~TIL, data=dt)
#data(mayo)

time_roc_res <- timeROC(T = dt$OS,delta = dt$死亡,marker = dt$OS_radio_score,cause = 1,weighting="marginal",times = c(12, 24),ROC = TRUE,iid = TRUE)

#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)

time_roc_res$AUC

confint(time_roc_res, level = 0.95)$CI_AUC

plot(time_roc_res, time=12, col = "red", title = FALSE)  
plot(time_roc_res, time=24, add=TRUE, col="green") 
legend("bottomright",c("12 Months" ,"24 Months"),
       col=c("red", "green"), lty=1, lwd=2)


time_ROC_df <- data.frame(
  TP_12month = time_roc_res$TP[, 1],
  FP_12month = time_roc_res$FP[, 1],
  TP_24month = time_roc_res$TP[, 2],
  FP_24month = time_roc_res$FP[, 2]
)
library(ggplot2)
ggplot(data = time_ROC_df) +
  geom_line(aes(x = FP_12month, y = TP_12month), size = 1, color = "#0072B5FF") +
  geom_line(aes(x = FP_24month, y = TP_24month), 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.15, size = 4.5,
           label = paste0("AUC at 12 months = ", sprintf("%.3f", time_roc_res$AUC[[1]])), color = "#0072B5FF"
  ) +
  annotate("text",
           x = 0.75, y = 0.05, size = 4.5,
           label = paste0("AUC at 24 months = ", sprintf("%.3f", time_roc_res$AUC[[2]])), 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)))
  )