From therneau at mayo.edu  Tue Jun 17 00:12:32 2014
From: therneau at mayo.edu (Therneau, Terry M., Ph.D.)
Date: Mon, 16 Jun 2014 17:12:32 -0500
Subject: [Rd] model.frame and parent environment
In-Reply-To: <539F38F5.9090902@mayo.edu>
References: <539F38F5.9090902@mayo.edu>
Message-ID: <27747b$8rkckh@ironport10.mayo.edu>

I had a typo in the prior example when transcribing from R to the message, now corrected 
below.  (The call to myfun).
   My apologies for that.  Corrected message below.

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( y~x, 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.