Wednesday, April 5, 2017

R code - Quick Kaplan-Meier

This give a nice Kaplan-Meier with graphically illustrated confidence intervals

Fire up the relevant packages in R:

> install.packages('survminer')
> library(survminer)
> library(survival)

Then go to excel and make and copy to clipboard a table of this format:

times
censor
therapy
111
0
Drug1
88
1
Drug2
221
1
Drug1
49
1
Drug2
303
0
Drug1
44
1
Drug2
602
1
Drug1
79
1
Drug2

(censor is 0 for right censored, 1 for event)

Then paste the data into an R data frame:

If mac:
> dat4km<-read.table(pipe("pbpaste"),sep="\t",header=T)

If PC:

>dat4km <- read.table(file = "clipboard", sep = "t", header=TRUE)


then create the survival model and plot it:

> fit4km<-(survfit(Surv(times,censor)~therapy, data=dat4km))

> ggsurvplot(fit4km, risk.table = TRUE,pval=TRUE, conf.int=TRUE)

1 comment:

  1. http://www.sthda.com/english/rpkgs/survminer/reference/ggsurvplot.html

    ReplyDelete