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

2012-05-29 Thread Paul Johnson
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

Re: [R] trouble automating formula edits when log or * are present; update trouble

2012-05-29 Thread R. Michael Weylandt
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],

Re: [R] trouble automating formula edits when log or * are present; update trouble

2012-05-29 Thread 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

Re: [R] trouble automating formula edits when log or * are present; update trouble

2012-05-29 Thread Bert Gunter
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

Re: [R] trouble automating formula edits when log or * are present; update trouble

2012-05-29 Thread Gabor Grothendieck
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

Re: [R] trouble automating formula edits when log or * are present; update trouble

2012-05-29 Thread Paul Johnson
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

Re: [R] trouble automating formula edits when log or * are present; update trouble

2012-05-29 Thread R. Michael Weylandt
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

Re: [R] trouble automating formula edits when log or * are present; update trouble

2012-05-29 Thread Bert Gunter
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)

Re: [R] trouble automating formula edits when log or * are present; update trouble

2012-05-29 Thread cberry
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,

Re: [R] trouble automating formula edits when log or * are present; update trouble

2012-05-29 Thread Bert Gunter
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)