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. 

 

I attach a reproducible example. 

Any help or suggestions of work-arounds will be appreciated. 

 

Thanks 

Julian

 

> version

               _                           

platform       x86_64-w64-mingw32          

arch           x86_64                      

os             mingw32                     

system         x86_64, mingw32             

status                                     

major          3                           

minor          0.1                         

year           2013                        

month          05                          

day            16                          

svn rev        62743                       

language       R                           

version.string R version 3.0.1 (2013-05-16)

nickname       Good Sport    

 

 

##TEST-DATA

 

# Create the simplest test data set 

test1 <- data.frame(time=c(4,3,1,1,2,2,3), 

              status=c(1,1,1,0,1,1,0), 

              x=c(0,2,1,1,1,0,0), 

              sex=c(0,0,0,0,1,1,1)) 

 

# Fit a stratified model 

fit1 <- coxph(Surv(time, status) ~ x + strata(sex), test1) 

summary(fit1)

 

#fit stratified wih spline

fit2 <- coxph(Surv(time, status) ~ pspline(x, df=2) + strata(sex), test1) 

summary(fit2)

 

#function to predict within

 

predicting_function <- function(model, newdata){

  subs <-vector(mode='logical', length=nrow(newdata))

  subs[1:length(subs)]<- TRUE

  

  ret <- predict (model, newdata=newdata[subs,])

  return(ret)

}

 

predicting_function(fit1, test1) # works

 

predicting_function(fit2,test1) #doesnt work - Error in
`[.data.frame`(newdata, subs, ) : object 'subs' not found

                                # probably because of NextMethod

 

#--------

> traceback()

#12: `[.data.frame`(newdata, subs, )

#11: newdata[subs, ]

#10: is.data.frame(data)

#9: model.frame.default(data = newdata[subs, ], formula = ~pspline(x, 

#       df = 2) + strata(sex), na.action = function (object, ...) 

#   object)

#8: model.frame(data = newdata[subs, ], formula = ~pspline(x, df = 2) + 

#       strata(sex), na.action = function (object, ...) 

#   object)

#7: eval(expr, envir, enclos)

#6: eval(tcall, parent.frame())

#5: predict.coxph(model, newdata = newdata[subs, ])

#4: NextMethod("predict", object, ...)

#3: predict.coxph.penal(model, newdata = newdata[subs, ])

#2: predict(model, newdata = newdata[subs, ]) at #5

#1: predicting_function(fit2, test1)

 


        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to