R version 4.2.2 Patched (2022-11-10 r83330) -- "Innocent and Trusting"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> pdf("g02.pdf");options(width=64)
> #setwd("C:\\Users\\kolassa\\Class542")
> setwd("~/Taught1/960-542/Data")
> #Effect of censoring on log rank statistic
> library(PHInfiniteEstimates)#For checkcensor
> checkcensor()
[1] 0.1922421 0.1031417
> #*******************************************************/
> # Larynx cancer data from Klelin and Moeschberger. See */
> # class web page for link.  Cancers are graded on an   */
> # integer scale, with increasing numbers indicating    */
> # more sever.  Variables are stage, time to death, year*/
> # of diagnosis, and status.                            */
> #*******************************************************/
> larynx<-read.table("larynx.txt",header=TRUE)
> #***************************************************/
> # Kaplan--Meier curve for the combined larynx data */
> #***************************************************/
> # The next command loads the appropriate package in R.
> library(survival)#For Surv
> #The inner function Surv takes a time variable and a
> #censoring variable, and sets them up as input for a
> #survival analysis.  survfit does the Kaplan-Meier.
> sf.larynx<-survfit(Surv(time,delta)~1,
+   data=larynx, conf.type="none")
> cat("Summary of survival fit\n")
Summary of survival fit
> summary(sf.larynx)
Call: survfit(formula = Surv(time, delta) ~ 1, data = larynx, conf.type = "none")

 time n.risk n.event survival std.err
  0.1     90       1    0.989  0.0110
  0.2     89       1    0.978  0.0155
  0.3     88       3    0.944  0.0241
  0.4     85       1    0.933  0.0263
  0.5     84       1    0.922  0.0282
  0.6     83       1    0.911  0.0300
  0.7     82       1    0.900  0.0316
  0.8     81       3    0.867  0.0358
  1.0     78       2    0.844  0.0382
  1.3     76       2    0.822  0.0403
  1.5     74       1    0.811  0.0413
  1.6     73       1    0.800  0.0422
  1.8     72       2    0.778  0.0438
  1.9     70       2    0.756  0.0453
  2.0     68       2    0.733  0.0466
  2.3     65       1    0.722  0.0472
  2.4     64       1    0.711  0.0478
  3.2     60       2    0.687  0.0491
  3.3     57       1    0.675  0.0497
  3.5     54       3    0.638  0.0514
  3.6     51       2    0.613  0.0524
  3.8     47       1    0.599  0.0528
  4.0     46       3    0.560  0.0540
  4.3     43       1    0.547  0.0543
  5.0     34       1    0.531  0.0550
  5.3     30       1    0.514  0.0560
  6.0     26       1    0.494  0.0572
  6.2     24       1    0.473  0.0584
  6.3     22       1    0.452  0.0596
  6.4     21       2    0.409  0.0612
  6.5     19       1    0.387  0.0616
  7.0     15       1    0.361  0.0627
  7.4     13       1    0.334  0.0637
  7.8      9       1    0.297  0.0666
> cat("Print survival fit\n")
Print survival fit
> print(sf.larynx)
Call: survfit(formula = Surv(time, delta) ~ 1, data = larynx, conf.type = "none")

      n events median
[1,] 90     50      6
> plot(sf.larynx,main="Larynx Tumor Survival",
+    conf.int="none", xlab="Time (months)",ylab="Survival")
> #***************************************************/
> # Weaning data from Klein and Moeschberger.       */
> # See class web page for link.  Variables are      */
> #duration of breast feeding, weeks                 */
> #Completed breast feeding? (1=yes, 0=no)           */
> #Race of mother (1=white, 2=black, 3=other)        */
> #Mother in poverty (1=yes, 0=no)                   */
> #Mother smoked at birth of child (1=yes, 0=no)     */
> #Mother used alcohol at birth of child (1=yes 0=no)*/
> #Age of mother at birth of child                   */
> #Year of birth                                     */
> #Education level of mother (years of school)       */
> #Prenatal care after 3rd month (1=yes, 0=no)       */
> #***************************************************/
> bfeed<-read.table("bfeed.txt",header=TRUE)
> #****************************************************/
> # Plot weaning times for smoking and non-smoking    */
> # groups separately.                                */
> #****************************************************/
> plot(survfit(Surv(duration,delta)~strata(smoke),data=bfeed),
+    main="Time Until Weaning, by Smoking Status",
+    ylab="Probability of Not Weaning",xlab="Time(days)",
+    lty=1:2)
> legend("topright",lty=1:2, legend=c("Nonsmoker","Smoker"))
> #***********************************************/
> # Survival functions with Confidence Intervals */
> #***********************************************/
> # Default confidence interval in survit is "log".
> # Do something more primitive here.
> plot(survfit(Surv(duration,delta)~1,data=bfeed,
+    conf.type="plain"),
+    main="Time Until Weaning, All Mothers",
+    sub="Confidence intervals, raw scale",
+    ylab="Probability of Not Weaning",xlab="Time(days)")
> cat('\n Larynx survival with conf. limits, raw scale\n')

 Larynx survival with conf. limits, raw scale
> #***************************************************/
> # Kaplan--Meier curve for the combined larynx data */
> #***************************************************/
> sfp.larynx<-survfit(Surv(time,delta)~1,data=larynx,
+    conf.type="plain")
> plot(sfp.larynx,main="Larynx Tumor Survival",
+    xlab="Time (months)",ylab="Survival",
+    sub="No transformation")
> # Application to high and low risk weeding data.;
> smdrk<-bfeed[(bfeed$smoke==1)&(bfeed$alcohol==1),]
> weanout<-survfit(Surv(duration,delta)~1,data=smdrk,
+    conf.type="plain")
> # Choose a small subset of mothers.  R truncates the
> # confidence interval at zero, but it would be negative
> # without truncation.
> plot(weanout, sub="Plain Confidence Interval",
+    xlab="Weaning time (days)",ylab="Survival",
+    main="Weaning times for Mothers who Smoke and Drink")
>