1
$\begingroup$

After having posted another question on the subject, I'm trying this time on my real data (unfortunately not openly publishable) to find the the best variable transformation that yields linearity in log hazard or log cumulative hazard of a Cox proportional Hazard model. For this I'm trying to plot the variable against the residuals by using this code in R

cox_mod_spline = coxph(Surv(timespan_censored,status)~ risk_factor, data = df)
res = residuals(cox_mod_spline, type = "martingale")
df$risk_factor
plot(na.omit(df$risk_factor), res)

However I get this error message : Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' and 'y' lengths differ

Indeed when I enter this code:

length(df$risk_factor)
length(res)

I get

[1] 587
[1] 577

respectively

I also checked that there are no NA in df$risk_factor, So I'm assuming that it can hardly come from there. Moreover, as it is a univariate (as opposed to multiple) model, it cannot comes from NA in other variable as there will not even be considered in the model

why do the residuals and the variable differ in length given the fact that the residuals of the model are created FROM the variable itself?

$\endgroup$
1
  • $\begingroup$ Many thanks @DimitrisRizopoulos. Well sorry, that was easy. Sorry if it wasn't the most interesting question ever. You can put your comment as an answer if you want and I'll accept it. By the way, thank you very much for the course you made on internet. It is very helpful to help understand the topic in general and to help understand the content of the book of Prof. Harrell (which I could also grasp eventually). $\endgroup$
    – ecjb
    Commented Oct 8, 2018 at 7:28

1 Answer 1

2
$\begingroup$

Do you perhaps have any missing data in timespan_censored or status? Could you perhaps try the following:

df_new <- with(df, df[complete.cases(timespan_censored, status, risk_factor), ])
cox_mod <- coxph(Surv(timespan_censored, status) ~ risk_factor, data = df_new)

df_new$martn_res <- residuals(cox_mod, type = "martingale")
plot(martn_res ~ risk_factor, data = df_new)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.