#The commands below assume that the melanoma data set has been stored

# in the dataframe "melanoma", that the survival library has been attached

# (cf. the R commands to practical exercise 3).

 

 

# We illustrate the methods by looking at the fit of the models:

 

cox.fit<- coxph(Surv(lifetime,status==1)~ ulcer+thickn, data=melanoma)

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

 

 

#We compute the martingale residuals for the two models (cf p. 119 in ABG):

 

martres.fit<-residuals(cox.fit,type="mart")

martres.logfit<-residuals(cox.logfit,type="mart")

 

#From these we get the Cox-Snell residuals

# (i.e. the estimated cumulative intensity processes evaluated at

#? the (possibly) censored survival times):

 

csres.fit<-(melanoma$status==1)-martres.fit

csres.logfit<-(melanoma$status==1)-martres.logfit

 

#If the model is correctly specified, the Cox-Snell residuals should behave like a

# censored sample from a unit exponential distribution.

# To check whther this is the case, we compute Nelson-Aalen plots of the Cox-Snell residuals

# for ?the three thickness 0-1mm, 2-5mm and 5+ mm and add a line with unit

# slope for easy reference. (Similar plots may be made for ulceration.):

 

plot(survfit(Surv(csres.fit,status==1)~grthick, data=melanoma, type="fh"), fun="cumhaz", mark.time=F, lty=1:3, xlab="Residuals", ylab="Cumulative hazard", main="Thickness not transformed",xlim=c(0,1))

abline(0,1)

 

plot(survfit(Surv(csres.logfit,status==1)~grthick, data=melanoma, type="fh"), fun="cumhaz", mark.time=F, lty=1:3, xlab="Residuals", ylab="Cumulative hazard", main="Thickness log transformed",xlim=c(0,1))

abline(0,1)

 

 

# To assess the appropriate functional form of a numeric covariate we may make a smoothed

# of the martingale residuals versus th covariate for a model fitted without the actual covariate.

# To check whether thickness or log-thickness has a log-linear form, we first fit a model without

# thickness

cox.fit0<- coxph(Surv(lifetime,status==1)~ ulcer, data=melanoma)

martres.fit0<-residuals(cox.fit0, type="mart")

 

#We then make a plot of the martingale residuals versus thickness and log-thickness

# and add a smoothed curve to the plots

 

plot(melanoma$thickn, martres.fit0, xlab="Thickness",ylab=" ")

spline.fit<-smooth.spline(melanoma$thickn, martres.fit0)
lines(spline.fit$x, spline.fit$y)

 

plot(log(melanoma$thickn,2), martres.fit0, xlab="Thickness",ylab=" ")

spline.fit<-smooth.spline(log(melanoma$thickn,2), martres.fit0)

lines(spline.fit$x, spline.fit$y)

 

# The result is that there is not a log-linear effect of thickness,

# but that seems to be the case for log-thickness

 

 

#We finally check for proportionality of the covariates:

cox.zph(cox.fit)

cox.zph(cox.logfit)

 

#and make plots that suggest the (possible) time dependent effect of a covariate:

par(mfrow=c(1,2))

plot(cox.zph(cox.fit))

plot(cox.zph(cox.logfit))