Skip to content
Prev 333152 / 398506 Next

issues with calling predict.coxph.penal (survival) inside a function

Thanks for the reproducable example.  I can confirm that it fails on my machine using 
survival 2-37.5, the next soon-to-be-released version,

The issue is with NextMethod, and my assumption that the called routine inherited 
everything from the parent, including the environment chain.  A simple test this AM showed 
me that the assumption is false.  It might have been true for Splus.  Working this out may 
take some time -- every other one of my wrestling matches with predict inside a function 
has -- and there is a reasonable chance that it won't make this already overdue release.

In the meantime, here is a workaround that I have sometimes used in other situations. 
Inside your function do the following: fit a new coxph model with fixed coefficients, and 
do prediction on that.

myfun <- function(oldfit, subset) {
    newX <- model.matrix(oldfit)[subset,]
    newY <- oldfit$y[subset]
    newfit <- coxph(newY ~ newX, iter=0, init=coef(oldfit))
    newfit$var <- oldfit$var
    predict(newfit)
    }

If the subset is all of a particular strata, as you indicated, then all of the predictions 
will be correct.  If not, then those that make use of the the baseline hazard (type= 
expect) will be incorrect but all others are ok.

Terry Therneau
On 11/14/2013 05:00 AM, r-help-request at r-project.org wrote: