#Setup for P4
rm(list=ls())
library("survival")
df1<-read.csv("Breast_cancer.csv", header=TRUE)
head(df1)
str(df1)
## 'data.frame': 45 obs. of 4 variables:
## $ id : int 1 2 3 4 5 6 7 8 9 10 ...
## $ times: int 224 212 208 198 181 148 101 100 71 70 ...
## $ delta: int 0 0 0 0 1 1 0 0 0 0 ...
## $ Z : int 0 0 0 0 0 0 0 0 0 0 ...
#(a) Compute the Kaplan-Meier Estimator for S(t)
lsurv1 <- with(df1, Surv(times, delta == 1))
lsurv1
## [1] 224+ 212+ 208+ 198+ 181 148 101+ 100+ 71+ 70+ 69 47 23 5 8
## [16] 10 13 18 24 25 26 31 35 40 41 48 50 59 61 68
## [31] 71 76+ 105+ 107+ 109+ 113 116+ 118 143 154+ 162+ 188+ 212+ 217+ 225+
km1<-survfit(lsurv1~1, data= df1, conf.type = "log-log")
print(km1)
## Call: survfit(formula = lsurv1 ~ 1, data = df1, conf.type = "log-log")
##
## n events median 0.95LCL 0.95UCL
## [1,] 45 26 113 50 NA
summary(km1)
## Call: survfit(formula = lsurv1 ~ 1, data = df1, conf.type = "log-log")
##
## time n.risk n.event survival std.err lower 95% CI upper 95% CI
## 5 45 1 0.978 0.0220 0.853 0.997
## 8 44 1 0.956 0.0307 0.834 0.989
## 10 43 1 0.933 0.0372 0.807 0.978
## 13 42 1 0.911 0.0424 0.780 0.966
## 18 41 1 0.889 0.0468 0.753 0.952
## 23 40 1 0.867 0.0507 0.727 0.938
## 24 39 1 0.844 0.0540 0.701 0.923
## 25 38 1 0.822 0.0570 0.676 0.907
## 26 37 1 0.800 0.0596 0.651 0.891
## 31 36 1 0.778 0.0620 0.626 0.874
## 35 35 1 0.756 0.0641 0.602 0.856
## 40 34 1 0.733 0.0659 0.578 0.839
## 41 33 1 0.711 0.0676 0.555 0.821
## 47 32 1 0.689 0.0690 0.532 0.802
## 48 31 1 0.667 0.0703 0.509 0.784
## 50 30 1 0.644 0.0714 0.487 0.765
## 59 29 1 0.622 0.0723 0.465 0.746
## 61 28 1 0.600 0.0730 0.443 0.726
## 68 27 1 0.578 0.0736 0.421 0.706
## 69 26 1 0.556 0.0741 0.400 0.686
## 71 24 1 0.532 0.0745 0.378 0.665
## 113 16 1 0.499 0.0769 0.342 0.638
## 118 14 1 0.463 0.0793 0.304 0.608
## 143 13 1 0.428 0.0808 0.269 0.578
## 148 12 1 0.392 0.0815 0.235 0.546
## 181 9 1 0.349 0.0833 0.193 0.509
#(b) Compute the 95% confidence intervals for sK-M(t) assuming conf.type=“plain”. What an issue?
survfit(lsurv1~1, data= df1, conf.type = "plain")
## Call: survfit(formula = lsurv1 ~ 1, data = df1, conf.type = "plain")
##
## n events median 0.95LCL 0.95UCL
## [1,] 45 26 113 59 NA
print(survfit(lsurv1~1, data= df1, conf.type = "plain"))
## Call: survfit(formula = lsurv1 ~ 1, data = df1, conf.type = "plain")
##
## n events median 0.95LCL 0.95UCL
## [1,] 45 26 113 59 NA
In some cases, the confidence limits calculated this way would extend below 0 or above 1.
#(c) Compute the 95% confidence intervals assuming conf.type=“log”. What an issue?
survfit(lsurv1~1, data= df1, conf.type = "log")
## Call: survfit(formula = lsurv1 ~ 1, data = df1, conf.type = "log")
##
## n events median 0.95LCL 0.95UCL
## [1,] 45 26 113 59 NA
print(survfit(lsurv1~1, data= df1, conf.type = "log"))
## Call: survfit(formula = lsurv1 ~ 1, data = df1, conf.type = "log")
##
## n events median 0.95LCL 0.95UCL
## [1,] 45 26 113 59 NA
By taking log and adding and subtracting an error term, the bounds of our interval can lie outside [0,1]
#(d) Compute the 95% confidence intervals assuming conf.type=“log-log”
survfit(lsurv1~1, data= df1, conf.type = "log-log")
## Call: survfit(formula = lsurv1 ~ 1, data = df1, conf.type = "log-log")
##
## n events median 0.95LCL 0.95UCL
## [1,] 45 26 113 50 NA
print(survfit(lsurv1~1, data= df1, conf.type = "log-log"))
## Call: survfit(formula = lsurv1 ~ 1, data = df1, conf.type = "log-log")
##
## n events median 0.95LCL 0.95UCL
## [1,] 45 26 113 50 NA
#(e) Compute Nelson-Aalen estimator for the cumulative hazard(t)
naest1 <- cumsum(km1$n.event/km1$n.risk)
naest1
## [1] 0.02222222 0.04494949 0.06820531 0.09201483 0.11640508 0.14140508
## [7] 0.16704610 0.19336189 0.22038892 0.24816670 0.27673813 0.30614989
## [13] 0.33645292 0.36770292 0.39996098 0.43329432 0.46777708 0.50349136
## [19] 0.54052840 0.57898994 0.57898994 0.62065660 0.62065660 0.62065660
## [25] 0.62065660 0.62065660 0.62065660 0.62065660 0.68315660 0.68315660
## [31] 0.75458518 0.83150825 0.91484159 0.91484159 0.91484159 1.02595270
## [37] 1.02595270 1.02595270 1.02595270 1.02595270 1.02595270 1.02595270
## [43] 1.02595270
plot(km1$time, naest1, type="s", col="red",
xlab="Time(Days)", ylab = "Cumulative Hazard", main = "Nelson-Aalan Estimator")
#(f) Compute the Fleming-Harrington estimator for S(t). Comment on the results compared to (a)
fh1 <- exp(-naest1)
fh2 <- survfit(lsurv1 ~ 1, data = df1, type = "fleming-harrington")
plot(fh2, xlab= "Time(Days)", ylab = "Survival Probability",
main = "Fleming-Harrington Estimator", col="orange",
conf.int = F)
points(km1$time, fh1, type = "s", col="red")
points(km1$time, km1$surv, type = "s", col="blue")
legend("topright", legend = c("F-H 1", "F-H 2", "K-M"),
col = c("red","orange","blue"), lty=1)
cbind(fh1, fh2$surv, km1$surv)
## fh1
## [1,] 0.9780229 0.9780229 0.9777778
## [2,] 0.9560458 0.9560458 0.9555556
## [3,] 0.9340687 0.9340687 0.9333333
## [4,] 0.9120916 0.9120916 0.9111111
## [5,] 0.8901146 0.8901146 0.8888889
## [6,] 0.8681376 0.8681376 0.8666667
## [7,] 0.8461606 0.8461606 0.8444444
## [8,] 0.8241837 0.8241837 0.8222222
## [9,] 0.8022067 0.8022067 0.8000000
## [10,] 0.7802299 0.7802299 0.7777778
## [11,] 0.7582530 0.7582530 0.7555556
## [12,] 0.7362763 0.7362763 0.7333333
## [13,] 0.7142995 0.7142995 0.7111111
## [14,] 0.6923228 0.6923228 0.6888889
## [15,] 0.6703462 0.6703462 0.6666667
## [16,] 0.6483696 0.6483696 0.6444444
## [17,] 0.6263931 0.6263931 0.6222222
## [18,] 0.6044167 0.6044167 0.6000000
## [19,] 0.5824404 0.5824404 0.5777778
## [20,] 0.5604642 0.5604642 0.5555556
## [21,] 0.5604642 0.5604642 0.5555556
## [22,] 0.5375913 0.5375913 0.5324074
## [23,] 0.5375913 0.5375913 0.5324074
## [24,] 0.5375913 0.5375913 0.5324074
## [25,] 0.5375913 0.5375913 0.5324074
## [26,] 0.5375913 0.5375913 0.5324074
## [27,] 0.5375913 0.5375913 0.5324074
## [28,] 0.5375913 0.5375913 0.5324074
## [29,] 0.5050203 0.5050203 0.4991319
## [30,] 0.5050203 0.5050203 0.4991319
## [31,] 0.4702056 0.4702056 0.4634797
## [32,] 0.4353921 0.4353921 0.4278274
## [33,] 0.4005801 0.4005801 0.3921751
## [34,] 0.4005801 0.4005801 0.3921751
## [35,] 0.4005801 0.4005801 0.3921751
## [36,] 0.3584548 0.3584548 0.3486001
## [37,] 0.3584548 0.3584548 0.3486001
## [38,] 0.3584548 0.3584548 0.3486001
## [39,] 0.3584548 0.3584548 0.3486001
## [40,] 0.3584548 0.3584548 0.3486001
## [41,] 0.3584548 0.3584548 0.3486001
## [42,] 0.3584548 0.3584548 0.3486001
## [43,] 0.3584548 0.3584548 0.3486001
The F-H estimator that is more sensitive to later events, it would tend to show a lower survival probability later compared to the K-M estimator.
##Problem5 #Setup
library(haven)
df2<-file.choose() #choose anemia2.sas7dbat
df2<-read_sas(df2)
df2$ageI <- as.factor(ifelse(df2$age >= 20,1,0))
km2.byage<- survfit(Surv(obs_time, GVHD==1)~ageI, data=df2, conf.type="log-log")
km2.byage
## Call: survfit(formula = Surv(obs_time, GVHD == 1) ~ ageI, data = df2,
## conf.type = "log-log")
##
## n events median 0.95LCL 0.95UCL
## ageI=0 28 5 NA NA NA
## ageI=1 36 15 NA 25 NA
summary(km2.byage)
## Call: survfit(formula = Surv(obs_time, GVHD == 1) ~ ageI, data = df2,
## conf.type = "log-log")
##
## ageI=0
## time n.risk n.event survival std.err lower 95% CI upper 95% CI
## 10 28 1 0.964 0.0351 0.772 0.995
## 22 27 2 0.893 0.0585 0.704 0.964
## 28 25 1 0.857 0.0661 0.663 0.944
## 49 24 1 0.821 0.0724 0.623 0.921
##
## ageI=1
## time n.risk n.event survival std.err lower 95% CI upper 95% CI
## 8 35 1 0.971 0.0282 0.814 0.996
## 9 34 1 0.943 0.0392 0.790 0.985
## 11 33 1 0.914 0.0473 0.757 0.972
## 12 32 1 0.886 0.0538 0.724 0.955
## 16 30 1 0.856 0.0595 0.688 0.938
## 17 29 1 0.827 0.0644 0.654 0.918
## 20 28 2 0.768 0.0721 0.588 0.876
## 25 26 3 0.679 0.0799 0.496 0.808
## 28 22 1 0.648 0.0820 0.464 0.783
## 31 21 1 0.617 0.0837 0.433 0.757
## 35 20 1 0.586 0.0850 0.403 0.731
## 46 19 1 0.556 0.0859 0.374 0.704