楼主: moonstone
17770 11

[程序分享] R进行生存资料分析学习笔记 [推广有奖]

讲师

74%

还不是VIP/贵宾

-

威望
0
论坛币
10442 个
通用积分
336.6055
学术水平
160 点
热心指数
169 点
信用等级
124 点
经验
263336 点
帖子
237
精华
1
在线时间
520 小时
注册时间
2007-4-27
最后登录
2024-4-11

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
与SAS 的proc lifetest及proc phreg相比,R进行生存资料分析的相对优势体现在:

1、能够更加方便快捷绘制Kaplan-Meier曲线,
2、能够相对更加美观地绘制Kaplan-Meier曲线
3、相对容易给出中位生存时间及其置信区间
4、检测比例风险假设的方法更加灵活且完善
5、易于构建时间依赖模型


以下是R绘制的Kaplan-Meier曲线图形示例:
Rplot.jpeg

以下是学习笔记的编码,供参考,欢迎交流:
  1. library(stats)
  2. library(survival)

  3. ## Information of data

  4. data(package = "survival")  # List datasets in survival package
  5. help(bladder1)              # Description of data
  6. head(bladder1)              # Show first 6 rows
  7. str(bladder1)               # Check type of variables
  8. summary(bladder1)           # Statistical summary

  9. ## Get the final data with nonzero follow-up
  10. bladder1$time <-
  11.   as.numeric(bladder1$stop -
  12.                bladder1$start)
  13. summary(bladder1$time)
  14. bladder1 <- subset(bladder1,status<=1 & time>0)


  15. ## Step1 Create Kaplan-Meier curve and estimate median survial/event time
  16. ## The "log-log" confidence interval is preferred.
  17. ## Create overval Kaplan-Meier curve
  18. km.as.one <- survfit(Surv(time, status) ~ 1, data = bladder1,
  19.                      conf.type = "log-log")

  20. ## Create Kaplan-Meier curve stratified by treatment
  21. km.by.trt <- survfit(Surv(time, status) ~ treatment, data = bladder1,
  22.                      conf.type = "log-log")

  23. ## Show simple statistics of Kaplan-Meier curve
  24. km.as.one
  25. km.by.trt

  26. ## See survival estimates at given time (lots of outputs)
  27. summary(km.as.one)
  28. summary(km.by.trt)

  29. ## Plot Kaplan-Meier curve without any specification
  30. plot(km.as.one)
  31. plot(km.by.trt)

  32. ## Plot Kaplan-Meier curve Without confidence interval and mark of event
  33. plot(km.as.one, conf = F, mark.time = F)
  34. plot(km.by.trt, conf = F, mark.time = F)


  35. ## step2 Create a simple cox regression and estimate HR:
  36. model1 <-coxph(Surv(time, status) ~ treatment + number +size,
  37.                data=bladder1)

  38. ## Model output
  39. summary(model1)                    # Output summary information
  40. confint(model1)                    # Output 95% CI for the coefficients
  41. exp(coef(model1))                  # Output HR (exponentiated coefficients)
  42. exp(confint(model1))               # 95% CI for exponentiated coefficients
  43. predict(model1, type="risk")       # predicted values
  44. residuals(model1, type="deviance") # residuals


  45. ## Step3 Check for violation of proportional hazard (constant HR over time)
  46. model1.zph <- cox.zph(model1)
  47. model1.zph                    
  48. ## Note: p value of treatmentthiotepa <0.05
  49. ## GLOBAL p value is more important than p values of treatmentthiotepa
  50. ## Only GLOBAL p value <0.05 is neccesary for solution of violation
  51. ## Following steps are just for providing examples

  52. ## Displays a graph of the scaled Schoenfeld residuals, along with a smooth curve.
  53. plot(model1.zph)

  54. ## Step4 Solution of violating of proportional hazard

  55. ## Solution 1 Stratify: create a stratified cox model,namely "contitional cox model "
  56. model2 <-coxph(Surv(start, stop, status) ~ number +size + strata (treatment),
  57.                data=bladder1)
  58. summary(model2)
  59. model2.zph <- cox.zph(model2)
  60. model2.zph

  61. ## Solution 1 Create a simple time-dependent model without time-dependent variable
  62. model3 <-coxph(Surv(start, stop, status) ~ number +size + treatment,
  63.                data=bladder1)
  64. summary(model3)

  65. ## Create a time-dependent model with time-dependent variable
  66. bladder1$thiotepa <-
  67.   as.numeric (bladder1$treatment=="thiotepa")
  68. model4 <-coxph(Surv(start, stop, status) ~ number +size + treatment
  69.                + thiotepa:time,     ## Note: "thiotepa:time" but not "thiotepa*time"
  70.                data=bladder1)
  71. summary(model4)

  72. ## Step5 Performs stepwise model selection by AIC
  73. model5 <-step(model4)  ## Default direction is "backward"
  74. summary(model5)


  75. ## rms::survplot() function
  76. ## Load rms package
  77. library(rms)

  78. ## Plot Kaplan-Meier curve without any specification
  79. fit1 <- npsurv(Surv(time, status) ~ 1, data = bladder1)
  80. fit2 <- npsurv(Surv(time, status) ~ treatment, data = bladder1)
  81. survplot(fit1)
  82. survplot(fit2)

  83. ## Plot Kaplan-Meier curve Without confidence interval
  84. survplot(fit1, conf = "none")
  85. survplot(fit2, conf = "none")

  86. ## More options for Kaplan-Meier curve
  87. survplot(fit2,
  88.          xlab = "Time, month",                  # add x-axis label
  89.          ylab = "Survival probability, %",      # add y-axis label
  90.          ## xlim=c(0,60),                       # add x-axis limits
  91.          ## ylim=c(-0.1,1),                     # add y-axis limits
  92.          ## conf.int=.95,                       # show 95% CI,
  93.          conf='none',                           # change type of CI
  94.          label.curves = list(keys = "lines"),   # legend instead of direct label
  95.          levels.only  = TRUE,                   # show only levels, no label
  96.          col=c('red','green','blue'),           # change legend color
  97.          ## fun = function(x) {1 - x},          # Cumulative probability plot
  98.          ## loglog   = TRUE,                    # log(-log Survival) plot
  99.          ## logt     = TRUE,                    # log time
  100.          time.inc = 5,                          # time increment
  101.          ## dots     = TRUE,                    # dot grid
  102.          n.risk   = TRUE,                       # show number at risk
  103.          y.n.risk = 0.01,                       # Change position of number at risk
  104.          cex.n.risk = 0.6                       # change character size for number at risk
  105.          )
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:资料分析 学习笔记 分析学 习笔记 information 资料

已有 2 人评分学术水平 热心指数 信用等级 收起 理由
东北小月 + 1 + 1 + 1 精彩帖子
smm1988 + 1 + 1 + 1 精彩帖子

总评分: 学术水平 + 2  热心指数 + 2  信用等级 + 2   查看全部评分

沙发
尚目目 发表于 2016-12-9 13:16:52 |只看作者 |坛友微信交流群
竟然可没有点赞么?这个小教程写得真心不错

使用道具

藤椅
oscarwanglalaal 发表于 2017-2-24 12:56:35 |只看作者 |坛友微信交流群
太厉害了!

使用道具

板凳
纪元之子 发表于 2017-2-24 22:30:34 |只看作者 |坛友微信交流群
谢谢楼主 非常厉害 能否将上图的原始数据发一份 弄不清楚代码里面的哪些参数是指X 哪些是生存事件变量?想运用这些代码去做别的分析,谢谢!

使用道具

报纸
lonestone 在职认证  发表于 2017-2-25 07:16:32 来自手机 |只看作者 |坛友微信交流群
moonstone 发表于 2015-12-11 21:32
与SAS 的proc lifetest及proc phreg相比,R进行生存资料分析的相对优势体现在:

1、能够更加方便快捷绘制 ...
谢谢你

使用道具

地板
ayzzya 发表于 2017-2-25 11:15:46 |只看作者 |坛友微信交流群
支持楼主,互相学习!

使用道具

7
teresa_ya 发表于 2017-3-20 15:51:03 |只看作者 |坛友微信交流群
谢谢楼主!想请教一下,我在编写程序时,用了conf.type="log-log",为什么出来的图像只是普通的曲线而不是log-log plot呢?

使用道具

8
lanhong1993 发表于 2017-4-18 10:42:04 |只看作者 |坛友微信交流群
> plot(km.as.one, conf = F, mark.time = F)
Error in plot.survfit(km.as.one, conf = F, mark.time = F) :
  argument 2 matches multiple formal arguments

此步骤运行错误,问题出在哪儿?

使用道具

9
pingguzh 发表于 2017-4-19 11:29:28 |只看作者 |坛友微信交流群
survplot和plot在使用上哪个更好呢?
可信区间如何去掉的呢?
legend是怎么加进去的呢

使用道具

10
东北小月 发表于 2018-10-31 16:58:27 |只看作者 |坛友微信交流群
lanhong1993 发表于 2017-4-18 10:42
> plot(km.as.one, conf = F, mark.time = F)
Error in plot.survfit(km.as.one, conf = F, mark.time = F ...
因为包升级了,conf选项变成其他的,改成conf.int = F就好啦

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-5-2 01:07