### R code from vignette source 'multi.Rnw' ################################################### ### code chunk number 1: multi.Rnw:32-75 ################################################### require(survival) #require(Rcolorbrewer) #brewer.pal(5, "Dark2") palette(c("#000000", "#D95F02", "#1B9E77", "#7570B3", "#E7298A", "#66A61E")) options(continue=' ') # These functions are used in the document, but not discussed until the end crisk <- function(what, horizontal = TRUE, ...) { nstate <- length(what) connect <- matrix(0, nstate, nstate, dimnames=list(what, what)) connect[1,-1] <- 1 # an arrow from state 1 to each of the others if (horizontal) statefig(c(1, nstate-1), connect, ...) else statefig(matrix(c(1, nstate-1), ncol=1), connect, ...) } state3 <- function(what, horizontal=TRUE, ...) { if (length(what) != 3) stop("Should be 3 states") connect <- matrix(c(0,0,0, 1,0,0, 1,1,0), 3,3, dimnames=list(what, what)) if (horizontal) statefig(1:2, connect, ...) else statefig(matrix(1:2, ncol=1), connect, ...) } state4 <- function() { sname <- c("Entry", "CR", "Transplant", "Transplant") layout <- cbind(c(1/2, 3/4, 1/4, 3/4), c(5/6, 1/2, 1/2, 1/6)) connect <- matrix(0,4,4, dimnames=list(sname, sname)) connect[1, 2:3] <- 1 connect[2,4] <- 1 statefig(layout, connect) } state5 <- function(what, ...) { sname <- c("Entry", "CR", "Tx", "Rel", "Death") connect <- matrix(0, 5, 5, dimnames=list(sname, sname)) connect[1, -1] <- c(1,1,1, 1.4) connect[2, 3:5] <- c(1, 1.4, 1) connect[3, c(2,4,5)] <- 1 connect[4, c(3,5)] <- 1 statefig(matrix(c(1,3,1)), connect, cex=.8,...) } ################################################### ### code chunk number 2: multi.Rnw:81-82 (eval = FALSE) ################################################### ## curves <- survfit(Surv(time, status) ~ group, data=mydata) ################################################### ### code chunk number 3: simple1 ################################################### set.seed(1952) crdata <- data.frame(time=1:11, endpoint=factor(c(1,1,2,0,1,1,3,0,2,3,0), labels=c("censor", "a", "b", "c"))) tfit <- survfit(Surv(time, endpoint) ~ 1, data=crdata) dim(tfit) summary(tfit) ################################################### ### code chunk number 4: multi.Rnw:112-113 ################################################### getOption("SweaveHooks")[["fig"]]() plot(tfit, col=1:3, lwd=2, ylab="Probability in state") ################################################### ### code chunk number 5: overall ################################################### myeloid[1:5,] ################################################### ### code chunk number 6: sfit0 ################################################### getOption("SweaveHooks")[["fig"]]() sfit0 <- survfit(Surv(futime, death) ~ trt, myeloid) plot(sfit0, xscale=365.25, xaxs='r', col=1:2, lwd=2, xlab="Years post enrollment", ylab="Survival") legend(20, .4, c("Arm A", "Arm B"), col=1:2, lwd=2, bty='n') ################################################### ### code chunk number 7: data1 ################################################### data1 <- myeloid data1$crstat <- factor(with(data1, ifelse(is.na(crtime), death, 2)), labels=c("censor", "death", "CR")) data1$crtime <- with(data1, ifelse(crstat=="CR", crtime, futime)) data1$txstat <- factor(with(data1, ifelse(is.na(txtime), death, 2)), labels=c("censor", "death", "transplant")) data1$txtime <- with(data1, ifelse(txstat=="transplant", txtime, futime)) for (i in c("futime", "crtime", "txtime", "rltime")) data1[[i]] <- data1[[i]] * 12/365.25 #rescale to months ################################################### ### code chunk number 8: curve1 ################################################### getOption("SweaveHooks")[["fig"]]() sfit1 <- survfit(Surv(futime, death) ~ trt, data1) #survival sfit2 <- survfit(Surv(crtime, crstat) ~ trt, data1) # CR sfit3 <- survfit(Surv(txtime, txstat) ~ trt, data1) layout(matrix(c(1,1,1,2,3,4), 3,2), widths=2:1) oldpar <- par(mar=c(5.1, 4.1, 1.1, .1)) plot(sfit2[,2], mark.time=FALSE, fun='event', xmax=48, lty=3, lwd=2, col=1:2, xaxt='n', xlab="Months post enrollment", ylab="Events") lines(sfit1, mark.time=FALSE, xmax=48, fun='event', col=1:2, lwd=2) lines(sfit3[,2], mark.time=FALSE, xmax=48, fun='event', col=1:2, lty=2, lwd=2) xtime <- c(0, 6, 12, 24, 36, 48) axis(1, xtime, xtime) #marks every year rather than 10 months temp <- outer(c("A", "B"), c("death", "transplant", "CR"), paste) temp[7] <- "" legend(25, .3, temp[c(1,2,7,3,4,7,5,6,7)], lty=c(1,1,1, 2,2,2 ,3,3,3), col=c(1,2,0), bty='n', lwd=2) abline(v=2, lty=2, col=3) # add the state space diagrams par(mar=c(4,.1,1,1)) crisk(c("Entry","Death", "CR"), alty=3) crisk(c("Entry","Death", "Tx"), alty=2) crisk(c("Entry","Death")) par(oldpar) ################################################### ### code chunk number 9: badfit ################################################### getOption("SweaveHooks")[["fig"]]() badfit <- survfit(Surv(txtime, txstat=="transplant") ~ trt, data1) layout(matrix(c(1,1,1,2,3,4), 3,2), widths=2:1) oldpar <- par(mar=c(5.1, 4.1, 1.1, .1)) plot(badfit, fun="event", xmax=48, xaxt='n', col=1:2, lty=2, lwd=2, xlab="Months from enrollment", ylab="P(state)") axis(1, xtime, xtime) lines(sfit3[,2], fun='event', xmax=48, col=1:2, lwd=2) legend(24, .3, c("Arm A", "Arm B"), lty=1, lwd=2, col=1:2, bty='n', cex=1.2) par(mar=c(4,.1,1,1)) crisk(c("Entry", "transplant"), alty=2, cex=1.2) crisk(c("Entry","transplant", "Death"), cex=1.2) par(oldpar) ################################################### ### code chunk number 10: