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

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Jan 19 15:50:01 CET 2011


On Thu, 6 Jan 2011, William Dunlap wrote:

> 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

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.

> Would it break anything to add to the top of

Unfortunately, that is rather hard to tell!

> 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?

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.

> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-devel mailing list