[Rd] formula(model.frame(y~.^2, data=d)) does not return formula from terms attribute of the model.frame
William Dunlap
wdunlap at tibco.com
Thu Jan 20 00:43:22 CET 2011
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
> -----Original Message-----
> From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk]
> Sent: Wednesday, January 19, 2011 6:50 AM
> To: William Dunlap
> Cc: r-devel at r-project.org
> Subject: Re: [Rd] formula(model.frame(y~.^2, data=d)) does
> not return formula from terms attribute of the model.frame
>
> 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