Update:I have written a much more detailed static page about the additive COX model: http://rforge.org/plothr/

The page has a download link to the functionplotHR()which does all the fuzz. It is extensively commented. It should be easy to understand the syntax and modify it for individual purposes.

Therneau et al. refer to the proportional hazards model or COX-regression model as “the workhorse of regression analysis for censored data”. They show how to implement the additive form of this model in SAS and S-pluss; already mentioned by Hastie and Tibshirany in 1986 when introducing Generalized Additive Models (GAM).

I found modelling the functional form of the covariates in a regression model for rightcensored survival times with smoothing splines extremely useful. And the implementation is absolutely straightforward in R.

The only thing needed is the installation of the R-libraries “survival” and “pspline”:

`install.packages("pspline")`

and

`install.packages("survival")`

In the following code I will refer to a dataset “MyData” with a binary status variable “death” and a time-to-event variable “days2death”.

The status variable “death” should be (not necessarily) 1 if the event of interesst occured to the subject and “days2death” gives then the time to this event.

Viualizing the functional form of a covariate takes the following steps:

- create the survival object of interesst
- fit a proportional hazards model with smoothing splines,
- predict the functional form of the covariate of interesst and
- plot it!

Note that there is the termplot() function in R which gives you the GAM plots after the modelfit, so step 3 would not be necessary – BUT: it has a bug and fails plotting a single covariate; and it does not allow all to much customizing.

This is the R code to achieve the analysis:

## 1 Create survival object:

`surv.death <- Surv(MyData$days2death, MyData$death)`

## 2 Fit proportional hazards model with smoothing splines for continuous covariates:

`library(survival)`

library(pspline)

pham.fit <- coxph( surv.death ~ pspline(EF, df=4) + pspline(Age, df=4) + strata (Sex, df=4) , data = MyData)

The model above includes the continuous covariates “EF” (ejection fraction) and “Age” and stratifies for “Sex”.

## 3 Produce the fitted smoothing spline for the first covariate in the above model formula with standard errors

`predicted <- predict(pham.fit , type = "terms" , se.fit = TRUE , terms = 1)`

“terms=1” refers to “pspline(EF,df=4)”

## 4 Plot it

First plotting axes and labels

`plot(0 , xlab="Ejection Fraction" , ylab = "Hazard Ratio" , main = "All-cause Death" , type = "n" , xlim=c(0,100) , ylim=c(0,3))`

the range of values on the x-axis (“xlim=c(0,100)”) is chosen manually for this specific covariate; of course it is possible to use something like `ylim = c( 0 , max(MyData$EF) )`

.

Now plot the fitted smoothing spline using the lines() function:

`lines( sm.spline(MyData$EF , exp(predicted$fit)) , col = "red" , lwd = 0.8)`

Note that the term prediction gives log-hazard-ratios; therefore exp(predicted$fit) is plotted against the values of the covariate. The sm.spline() function is necessary since the points of the plot appear in random order and density, according to the underlying dataset; a plain lines() function would produce just a chaotic pattern. Alternative:

`plot(MyData$EF , exp(predicted$fit) , col = "red" , cex = 0.2)`

produces a scattered plot that reflects the distribution of the underlying data – I do prefer adding a rug-plot on the bottom of the graph to illustrate this (see under).

… upper and lower confidence limits with dashed thinner lines

`lines(sm.spline(MyData$EF , exp(predicted$fit + 1.96 * predicted$se)) , col = "orange" , lty = 2 , lwd = 0.4)`

and

`lines(sm.spline(MyData$EF , exp(predicted$fit - 1.96 * predicted$se)) , col = "orange" , lty = 2 , lwd = 0.4)`

… a tiny horizontal line at hazard level 1, do see where the confidence limits cross:

`abline( h = 1 , col = "lightgrey" , lty = 2 , lwd = 0.4)`

… tiny tickmarks on the x-axes to reflect the distribution of the underlying data:

`axis( side = 1 , at = MyData$EF, labels = F , tick = T , tcl = 0.4 , lwd.ticks = 0.1)`

… and some fancy red tickmarks to mark minimum, lower hinge, median, upper hinge and maximum of the covariate in the dataset:

`axis( side = 1 , at = fivenum(MyData$EF), labels = F , tick = T , tcl = -0.2 , lwd.ticks = 1 , col.ticks = "red")`

Thats it!

## 4b) The easy way (works ONLY with MORE then 1 continous covariate) – predicting the terms can be omitted:

`termplot(pham.fit, se=T, rug=T)`

Resulting in …