[R] issues with calling predict.coxph.penal (survival) inside a function
Terry Therneau
therneau at mayo.edu
Thu Nov 14 16:30:52 CET 2013
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:
> Hello everyone,
>
>
>
> I got an issue with calling predict.coxph.penal inside a function.
>
>
>
> Regarding the context: My original problem is that I wrote a function that
> uses predict.coxph and survfit(model) to predict
>
> a lot of survival-curves using only the basis-curves for the strata (as
> delivered by survfit(model) ) and then adapts them with
>
> the predicted risk-scores. Because there are cases where my new data has
> strata which didn't exist in the original model I exclude
>
> them, using a Boolean vector inside the function.
>
> I end up with a call like this: predict (coxph_model,
> newdata[subscript_vector,] )
>
>
>
> This works fine for coxph.model, but when I fit a model with a spline
> (class coxph.penal), I get an error:
>
> "Error in `[.data.frame`(newdata, [subscript_vector, ) : object
> '[subscript_vector ' not found"
>
>
>
> I suppose this is because of NextMethod, but I am not sure how to work
> around it. I also read a little bit about all those
> matching-and-frame-issues,
>
> But must confess I am not really into it.
>
More information about the R-help
mailing list