Event History Analysis
dr hab. Joanna Landmesser-Rusek
Exercises, 15/03/2023
1. The observed survival times of patients after heart surgery (in years) for 10 people are as follows:
1+, 3, 4+, 5, 5, 6+, 7, 7, 7+, 8+, where „+” denotes censored.
Please, calculate the values of the survival function using the Kaplan-Meier estimator.
t(f) – ordered failure Times
nf – # of subjects in the risk set at the start of the interval
mf – # of failures
qf – censored in [t(f),t(f+1))
Sˆ KM (t ( f ) )
t(f)
nf
mf
qf
ni mi
n
i
i 1
f
(nf-mf)/nf
Sˆ KM (t ( f ) )
2. For the data below, estimate the Kaplan-Meier survival functions using R:
a) for group 1,
b) for group 2,
c) for the whole sample.
Make graphs of survival.
Which group (treatment or placebo) has a better survival prognosis?
What are the median survival times for the treatment and placebo groups?
The data: remission times (weeks) for two groups of leukemia patients
a) Group 1 (n=21)
Treatment group
6,6,6,7,10,13,16,22,23,6+,9+,10+,11+,17+,19+,20+,25+,32+,32+,34+,35+
b) Group 2 (n=21)
Placebo group
1,1,2,2,3,4,4,5,5,8,8,8,8,11,11,12,12,15,17,22,23
Group 1
Group 2
failed
9
21
censored
12
0
total
21
21
The data are available in the treatment.csv file in Moodle.
Example code in R:
#install.packages("OIsurv")
library(survival)
read.table(
'D:/JOANNA/Dydaktyka/Analiza_historii_zdarzen/kody_R/treatment.csv',
sep=';',
dec=',',
header=TRUE
) -> data
data
attach(data)
#survival object Surv(time,event) or Surv(time1,time2,event,type)
my.survival.object<-Surv(duration,failure)
my.survival.object
#kaplan-meier estimator
fit<-survfit(my.survival.object ~ 1) #krzywa przezycia
plot(fit, xlab = "Weeks", ylab="Survival")
summary(fit)
summary(fit)$surv # returns the Kaplan-Meier estimate at each t_i
summary(fit)$time # {t_i}
summary(fit)$n.risk # {Y_i}
summary(fit)$n.event # {d_i}
summary(fit)$std.err # standard error of the K-M estimate at {t_i}
summary(fit)$lower # lower pointwise estimates (alternatively, $upper)
str(fit)
# full summary of the fit object
str(summary(fit)) # full summary of the fit object
plot(fit, main="Kaplan-Meier estimate with 95% confidence bounds",
xlab="time", ylab="survival function")
#confidence bands
my.cb<-confBands(my.survival.object,confLevel=0.95,type="hall")
#plot graphs
plot(survfit(my.survival.object ~ 1), xlim=c(0, 40), xlab="weeks",
ylab="Estimated Survival Function",
main="Reproducing Confidence Bands for S(x)")
plot(survfit(my.survival.object ~ treatment), xlab = "Weeks", ylab="Survival")
my.fit1<-survfit(Surv(duration, failure) ~ treatment)
plot(my.fit1, xlab = "Weeks", ylab="Survival")