From therneau at mayo.edu  Mon Jun 16 20:35:33 2014
From: therneau at mayo.edu (Therneau, Terry M., Ph.D.)
Date: Mon, 16 Jun 2014 13:35:33 -0500
Subject: [Rd] model.frame and parent environment
Message-ID: <d46aec$hcl2i9@ironport9.mayo.edu>

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)
     model.frame(fit)
     }

 > 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



[Rd] model.frame and parent environment
Therneau, Terry M., Ph.D.