Skip to content
Prev 295960 / 398503 Next

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

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],gsub("x1", "x1c", as.character(f2[3]))))

It's admittedly unwieldy, but it seems pretty robust.

Something like:

changeFormula <- function(form, xIn, xOut){
    as.formula(paste(form[2], form[1], gsub(xIn, xOut, as.character(form[3]))))
}

changeForm(formula(m2), "x1", "x1c")

I'm not sure if this will play nice with environments and what not so
you might need to change those manually.

Hope this gets you started,
Michael
On Tue, May 29, 2012 at 11:43 AM, Paul Johnson <pauljohn32 at gmail.com> wrote: