R-alpha: frames

Thomas Lumley (thomas@biostat.washington.edu)
Fri, 6 Dec 1996 16:09:52 -0800 (PST)


Date: Fri, 6 Dec 1996 16:09:52 -0800 (PST)
From: Thomas Lumley <thomas@biostat.washington.edu>
To: r-testers@stat.math.ethz.ch
Subject: R-alpha: frames
In-Reply-To: <Pine.SUN.3.91.961120105257.16564D-100000@uhura>


An interesting question arises when you use model.frame() on a fitted 
regression model (to extract the model frame it was fitted with).

For concreteness, consider the following
fit<-lm(y~x)

In what environment should model.frame(fit) evaluate y and x?

Currently it is evaluated in fit$call$data (if that exists) and otherwise
in sys.frame(sys.parent()).  This seems sensible, but a problem arises
when you consider functions that might want to run model.frame() on their
arguments. 

Consider a predict.lm() function that doesn't require the fit to contain 
both $y and $x objects (the S one doesn't).  If this function calls 
model.frame(fit) then sys.frame(sys.parent()) is the environment of the 
predict.lm() call.  A local variable called y or x will override the 
global one and horrible things will happen.  This also occurs in 
survival4 when computing fitted survival curves for a cox model.

Ideally it would be possible to find the frame in which the object was 
created and evaluate everything there.  I don't know how to do this.  
Second best may be to evaluate in the global environment, which is where 
most model objects are defined.  Any comments?

thomas lumley
UW biostatistics




=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-testers mailing list -- For info or help, send "info" or "help",
To [un]subscribe, send "[un]subscribe"
(in the "body", not the subject !)  To: r-testers-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-