资源描述:
《用r作生存分析》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、生存分析一.Librariesanddatasetslibrary()library(survival)library(help=survival)data(aml)ami#查看你的系统中所有己装的libraries#加载一个library,也口J使用鼠标来做#seethelistofavailablefunctionsanddatasets.#加载数据ami#seethedata#《生存分析》需要的两个librarieslibrary(survival)#包含我们教材中所有的数据library(KMsurv)#包"#若想使数据中的变量能够在R
2、中作为一个变量使用,useattach():>attach(aids)>infect>detach(aids)—.SurvivalObjects#由函数Surv产生一个survivalobjectSurv(time,time2,event,type)•time:survivaltime•time2:endingtimeifitisintervalcensored•event:censoringvariable.Forintervalcensoreddata,O=rightcensored,l=eventattime,2=leftcensored
3、,3=intervalcensored.•type:indicatingwhetheritisrightcensored,leftcensoredorintervalcensoredorcountingprocess.默认值是右删失或countingprocess#假设一个观测值为[2,3],则R语句为Surv(time=2,time2=3,event=3,type="interval")#右删失数据•attach(aml)•Surv(time,status)•detach(aml)#左截断右删失数据•data(psych)•attach(ps
4、ych)•my.surv.object<-Surv(age,age+time,death)•my.surv.object[1](51,52](58,59](55,57](28,50](21,51+](19,47](25,57][22](29,63+](35,65+](32,67](36,76](32,71+]•detach(psych)三TheKaplan-MeierEstimates#计算生存曲线:survfit,有三个重要的参数:formula,conf.int,andconf.type.survfit(formula,conf.int=0
5、.95,conf.type="log")conf.type二"plai#linearconfideneeintervalinourbookconf.type二〃log〃#对H(t)作logtransformationconf.type二"logJog"#log-transformedinourbook#在新版的R里,这个"是必要的。#在新版的R里,这个T是必要的。myfit=survfit(Surv(time,status)~l,data=kidney)attach(kidney)myfit=survfit(Surv(time,status)~
6、1)a=summary(survfit(Surv(time,status)^!))a$surva$timea$n.riska$n.event#outputstheKaplarvMeierestimateateacht_i#{t_i}"#{YJ}#{dj}#提取survfit产生的结果summary(myfit)$std.errsummary(myfit)$lowersummary(myfit)$upperb=summary(survfit(Surv(time/status)/x/l),censored=TRUE)#standarderrorof
7、theK-Mestimateat{t_i}#lowerpointwiseestimates#upperpointwiseestimates画出生存曲线plot(myfit,main二"Kaplan-Meierestimatewith95%confideneeboundsn,xlab=,,time,,/ylab二"survivalfunction”)par(mfcol=c(2,l))plot(myfit,conf,int=Fzmain=nKaplan・Meierestimatewith95%confideneebounds11,xlab=ntim
8、e,f,ylab=HsurvivalfunctiorT)plot(survfit(Surv(time,status)~1,conf.int=0.99)