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

Liviu Andronic landronimirc at gmail.com
Thu Nov 24 23:23:41 CET 2011


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



More information about the R-help mailing list