# Load data heart <- read.table("http://publicifsv.sund.ku.dk/~pka/epidata/stanford.txt", header = TRUE) library(survival) # dummy variables for 3 age groups heart$age1 <- 0 heart$age2 <- 0 heart$age3 <- 0 heart$age1[heart$age <= 40] <- 1 heart$age2[40 < heart$age & heart$age <= 50] <- 1 heart$age3[heart$age > 50] <-1 # 1.: model with age categories fit <- coxph(Surv(days, cens) ~age2 + age3, data = heart) summary(fit) # 2.: time-fixed 'effect' of trans fit2 <- coxph(Surv(days, cens) ~trans, data = heart) summary(fit2) # 3.: time-dep. effect of trans # create new dataset including information on transplantation notrans <- heart[heart$wait <0, ] notrans$entry <- 0 notrans$exit <- notrans$days notrans$death <- notrans$cens notrans$xstatus <- 0 trans <- heart[heart$wait> 0, ] trans$entry <- 0 trans$exit <- trans$wait trans$death <- 0 trans$xstatus <- 0 trans2 <- heart[heart$wait> 0, ] trans2$entry <- trans2$wait trans2$exit <- trans2$days trans2$death <- trans2$cens trans2$xstatus <- 1 # merge the datasets updated <- rbind(notrans, trans, trans2) fit3 <- coxph(Surv(entry, exit, death) ~ xstatus, data = updated) summary(fit3) # 4.: include age categories fit4 <- coxph(Surv(entry, exit, death) ~ xstatus + age2 + age3, data = updated) summary(fit4) # 5. age as time variable updated$aentry <- updated$age updated$aexit <- updated$age + updated$exit/365 fit5 <- coxph(Surv(aentry, aexit, death) ~ xstatus, data = updated) summary(fit5) # 6. (linear) adjustment for follow-up time updated$fup <- updated$aexit - updated$aentry fit6 <- coxph(Surv(aentry, aexit, death) ~ xstatus + fup, data = updated) summary(fit6)