## simulate right censored data from a two state model
dat <- data.frame(time=rexp(100),status=rbinom(100,1,.3),X=rbinom(100,1,.5),Z=rnorm(100,10,3),patnr=sample(1:10,size=100,replace=TRUE))
with(dat,plot(Hist(time,status)))
### marginal Kaplan-Meier estimator
kmfit <- prodlim(Hist(time, status) ~ 1, data = dat)
plot(kmfit)
plot(kmfit,percent=TRUE)
plot(kmfit,percent=TRUE,axis1.at=seq(0,kmfit$maxtime,365.25/2),axis1.labels=seq(0,kmfit$maxtime/365.25,1/2),xlab="Years",axis2.las=2,atrisk.label="Patients")
plot(kmfit,confint.citype="shadow",col=4,background=TRUE,background.fg="yellow",background.horizontal=seq(0,1,.25/2),background.vertical=seq(0,kmfit$maxtime,1),background.bg=c("gray88"))
plot(kmfit,confint.citype="dots",col=4,background=TRUE,background.bg=c("white","gray88"),background.fg="gray77",background.horizontal=seq(0,1,.25/2),background.vertical=seq(0,kmfit$maxtime,1),background.bg=c("gray88","gray88"))
### Kaplan-Meier in discrete strata
kmfitX <- prodlim(Hist(time, status) ~ X, data = dat)
plot(kmfitX)
plot(kmfitX,legend.x="bottomleft",atRisk.cex=1.3)
## add the log-rank test p-value
library(survival)
library(Publish) ## get it on R-forge
lr.pval = attr(publish(survdiff(Surv(time,status)~X,data=dat),verbose=FALSE),"p-value")
lr.pval = format.pval(lr.pval,digits=4,eps=0.0001)
plot(kmfitX)
legend(x="bottomleft",title="Log-rank test",legend=paste("p=",lr.pval))
### Kaplan-Meier in continuous strata
kmfitZ <- prodlim(Hist(time, status) ~ Z, data = dat)
plot(kmfitZ,newdata=data.frame(Z=c(5,7,12)))
### Cluster-correlated data
library(survival)
kmfitC <- prodlim(Hist(time, status) ~ cluster(patnr), data = dat)
plot(kmfitC,atrisk.labels=c("Units","Patients"))
## simulate right censored data from a competing risk model
datCR <- data.frame(time=rexp(100),status=rbinom(100,2,.3),X=rbinom(100,1,.5),Z=rnorm(100,10,3))
with(datCR,plot(Hist(time,status)))
### marginal Aalen-Johansen estimator
ajfit <- prodlim(Hist(time, status) ~ 1, data = datCR)
plot(ajfit)
### conditional Aalen-Johansen estimator
ajfitXZ <- prodlim(Hist(time, status) ~ X+Z, data = datCR)
plot(ajfitXZ,newdata=data.frame(X=c(1,1,0),Z=c(4,10,10)))
plot(ajfitXZ,newdata=data.frame(X=c(1,1,0),Z=c(4,10,10)),cause=2)
Run the code above in your browser using DataLab