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

Gabor Grothendieck ggrothendieck at gmail.com
Tue May 29 19:26:49 CEST 2012


On Tue, May 29, 2012 at 11:43 AM, Paul Johnson <pauljohn32 at 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 become
>
> m1 <- lm(y ~ x1c, data=dat)
>
> I have working code to finish that part of the problem, but it fails
> when the formula is more complicated. If the formula has log(x1) or
> x1:x2, the update code I'm testing doesn't get right.
>
> Here's the test code:
>
> ##PJ
> ## 2012-05-29
> dat <- data.frame(x1=rnorm(100,m=50), x2=rnorm(100,m=50),
> x3=rnorm(100,m=50), y=rnorm(100))
>
> m1 <- lm(y ~ log(x1) + x1 + sin(x2) + x2 + exp(x3), data=dat)
> m2 <- lm(y ~ log(x1) + x2*x3, data=dat)
>
> suffixX <- function(fmla, x, s){
>    upform <- as.formula(paste0(". ~ .", "-", x, "+", paste0(x, s)))
>    update.formula(fmla, upform)
> }
>
> newFmla <- formula(m2)
> newFmla
> suffixX(newFmla, "x2", "c")
> suffixX(newFmla, "x1", "c")
>
> The last few lines of the output. See how the update misses x1 inside
> log(x1) or in the interaction?
>
>
>> newFmla <- formula(m2)
>> newFmla
> y ~ log(x1) + x2 * x3
>> suffixX(newFmla, "x2", "c")
> y ~ log(x1) + x3 + x2c + x2:x3
>> suffixX(newFmla, "x1", "c")
> y ~ log(x1) + x2 + x3 + x1c + x2:x3
>

Try substitute:

> do.call("substitute", list(newFmla, setNames(list(as.name("x1c")), "x1")))
y ~ log(x1c) + x2 * x3


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list