获取更多R语言知识,请关注公众号:医学和生信笔记

医学和生信笔记 公众号主要分享:1.医学小知识、肛肠科小知识;2.R语言和Python相关的数据分析、可视化、机器学习等;3.生物信息学学习资料和自己的学习笔记!

多时间点ROC

加载R包和数据

  1. rm(list = ls())
  2. library(timeROC)
  3. library(survival)
  4. load(file = "../000files/timeROC.RData")

多个时间点ROC

首先看一下数据结构,对于多个时间点的ROC,需要3列数据:time, event, marker(比如你计算得到的risk score)

  1. # 看一下画图所需的数据长什么样子,event这一列,0代表living,1代表dead,futime这一列单位是年,也可以改成
  2. # 其他的
  3. knitr::kable(df[1:10,])
event riskScore futime
0 -0.2493257 3.0275000
0 -0.5111057 1.1558333
1 -0.2113056 1.8191667
0 -0.4270568 1.5166667
0 0.2785857 1.3441667
1 -0.0067608 0.0500000
0 -0.3104566 2.3108333
0 -0.3660733 1.0225000
0 -0.2568007 7.5083333
0 -0.0232976 0.3308333
  1. str(df)
  2. ## 'data.frame': 297 obs. of 3 variables:
  3. ## $ event : num 0 0 1 0 0 1 0 0 0 0 ...
  4. ## $ riskScore: num -0.249 -0.511 -0.211 -0.427 0.279 ...
  5. ## $ futime : num 3.03 1.16 1.82 1.52 1.34 ...
  1. # 下面是画图代码
  2. ROC <- timeROC(T=df$futime,
  3. delta=df$event,
  4. marker=df$riskScore,
  5. cause=1, #阳性结局指标数值
  6. weighting="marginal", #计算方法,默认为marginal
  7. times=c(1, 2, 3), #时间点,选取1年,3年和5年的生存率
  8. iid=TRUE)
  9. ROC #查看模型变量信息
  10. ## Time-dependent-Roc curve estimated using IPCW (n=297, without competing risks).
  11. ## Cases Survivors Censored AUC (%) se
  12. ## t=1 57 203 37 71.02 3.68
  13. ## t=2 66 106 125 69.23 3.94
  14. ## t=3 68 74 155 65.53 4.85
  15. ##
  16. ## Method used for estimating IPCW:marginal
  17. ##
  18. ## Total computation time : 0.19 secs.
  19. # tiff("figures/time_roc.tiff",width = 12,height = 12,units = "cm",compression = "lzw", res = 500)
  20. plot(ROC,
  21. time=1, col="red", lwd=2, title = "") #time是时间点,col是线条颜色
  22. plot(ROC,
  23. time=2, col="blue", add=TRUE, lwd=2) #add指是否添加在上一张图中
  24. plot(ROC,
  25. time=3, col="orange", add=TRUE, lwd=2)
  26. #添加标签信息
  27. legend("bottomright",
  28. c(paste0("AUC at 1 year: ",round(ROC[["AUC"]][1],2)),
  29. paste0("AUC at 2 year: ",round(ROC[["AUC"]][2],2)),
  30. paste0("AUC at 3 year: ",round(ROC[["AUC"]][3],2))),
  31. col=c("red", "blue", "orange"),
  32. lty=1, lwd=2,bty = "n")

image.png

  1. # dev.off()

多指标ROC

首先也是看一下所需要的数据结构,其中futime和event是必须的,另外的几列是你想要用来画ROC曲线图的指标,可以自己添加,在这里我使用了riskScore, gender, TNM分期。 在gender这一列,1是female,2是male,t,n,m这3列,数字代表不同的分期

  1. knitr::kable(df2[1:10,])
event age riskScore futime gender t n m
0 59 -0.2493257 3.0275000 2 4 1 1
0 63 -0.5111057 1.1558333 2 4 5 1
1 65 -0.2113056 1.8191667 2 4 1 1
0 73 -0.4270568 1.5166667 1 3 1 1
0 59 0.2785857 1.3441667 2 3 1 1
1 66 -0.0067608 0.0500000 2 3 1 3
0 56 -0.3104566 2.3108333 1 5 3 1
0 42 -0.3660733 1.0225000 2 3 1 1
0 61 -0.2568007 7.5083333 2 NA NA 3
0 48 -0.0232976 0.3308333 2 4 1 3
  1. str(df2)
  2. ## 'data.frame': 297 obs. of 8 variables:
  3. ## $ event : num 0 0 1 0 0 1 0 0 0 0 ...
  4. ## $ age : int 59 63 65 73 59 66 56 42 61 48 ...
  5. ## $ riskScore: num -0.249 -0.511 -0.211 -0.427 0.279 ...
  6. ## $ futime : num 3.03 1.16 1.82 1.52 1.34 ...
  7. ## $ gender : num 2 2 2 1 2 2 1 2 2 2 ...
  8. ## $ t : num 4 4 4 3 3 3 5 3 NA 4 ...
  9. ## $ n : num 1 5 1 1 1 1 3 1 NA 1 ...
  10. ## $ m : num 1 1 1 1 1 3 1 1 3 3 ...
  1. # riskScore的ROC曲线
  2. ROC.risk <- timeROC(T=df2$futime,
  3. delta=df2$event,
  4. marker=df2$riskScore,
  5. cause=1,
  6. weighting="marginal",
  7. times=3,
  8. iid=TRUE)
  9. # gender的ROC曲线
  10. ROC.gender <- timeROC(T=df2$futime,
  11. delta=df2$event,
  12. marker=df2$gender,
  13. cause=1,
  14. weighting="marginal",
  15. times=3,
  16. iid=TRUE)
  17. # age的ROC曲线
  18. ROC.age <- timeROC(T=df2$futime,
  19. delta=df2$event,
  20. marker=df2$age,
  21. cause=1,
  22. weighting="marginal",
  23. times=3,
  24. iid=TRUE)
  25. # T分期的ROC曲线
  26. ROC.T <- timeROC(T=df2$futime,
  27. delta=df2$event,
  28. marker=df2$t,
  29. cause=1,
  30. weighting="marginal",
  31. times=3,
  32. iid=TRUE)
  33. # N分期的ROC曲线
  34. ROC.N <- timeROC(T=df2$futime,
  35. delta=df2$event,
  36. marker=df2$n,
  37. cause=1,
  38. weighting="marginal",
  39. times=3,
  40. iid=TRUE)
  41. # M分期的ROC曲线
  42. ROC.M <- timeROC(T=df2$futime,
  43. delta=df2$event,
  44. marker=df2$m,
  45. cause=1,
  46. weighting="marginal",
  47. times=3,
  48. iid=TRUE)
  49. # tiff("figures/多指标ROC.tiff",width = 12,height = 12,units = "cm",compression = "lzw", res = 500)
  50. plot(ROC.risk, time = 3, col="#E41A1C", lwd=2, title = "")
  51. plot(ROC.gender, time = 3, col="#A65628", lwd=2, add = T)
  52. plot(ROC.age, time = 3, col="#4DAF4A", lwd=2, add = T)
  53. plot(ROC.T, time = 3, col="#377EB8", lwd=2, add = T)
  54. plot(ROC.N, time = 3, col="#984EA3", lwd=2, add = T)
  55. plot(ROC.M, time = 3, col="#FFFF33", lwd=2, add = T)
  56. legend("bottomright",
  57. c(paste0("Risk score: ",round(ROC.risk[["AUC"]][2],2)),
  58. paste0("gender: ",round(ROC.gender[["AUC"]][2],2)),
  59. paste0("age: ",round(ROC.age[["AUC"]][2],2)),
  60. paste0("T: ",round(ROC.T[["AUC"]][2],2)),
  61. paste0("N: ",round(ROC.N[["AUC"]][2],2)),
  62. paste0("M: ",round(ROC.M[["AUC"]][2],2))
  63. ),
  64. col=c("#E41A1C", "#A65628", "#4DAF4A","#377EB8","#984EA3","#FFFF33"),
  65. lty=1, lwd=2,bty = "n")

image.png

  1. # dev.off()
  2. # "#E41A1C" "#377EB8" "#4DAF4A" "#984EA3" "#FF7F00" "#FFFF33" "#A65628" "#F781BF"

获取更多R语言知识,请关注公众号:医学和生信笔记

医学和生信笔记 公众号主要分享:1.医学小知识、肛肠科小知识;2.R语言和Python相关的数据分析、可视化、机器学习等;3.生物信息学学习资料和自己的学习笔记!