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

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Nov 24 23:55:08 CET 2011


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)
}



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


More information about the R-help mailing list