{cat("--------------- Survival Analysis ---------------\n");T}
{
# Functions: survfit, summary.survfit, Surv
# Data: Miller, AML maintenance study p. 49-50; included with Splus as 
#      the data frame leukemia
# Reference(s): Miller, R.G. 1981. Survival Analysis. John Wiley
#      and Sons.
#
#      Embury, S.H., Elias, L., P.H. Heller, Hood, C.E.,
#      Greenberg, P.L. and S.L. Schrier. 1977. Remission
#      Maintenance Therapy in Acute Myelogenous Leukemia.
#      Western Journal of Medicine. 126:267-272.
# Description: compute Kaplan-Meier estimators; check the KM estimate
#      for the maintained group against results on p. 50
tol <- 4.1e-3
fit <- survfit(Surv(time,status) ~ group, data=leukemia)
all(abs(summary(fit)$surv[1:7] - c(0.91,0.82,0.72,0.61,0.49,0.37,0.18)) < tol)
}
{
# Functions: survreg, Surv
# Data: Kalbfleisch and Prentice, Table 1.1 p. 2
# References: Kalbfleisch, J.D. and R.L. Prentice. 1980. The Statistical 
#      Analysis of Failure Time Data. John Wiley and Sons.
#
#      Pike, M.C. 1966. A Method of Analysis of Certain Class of
#      Experiments in Carcinogenesis. Biometrics. 22:142-161.
# Description: data consist of the times from insult with the carcinogen
#      DMBA to mortality from vaginal cancer in rats; perform a
#      log-normal regression on survival beyond 100 days; check parameter 
#      estimates, dispersion and  maximized log-likelihood against
#      results in KP, p. 57
tol <- 1.5e-3
cancer.df <- data.frame(
	days=c(143,190,213,230,304,156,232,233,261,296,164,192,216,234,216,
	  163,232,233,280,323,188,206,220,246,244,198,233,239,280,204,188,
	  209,227,265,142,205,233,240,296,344),
	status=c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,0,1,1,1,1,0,
            1,1,1,1,1,1,1,1,1,0),
	group=c(0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,
           0,0,0,0,1,1,1,1,1,1))
cancer.surv <- Surv(cancer.df$days-100,cancer.df$status)
fit <- survReg(cancer.surv ~ cancer.df$group, dist="lognormal")
all(c(abs(fit$coefficients - c(4.73,0.154)) < tol,
      abs(fit$scale - 0.416) < tol,
      abs(fit$loglik[2] + 194.2059) < tol))
}
{
# Functions: survreg, Surv
# Data: Kalbfleisch and Prentice, Table 1.1 p. 2
# References: Kalbfleisch, J.D. and R.L. Prentice. 1980. The Statistical
#      Analysis of Failure Time Data. John Wiley and Sons.
#
#      Pike, M.C. 1966. A Method of Analysis of Certain Class of
#      Experiments in Carcinogenesis. Biometrics. 22:142-161.
# Description: mortality from cancer in rats defined in the previous test; 
#      use a Weibull regression model for survival beyond 100 days; 
#      check parameter estimates, dispersion and maximized 
#      log-likelihood against results in KP, p. 57
tol <- 3.5e-3
fit <- survReg(cancer.surv ~ cancer.df$group)
all(c(abs(fit$coefficients - c(4.87,0.213)) < tol,
      abs(fit$scale - 0.323) < tol,
      abs(fit$loglik[2] + 191.9335) < tol))
}
{
# Functions: survreg, Surv
# Data: Kalbfleisch and Prentice, Table 1.1 p. 2
# References: Kalbfleisch, J.D. and R.L. Prentice. 1980. The Statistical
#      Analysis of Failure Time Data. John Wiley and Sons.
#
#      Pike, M.C. 1966. A Method of Analysis of Certain Class of
#      Experiments in Carcinogenesis. Biometrics. 22:142-161.
# Description: mortality from cancer in rats used in the previous 2 tests; 
#      use an exponential regression model for survival beyond 100 days; 
#      check parameter estimates and maximized 
#      log-likelihood against results in KP, p. 57
tol <- 1.6e-4
fit <- survReg(cancer.surv ~ cancer.df$group, dist="exponential")
all(c(abs(fit$coefficients - c(4.8607,0.1752)) < tol,
      abs(fit$loglik[2] + 214.3149) < tol))
}
{
# Functions: coxph, Surv
# Data: Kalbfleisch and Prentice, Table 1.1 p. 2
# References: Kalbfleisch, J.D. and R.L. Prentice. 1980. The Statistical 
#      Analysis of Failure Time Data. John Wiley and Sons.
#
#      Pike, M.C. 1966. A Method of Analysis of Certain Class of
#      Experiments in Carcinogenesis. Biometrics. 22:142-161.
#
#      SAS Institute Inc., SAS Technical Report P-217, SAS/STAT Software:
#      The PHREG Procedure, Version 6, Cary, NC: SAS Institute Inc.,
#      1991. 63 pp.
# Description: mortality from cancer in rats defined in the previous
#      3 tests; use a cox proportional hazard model by treatment group;
#      check coefficient, standard error, and hypothesis test results
#      for Ho: B=0; compare with results in the SAS TR P-217, p. 5 
tol <- 4.5e-4
cancer.surv <- Surv(cancer.df$days,cancer.df$status)
fit <- coxph(cancer.surv ~ cancer.df$group,method="breslow")
logtest <- -2 * (fit$loglik[1] - fit$loglik[2])
waldtest <- sum(fit$coef * solve(fit$var,fit$coef))
all(c(abs(fit$coefficients + 0.595896) < tol,
      abs(sqrt(diag(fit$var)) - 0.34840) < tol,
      abs((-2 * (fit$loglik)) - c(204.317,201.438)) < tol,
      abs(logtest - 2.878) < tol,
      abs((1 - pchisq(logtest,1)) - 0.0898) < tol,
      abs(waldtest - 2.925) < tol,
      abs((1 - pchisq(waldtest,1)) - 0.0872) < tol,
      abs(fit$score - 3.000) < tol,
      abs((1 - pchisq(fit$score,1)) - 0.0833) < tol,
      abs(exp(fit$coef) - 0.551) < tol))
}
{
# Function: survdiff
# Data: Kalbfleisch and Prentice, Table 1.1 p. 2
# References: Kalbfleisch, J.D. and R.L. Prentice. 1980. The Statistical
#      Analysis of Failure Time Data. John Wiley and Sons.
# 
#      Pike, M.C. 1966. A Method of Analysis of Certain Class of
#      Experiments in Carcinogenesis. Biometrics. 22:142-161.
# 
#      SAS Institute Inc., SAS/STAT User's Guide, Version 6, Fourth
#      Edition, Volume 2, Cary, NC: SAS Institute Inc., 1989. 846 pp.
# Description: mortality from cancer in rats defined in the previous
#      4 tests; compute log-rank test for homogeneity of survival
#      estimates by treatment group; check test statistic and p-value
#      with results in KP, p.18 and the SAS User's Guide, Vol. 2, p. 1034
tol1 <- 2.8e-3
tol2 <- 1e-5
s.diff1 <- survdiff(cancer.surv ~ cancer.df$group)
all(c(abs(s.diff1$chisq - 3.12) < tol1,
      abs((1 - pchisq(s.diff1$chisq,1)) - 0.0772) < tol2))
}
{
# Functions: coxph, Surv
# Data: Wei, Lin and Weissfeld, study on time to recurrence of bladder
#      cancer; data included as part of Splus in the data frame bladder
# Reference: Wei, L.J., Lin D.Y., and L. Weissfeld. 1989. Regression Analysis
#      of Multivariate Incomplete Failure Time Data by Modeling 
#      Marginal Distributions. Journal of the American Statistical
#      Association, 84(408) 1065-1073.
# Description: multivariate failure time data that includes repeated 
#      failures defined as multiple tumor recurrences; use Cox
#      proportional hazards model to model the marginal distribution
#      of each failure time variable; check coefficient estimates
#      for treatments and corresponding covariance matrix to results
#      given in (3.2) and Table 5
tol <- 6e-4
old.op <- options(warn=-1, contrasts=c("contr.treatment", "contr.poly"),
	TEMPORARY=T)
wfit <- coxph(Surv(stop, event) ~ (rx + size + number)* strata(enum) +
                 cluster(id), bladder, method="breslow")
options(old.op, TEMPORARY=T)
rx.coef <- c(1,4,5,6)
contrast.mat <- diag(4)
contrast.mat[1,] <- 1
reg.coef <- wfit$coef[rx.coef] %*% contrast.mat
var.mat <- t(contrast.mat) %*% wfit$var[rx.coef,rx.coef] %*% contrast.mat
all(c(abs(reg.coef - c(-0.518,-0.619,-0.700,-0.651)) < tol,
      abs(var.mat[1,1] - 0.095) < tol,
      abs(var.mat[1:2,2] - c(0.060,0.132)) < tol,
      abs(var.mat[1:3,3] - c(0.057,0.130,0.172)) < tol,
      abs(var.mat[1:4,4] - c(0.044,0.116,0.159,0.240)) < tol))
}
{
# Functions: coxph, Surv
# Data: Wei, Lin and Weissfeld, study on time to recurrence of bladder
#      cancer; data included as part of Splus in the data frame bladder
# Reference: Wei, L.J., Lin D.Y., and L. Weissfeld. 1989. Regression
#      Analysis of Multivariate Incomplete Failure Time Data by Modeling
#      Marginal Distributions. Journal of the American Statistical
#      Association, 84(408) 1065-1073.
# Description: implementing an Andersen-Gill fit on bladder data introduced
#      in previous test; check coefficient estimate for treatment 
#      effect with result given in Table 5
tol <- 9.8e-5
bladder.sub <- bladder[bladder$start < bladder$stop,]
fit <- coxph(Surv(start, stop, event) ~ rx + size + number + cluster(id),
                  bladder.sub, method="breslow")
abs(fit$coef[1] + 0.407) < tol
}
{
# Clean up after survival
rm(tol,fit,cancer.df,cancer.surv,logtest,waldtest)
rm(tol1,tol2,s.diff1,old.op,wfit,rx.coef,contrast.mat,reg.coef,var.mat)
rm(bladder.sub)
T
}
