Skip to content
Prev 295964 / 398503 Next

trouble automating formula edits when log or * are present; update trouble

Michael:

m2 is a model fit, not a formula. So I don't think what you suggested will work.

However, I think your idea is a good one. The trick is to protect the
model specification from evaluation via quote(). e.g.
[1] "lm(y ~ x1)"

Then you can apply your suggestion:
[1] "lm(y ~ log(x1))"
Call:
lm(formula = y ~ log(x1))

Coefficients:
(Intercept)      log(x1)
   -0.04894      0.36484


The gsub() would make the substitution wherever "x1" appeared in the
model formula, thus fulfilling the OP's request.

Two comments:

1. update() behaves as documented. It is a formula update method, not
a macro substitution procedure.

2. I believe this illustrates a legitimate violation of the "avoid the
eval(parse)) construction" precept. However, I may be wrong about this
and would welcome being corrected and shown a better alternative.

Cheers,
Bert





On Tue, May 29, 2012 at 9:31 AM, R. Michael Weylandt
<michael.weylandt at gmail.com> wrote: