# Calculate Schoenfeld residual for testing data? [closed]

I am trying to get Schoenfeld residuals for testing data, using the model parameters obtained from training data. However in r, predict.coxph gives only the linear predictors and their exponentials. Could anyone give me some suggestion?

## closed as off-topic by Michael M, kjetil b halvorsen, Michael Chernick, jbowman, Peter Flom♦Jan 13 '18 at 14:22

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – Michael M, kjetil b halvorsen, Michael Chernick, jbowman, Peter Flom
If this question can be reworded to fit the rules in the help center, please edit the question.

• All your suggestions are about getting Schoenfeld residuals for the current dataset, not the testing data. To do so for the testing data, you get the parameter, say, beta, and doresidual(coxph(..., data = testing, init = beta, control = coxph.control(iter.max)), type = "schoenfeld") – Amber Xue Jan 18 '18 at 19:51

Use cox.zph from the survival package in the format of:

viol <- cox.zph(cox.model)


This will give you a table of hazard assumption test results (if significant, the coefficient is time-varying). You can plot the cox.zph to plot the Schoenfeld residuals, which looks something like:

plot(viol.cox)


Per coefficient.

• thanks but not quite what I'm asking – Amber Xue Feb 13 '17 at 16:30

In my opinion, Yuval nails it quite well.

If you want to get the exact (scaled) Schoenfeld residuals for all covariates, access them from the viol .zph object like that:  viol$y  and viol$x gives you the corresponding time steps that you would need to rebuild Yuval's plot from scratch.

• Not what I was asking. – Amber Xue Jan 18 '18 at 19:53

You can also get them out just using the residual function

residual( <coxmodel>  ,  type = "scaledsch")

• Not what I was asking. – Amber Xue Jan 18 '18 at 19:53