Skip to content

formula(model.frame(y~.^2, data=d)) does not return formula from terms attribute of the model.frame

3 messages · William Dunlap, Brian Ripley

#
In R 2.12.0 I get
  > d <- data.frame(x=1:10, y=log(1:10), f3=LETTERS[rep(1:3,c(3,3,4))])
  > m <- model.frame(y~.^2, data=d)
  > formula(m)
  y ~ x + f3
In S+ formula(m) gives formula given to model.frame(),
but in R you have to do the following get that formula:
  > formula(attr(m, "terms"))
  y ~ (x + f3)^2

Would it break anything to add to the top of formula.data.frame
something like
  if (!is.null(tms <- attr(x, "terms"))) {
    return(formula(tms))
  }
so that formula() would retrieve the formula buried
in model.frame's output?

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
12 days later
#
On Thu, 6 Jan 2011, William Dunlap wrote:

            
But that has the advantage that you almost certainly have a model 
frame and hence that is what you intend.  With 6-6 (or 20-20 in 
Imperial units) hindsight it would have been better to give model 
frames a class inheriting from "data frame", but it seems that the 
presence of attr(, "terms") is the most common test.
Unfortunately, that is rather hard to tell!
I looked (not hard, but without success) for examples of calling 
formula() on a data frame.  I did see that model.frame.default() calls 
as.formula() on a data frame, but only after checking for the absence 
of a "terms" attribute.

Can you explain where it would help?  I think we need to see examples 
to see if a change in meaning would be clearly beneficial.  I can 
envisage cases in which 'x' was a data frame that just happened to 
have been constructed as a model frame and where the currently 
documented meaning was intended.

  
    
#
It is not terribly important, but I had a model.frame
which I forgot was a model.frame and was surprised that
   lm(modelFrame)
gave me a result based on the different formula than
   formula(modelFrame)
showed.

S+'s formula() makes those consistent (and it also
makes model.frame's output have class "model.frame"
instead of just "data.frame").  That is inherited
from S version 3.  R has formula methods (especially
in package:nlme) to extract the formula from lots
of other kinds of objects, but not for model.frames.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com