kassambara / survminer

Survival Analysis and Visualization
https://rpkgs.datanovia.com/survminer/
495 stars 160 forks source link

ggcoxdiagnostics() is not working properly for schoenfeld residuals #122

Closed pbiecek closed 7 years ago

pbiecek commented 7 years ago

Some residuals (like schoenfeld) are calculated for each failure time, not each observation.

Thus lines 91-94 will trigger an error because a data frame with schoenfeld residuals (n.rows = number of failure times) cannot be bind to vector of linear predictions for observations (=number of rows in the data).

The easiest fix is to replace xval <- 1:fit$n by xval <- 1:nrow(res) and change the default value for linear.predictions e.g. by linear.predictions = type %in% c("martingale", "score", "dfbeta", ...)

Expected behaviour

Example for ggcoxdiagnostics() will work also for schoenfeld residuals

coxph.fit2 <- coxph(Surv(futime, fustat) ~ age + ecog.ps, data=ovarian)
ggcoxdiagnostics(coxph.fit2, type = "schoenfeld")

Actual behaviour

Error in `$<-.data.frame`(`*tmp*`, "xval", value = c(2.60222487953181,  : 
  replacement has 26 rows, data has 12

Steps to reproduce the problem

coxph.fit2 <- coxph(Surv(futime, fustat) ~ age + ecog.ps, data=ovarian)
ggcoxdiagnostics(coxph.fit2, type = "schoenfeld")
kassambara commented 7 years ago

Bug fixed now by the pull request #123. Thank you for pointing this out and for your work!