[Rd] model.frame and parent environment

Therneau, Terry M., Ph.D. therneau at mayo.edu
Mon Jun 16 20:35:33 CEST 2014

Someone has reported a problem with predict.coxph that I can't seem to solve.  The 
underlying issue is with model.frame.coxph; the same issue is also found in lm so I'll use 
that for the example.


 > test <- data.frame(y = 1:10 + runif(10), x=1:10)

 > myfun <- function(formula, nd) {
     fit <- lm(formula, data=nd, model=FALSE)

 > myfun(test)
Error in is.data.frame(data): object "nd" not found


1. The key line, in both model.frame.coxph and model.frame.lm is
     eval(fcall, env, parent.frame())

and it appear (at least to me) that the parent.frame() part of this is effectively ignored 
when fcall is itself a reference to model.frame.  I'd like to understand this better.

2. The modeling functions coxph and survreg in the survival default to model=FALSE, 
originally in mimicry of lm and glm; I don't know when R changed the default to model=TRUE 
for lm and glm.  One possible response to my question would be advice to change my 
routine's defaults too.  I'm somewhat reluctant since I work with a few very large data 
sets, but would entertain that discussion as well.   I'd still like to understand how 
model.frame could be made to work under the current regimen.

Terry Therneau

More information about the R-devel mailing list