or use quote()...
-- Bert
On Tue, May 29, 2012 at 10:48 AM, wrote:
> Paul Johnson writes:
>
>> do.call( substitute, list( frm, list( x = as.name("z") ) ) )
## or using quote()
>>do.call( substitute, list( frm, list( x = quote(z
> y ~ log(z) * (w + u)
>
>
> HTH,
>
> Chuck
>
>
>>
>> I h
Paul Johnson writes:
> Greetings
>
> I want to take a fitted regression and replace all uses of a variable
> in a formula. For example, I'd like to take
>
> m1 <- lm(y ~ x1, data=dat)
>
> and replace x1 with something else, say x1c, so the formula would become
>
> m1 <- lm(y ~ x1c, data=dat)
So
Paul et. al:
I think Gabor's incantation qualifies as my desired alternative to
eval(parse())). It is, unfortunately, rather tricky, imo.
However, the objection you raise to the gsub(deparse()) solution is
easily overcome through the use of an appopriate regex: e.g.:
gsub("\\","log(x)","x+xc+cx
Deparse... that's it -- was disappointed with having to turn
as.character.formula inside out once and again. Merci!
But, as always, we all loose to Gabor ;-)
Michael
On Tue, May 29, 2012 at 1:16 PM, Bert Gunter wrote:
> I should have added:
>
> If the formula is just assigned to a name, quote()
>>
>
> Try substitute:
>
>> do.call("substitute", list(newFmla, setNames(list(as.name("x1c")), "x1")))
> y ~ log(x1c) + x2 * x3
>
Damn. That's pretty. I'd say "setNames" a magic bullet too.
Thanks very much.
The approach suggested by Michael and Bert has the little shortcoming
that grepping for "
On Tue, May 29, 2012 at 11:43 AM, Paul Johnson wrote:
> Greetings
>
> I want to take a fitted regression and replace all uses of a variable
> in a formula. For example, I'd like to take
>
> m1 <- lm(y ~ x1, data=dat)
>
> and replace x1 with something else, say x1c, so the formula would become
>
>
I should have added:
If the formula is just assigned to a name, quote() and
eval(parse(...)) are not needed:
fm1 <- y ~ x1 ## a formula
w <- gsub( "x1","log(x1)", deparse(fm1))
fm2 <- formula(w)
This is probably the btter way to do it.
-- Bert
On Tue, May 29, 2012 at 10:01 AM, Bert Gunter
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.
> z <- deparse(quote(lm(y~x1)))
> z
[1] "lm(y ~ x1)"
Then you can apply your sug
Hi Paul,
I haven't quite thought through this yet, but might it not be easier
to convert your formula to a character and then use gsub et al on it
directly?
Something like this
# Using m2 as you set up below
m2 <- lm(y ~ log(x1) + x2*x3, data=dat)
f2 <- formula(m2)
as.formula(paste(f2[2], f2[1
9 matches
Mail list logo