[R] proper work-flow with 'formula' objects and lm()

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Nov 24 22:25:33 CET 2011


On Thu, 24 Nov 2011, Liviu Andronic wrote:

> On Thu, Nov 24, 2011 at 9:14 PM, Duncan Murdoch
> <murdoch.duncan at gmail.com> wrote:
>> It is retained.  terms(fit) will give it to you, if fit is an lm object.
>>
>
> Thank you. The following works nicely.
>> (form <- formula(y1~x1))
> y1 ~ x1
>> x <- lm(form, anscombe)
>> formula(terms(x))
> y1 ~ x1
>
>
> However, I was hoping that there was a way to input the 'form' object
> so that summary(x) would print the underlying formula used, not the
> formula object name. I was thinking of something in the style of
> x <- lm(deparse(form), anscombe)
> summary(x)
>
> Can this be done? Regards

Yes.  That's a job for substitute (the second time today).

> form <- formula(y1~x1)
> x <- eval(substitute(lm(f, anscombe), list(f = form)))
> summary(x)

Call:
lm(formula = y1 ~ x1, data = anscombe)
...

> Liviu
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
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-help mailing list