[R] formula error inside function

Terry Therneau therneau at mayo.edu
Wed Jan 25 14:25:31 CET 2012


> I want use survfit() and basehaz() inside a function, but it doesn't
> work. Could you take a look at this problem. Thanks for your help. 

Your problem has to do with environments, and these lines

   fmla <- as.formula("Surv(time, event) ~ Z1 + Z2")
   BaseFun <- function(x){
        start.coxph <- coxph(x, phmmd)  
         ...
        survfit(start.coxph)
        }
   Basefun(fmla)

The survfit routine needs to reconstruct the model matrix, and by
default in R this is done in the context where the model formula was
first defined.  Unfortunately this is outside the function, leading to
problems -- your argument "x" is is unknown in the outer envirnoment.
The solution is to add "model=TRUE" to the coxph call so that the model
frame is saved and survfit doesn't have to do reconstruction.

If you think this should work as is, well, so do I.  I spent a lot of
time on this issue a few months ago and finally threw in the towel.  The
interaction of environments with model.frame and model.matrix is subtle
and far from obvious.  (Just to be clear: I didn't say "broken".  Each
aspect of the process has well thought out reasons.)  The standard
modeling functions lm, glm, etc changed their defaults from model=F to
model=T at some point.  This costs some space & memory, but coxph may
need to do the same.

Terry T



More information about the R-help mailing list