I am navigating through different packages that let fit non-linear predictors in the context of COX PH models.
However, I was looking to produce plots to represent the non-linear relationship between a continuous predictor and the risk (Hazard Ratio) of an event in a survival analysis. While I was able to do easily with the rms
package, I was looking to produce a similar plot using mgcv
.
Here is a toy example using colon
database in survival
.
library(rms)
library(survival)
library(mgcv)
dd <- datadist(colon)
options(datadist="dd")
#Using rms
rms.spline <- cph(Surv(time, status) ~ rcs(age,3), data=colon)
ggplot(rms::Predict(cph.spline, age=seq(40,80, by=1), fun=exp))
Here's the output, which is quite what's I have in mind:
Then with the mgcv package:
gam.model <-gam(time ~ s(age), data=colon, family="cox.ph", weights=status)
plot(gam.model)
And the output:
It is clear that this time the plot is not representing the predictor against the Hazard Ratio, and I had a very hard time in figuring out a) what actually the y axis in this plot represent (I suppose log-hazard?), and b) how to plot the predictor against HR instead.
I believe that you are correct that it is plotting the log of the hazard ratio. Here is a way to plot it in the original coordinates using base R. Modification for ggplot would be straightforward. Often, you can use type="response" in the predict call, but this does not work for the Cox PH family in gam.
age = seq(40, 80, 1)
y = predict(gam.model, newdata = data.frame(age=age), se.fit=TRUE)
plot(age, exp(y$fit), ylim=c(.8, 1.3))
lines(age, exp(y$fit + 1.96 * y$se.fit))
lines(age, exp(y$fit - 1.96 * y$se.fit))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.