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)
I have working code to finish that part of
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],
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
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
On Tue, May 29, 2012 at 11:43 AM, Paul Johnson pauljoh...@gmail.com 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
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 x1 also picks up
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 gunter.ber...@gene.com wrote:
I should have added:
If the formula is just
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(\\x\\,log(x),x+xc+cx)
Paul Johnson pauljoh...@gmail.com 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,
or use quote()...
-- Bert
On Tue, May 29, 2012 at 10:48 AM, cbe...@tajo.ucsd.edu wrote:
Paul Johnson pauljoh...@gmail.com 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)
10 matches
Mail list logo