Re: R-alpha: model.frame and data=

Thomas Lumley (thomas@biostat.washington.edu)
Fri, 13 Sep 1996 12:00:23 -0700 (PDT)


Date: Fri, 13 Sep 1996 12:00:23 -0700 (PDT)
From: Thomas Lumley <thomas@biostat.washington.edu>
To: r-testers <r-testers@stat.math.ethz.ch>
Subject: Re: R-alpha: model.frame and data=
In-Reply-To: <Pine.SUN.3.91.960913113316.19284G-100000@chekov>

> When a model is fitted with the data= option, model.frame does not 
> reconstruct the data frame

I have a modification to model.frame that fixes this
The procedure is:
 if the formula argument has a model.frame component (like an R lm.object
but not an S lm.object) *and* the data argument is missing then that is
returned

 if there is a data argument it is used as the environment

 if there is no data argument but the formula has a $call component with 
a data argument it is evaluated in sys.frame(sys.parent) and used

 otherwise everything is in the global environment.

This works on lm, both for new data and the original data and works for 
the residuals() functions in survival4.  The changes are marked with ### 
and include the changes needed to get real labels for literal arguments.

thomas lumley
=============================================================
"model.frame" <-
function (formula, data, weights = NULL, offset = NULL, subset = NULL, 
	na.action = na.fail) 
{  if (!missing(data) || is.null(formula$model.frame)){    ###
	frame <- attr(terms(formula), "variables") 
	if (missing(data)) {                            
	        if (is.null(formula$call))	           ###
		  data <- sys.frame(sys.parent())
		else                                       ### 
		  data<-eval(formula$call$data,sys.frame(sys.parent())) ###
	      }
	if (is.null(weights) & is.null(offset)) 
		rval <- na.action(eval(frame, data)[subset, , drop = FALSE])
	else if (is.null(offset)) 
		rval <- na.action(data.frame(eval(frame, data), .weights 
= weights)[subset, , drop = FALSE])
	else if (is.null(weights)) 
		rval <- na.action(data.frame(eval(frame, data), 
.offset = offset)[subset, , drop = FALSE])
	else rval <- na.action(data.frame(eval(frame, data), .weights = weights
, .offset = offset)[subset, , drop = FALSE])
	attr(rval, "terms") <- terms(formula)
	xnames<-attr(terms(formula), "term.labels")[attr(terms(formula),
"order")==1]       ###
        names(rval)[-1]<-xnames                       ###
	rval
      }                                               ###
   else formula$model.frame                           ### 
}
===========================================================
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-testers mailing list -- To (un)subscribe, send
subscribe	or	unsubscribe
(in the "body", not the subject !)  To: r-testers-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-