# Commands for estimating cumulative hazards and

# survival probabilites for the melanoma data

 

 

# Define sex and ulceration as factors (seems to be necessary when

# estimating cumulative hazards and survival probabilities)

melanoma$sex<-factor(melanoma$sex)

melanoma$ulcer<-factor(melanoma$ulcer)

 

 

 

# Compute and plot Nelson-Aalen estimates for females and males (cf. practical exercise 1):

fit.mel.a=survfit(Surv(lifetime,status==1)~sex, data=melanoma, type="fl", error="ts", conf.type="plain")

plot(fit.mel.a, fun="cumhaz", mark.time=F, xlim=c(0,10), xlab="Years after operation", ylab="Cumulative hazard", main="Sex")

 

# Compute cumulative hazards estimates for females and males based on a Cox regression and plot the estimates on the same plot as the Nelson-Aalen estimates:

cox.mel.a<- coxph(Surv(lifetime,status==1)~sex, data=melanoma)

lines(survfit(cox.mel.a,newdata=data.frame(sex=0)),fun="cumhaz",lty=3)

lines(survfit(cox.mel.a,newdata=data.frame(sex=1)),fun="cumhaz",lty=3)

 

# Alternative commands for the cumulative hazards estimates based on the Cox regression:

A0=basehaz(cox.mel.a,centered=F)

lines(A0$time,A0$hazard,type='s',lty=2)

lines(A0$time, exp(cox.mel.a$coef[1])*A0$hazard,type='s',lty=2)

 

 

# Compute and plot Kaplan-Meier estimates for males and females and estimates of the survival functions based on the Cox regression:

fit.mel.a=survfit(Surv(lifetime,status==1)~sex, data=melanoma, conf.type="plain")

plot(fit.mel.a, mark.time=F, xlim=c(0,10), xlab="Years after operation", main="Sex")

lines(survfit(cox.mel.a,newdata=data.frame(sex=0)),lty=2)

lines(survfit(cox.mel.a,newdata=data.frame(sex=1)),lty=2)

 

 

# Compute and plot estimates of survival functions for some combinations of the covariates sex, ulceration and tumor thickness: ?

cox.mel.e<- coxph(Surv(lifetime,status==1)~sex+ulcer+log(thickn,2), data=melanoma)

plot(survfit(cox.mel.e,newdata= data.frame(sex=0,ulcer=1,thickn=1),conf.type="none"),lty=1)

lines(survfit(cox.mel.e,newdata= data.frame(sex=0,ulcer=0,thickn=1),conf.type="none"),lty=2)

lines(survfit(cox.mel.e,newdata= data.frame(sex=0,ulcer=0,thickn=4),conf.type="none"),lty=3)