In an epidemiological study, I'm using martingale plot to assess the linearity of continuous variables.
Here are the Martingale Residuals (from Null Model) using R's survminer::ggcoxfunctional()
output for 2 variables, on which we see that the linearity assumption is violated. Please note that since I have left-truncated data, the timescale is age (start is age at inclusion and stop is age at event or censoring, see this or this).
ggcoxfunctional(Surv(age_start, age_stop, event) ~ x1 + x2, data=db)
These are zoomed-in images so user can see the scaled functionnal form of the curve, the lowess account for other points which you can see on unzoomed images here (x1) and here (x2). Indeed the curve form is quite different and non-linearity could be leaved unseen in the unzoomed picture.
Since these variables are for adjustment only, I don't need a rock-hard precision, so I would like to break it into a categorical factor. My problem is to decide which breaks to decide. Since there is no consensus or clinical evidence on where to break these variables, I see 2 main options:
- break around the median or some quantiles
- use the Martingale to decide where to break
Is deciding using the Martingale plot better ? If yes, how to decide ?
Side question: depending on the source, the reading of the martingale plot is quite different. I saw two interpretations: "the curve should be somehow linear", and "the curve should be somehow linear and parallel to the x axis". Which one is the right one ?
EDIT :
Probably related (but unanswered) : What if linearity doesn't hold in a cox model?
I obviously should go for the spline method, but I couldn't find any ressource which explains it practically.
For instance, let's consider my case, with y
my measured variable and x1
, x2
and x3
my adjustment variables (confounding factors):
coxph(Surv(age_start, age_stop, event) ~ y + x1 + x2 + x3, data=db)
For what I witness with Martingale plots, x1
and x2
violate the linearity assumption, but y
and x3
are fine (the proportional hasard assumption is OK for everyone). From what I understood, I should apply splines on my "guilty" variables, and so write something like this:
library(rms)
coxph(Surv(age_start, age_stop, event) ~ y + rcs(x1) + rcs(x2) + x3, data=db)
Unfortunately, the output is not easily understandable and the help (?rcs
) is not providing much help. (Sorry Pr Harrell, this seems a great library but it is quite not beginner-friendly).
How can I correct my variables so the linearity assumption is not violated, without cheating by looking too much at my data ?
Surv()
function in the way you show in your question. $\endgroup$