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

Liviu Andronic landronimirc at gmail.com
Fri Nov 25 00:09:37 CET 2011


On Thu, Nov 24, 2011 at 11:55 PM, Prof Brian Ripley
<ripley at stats.ox.ac.uk> wrote:
> You would get exactly the same problem with ...,, anway.
>
> Here's a commonly used approach in R sources:
>
> x.lm <- function(formula, data, ...)
> {
>    Call <- match.call(expand.dots = TRUE)
>    Call[[1]] <- as.name("lm")
>    Call$formula <- as.formula(terms(formula))
>    eval(Call)
> }
>
>From limited testing this seems to do exactly what I wanted!

I thank all those who came up with suggestions. Regards
Liviu


>
>
> On Thu, 24 Nov 2011, Liviu Andronic wrote:
>
>> On Thu, Nov 24, 2011 at 10:25 PM, Prof Brian Ripley
>> <ripley at stats.ox.ac.uk> wrote:
>>>
>>> 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)
>>>
>> That's what I wanted. Thanks!
>>
>> However, I do want to simplify the syntax and define a new function:
>> x.lm <-
>>  function(formula, data, ...)
>> {
>>  eval(substitute(lm(f, data, ...), list(f = formula)))
>> }
>>
>> For the simple case it works just fine
>>>
>>> (form <- formula(y1~x1))
>>
>> y1 ~ x1
>>>
>>> x <- x.lm(form, anscombe)
>>
>> But it fails when I try to input more lm() arguments:
>>>
>>> (x <- x.lm(form, anscombe, subset=-5))
>>
>> Error in eval(expr, envir, enclos) :
>>  ..1 used in an incorrect context, no ... to look in
>>
>> Am I doing something obviously wrong? Regards
>> Liviu
>>
>
> --
> 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



-- 
Do you know how to read?
http://www.alienetworks.com/srtest.cfm
http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader
Do you know how to write?
http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail



More information about the R-help mailing list