I was approaching Cox regression and need to evaluate whether is more suitable to model my independent variable as a non-linear prediction of the outcome.
Main questions is:
- How do I check whether or not my independent variable should be modelled in a non-linear fashion?
I came into several methods, one of which was to plot the variable versus the martingale-based residual of the Cox regression (See for example www.sthda.com/english/wiki/cox-model-assumptions, last figures).
I therefore produce this type of plot with the following code, being var
my independent variable:
time <- data$time
status <- data$status
S <- Surv(time, status)
model1 <- coxph(S~1, data = data)
data$residual <- residuals(model1, type = "martingale")
ggplot(data = data, mapping = aes(x = var, y = residual)) +
geom_point() +
geom_smooth()
Here comes the other questions:
- As you can see, in my plot martingale-based residuals seems to be a lot "clustered" at extreme values, different from other plots that I found online in which residuals were more or less "uniform". I don't know how to interpret this data - seems like the data are "splitted".
- Based on the plot, should I assume a linear relationship? I was considering to use a restricted cubic spline with 4 knots, since my guess was that a non-linear relationship may be the case here.
- What is the correct interpretation of this plot? Do anyone have a guidance paper to interpret this plots and/or more generally speaking a guide to Cox diagnostics?
[EDIT]
This is the result of the model and of the anova as requested by @EdM - actually to generate this result I have fitted model2 with cph
.
Model:
Coef S.E. Wald Z Pr(>|Z|)
var 4.5077 1.5089 2.99 0.0028
var' -0.1702 4.8386 -0.04 0.9719
var'' -5.4636 12.5472 -0.44 0.6632
Anova:
Wald Statistics Response: S
Factor Chi-Square d.f. P
var 217.03 3 <.0001
Nonlinear 17.17 2 2e-04
TOTAL 217.03 3 <.0001