Lecture slides here. (To convert html to pdf, press E \(\to\) Print \(\to\) Destination: Save to pdf)
Base R Code
Show the code
################################################################### This code generates all numerical results in chapter 9. ####################################################################library(survival)# read in the cgd dataset in counting process formatcgd <-read.table("Data\\Chronic Granulomatous Disease Study\\cgd_counting.txt")head(cgd)# Andersen-Gill modelobj.AG <-coxph(Surv(tstart, tstop, status) ~ treat + sex + age + inherit + steroids + propylac, data = cgd)summary(obj.AG)# Frailty modelobj.frail <-coxph(Surv(tstart, tstop, status) ~ treat + sex + age + inherit + steroids + propylac +frailty(id, distribution ="gamma"), data = cgd)summary(obj.frail)# proportional mean model (LWYY)obj.pm <-coxph(Surv(tstart, tstop, status) ~ treat + sex + age + inherit + steroids + propylac +cluster(id), data = cgd)summary(obj.pm)# extract the beta's from the three modelscoeff.AG <-summary(obj.AG)$coeffcoeff.frail <-summary(obj.frail)$coeffcoeff.pm <-summary(obj.pm)$coeff########################################## Table 9.1. beta, se(beta), and p-vaues# from the three models########################################### Andersen-Gill modelc1 <- coeff.AG[,1]c2 <- coeff.AG[,3]c3 <- coeff.AG[,5]# Frailty modelc4 <- coeff.frail[1:6,1]c5 <- coeff.frail[1:6,2]c6 <- coeff.frail[1:6,6]# Proportional means modelc7 <- coeff.pm[1:6,1]c8 <- coeff.pm[1:6,4]c9 <- coeff.pm[1:6,6]#print out Table 9.1noquote(round(cbind(c1,c2,c3,c4,c5,c6,c7,c8,c9),3))### Figure 9.2 ############################################## predicted mean functions by treatment# for a female/male patient of 12 years old with X-linked # inheritance pattern and use of both steroids and # prophylactic antibiotics############################################################# get betabeta <- obj.pm$coeff# get baseline mean function mu_0(t)# and tLt <-basehaz(obj.pm,centered = F)t <- Lt$timemu0 <- Lt$hazard# covariate vector (besides treatement) for female patientzf <-c(0,12,1,1,1)# covariate vector (besides treatement) for male patientzm <-c(1,12,1,1,1)# female in treatment and controlmu.f.trt <-exp(sum(c(1,zf)*beta))*mu0mu.f.contr <-exp(sum(c(0,zf)*beta))*mu0# male in treatment and controlmu.m.trt <-exp(sum(c(1,zm)*beta))*mu0mu.m.contr <-exp(sum(c(0,zm)*beta))*mu0# Plot the figurepar(mfrow=c(1,2))# for female (left panel)plot(t/30.5, mu.f.trt, type="s",xlim=c(0, 12), ylim=c(0,6),frame.plot =F,lty=1, main="Female",xlab="Time (months)",ylab ="Mean number of infections", lwd=2)lines(t/30.5,mu.f.contr,lty=3,lwd=2)#for male (right panel)plot(t/30.5, mu.m.trt, type="s", xlim=c(0, 12), ylim=c(0,6),frame.plot =F,lty=1,main="Male",xlab="Time (months)",ylab ="Mean number of infections",lwd=2)lines(t/30.5,mu.m.contr,lty=3,lwd=2)