Julian
Julian

Reputation: 305

How to rename a complicated formula?

Suppose I have a formula y~x1+x2+I( (x1==x2)*x3 ) and estimate an linear model

summary(lm(y~x1+x2+I( (x1==x2)*x3 ), data=some_data ))
Coefficients:
                   Estimate Std. Error t value Pr(>|t|)  
(Intercept)          3.6027     1.6069   2.242   0.0662 .
x1                   1.8685     1.9769   0.945   0.3811  
x2                   2.6041     2.0286   1.284   0.2466  
I((x1 == x2) * x3)   0.5666     1.5456   0.367   0.7265  

Beside creating a new 'data.frame', is there any methods to modify the formula so that the summary table would become

Coefficients:
                   Estimate Std. Error t value Pr(>|t|)  
(Intercept)          3.6027     1.6069   2.242   0.0662 .
x1                   1.8685     1.9769   0.945   0.3811  
x2                   2.6041     2.0286   1.284   0.2466  
some_name            0.5666     1.5456   0.367   0.7265  

Upvotes: 3

Views: 283

Answers (1)

thelatemail
thelatemail

Reputation: 93813

Using within you could hack something up.

Instead of:

summary(lm(Sepal.Length ~ I(Sepal.Width * Petal.Length), data=iris))

#...
#(Intercept)                   4.252934   0.069396   61.28   <2e-16 ***
#I(Sepal.Width * Petal.Length) 0.142483   0.005632   25.30   <2e-16 ***
#...

You can use:

summary(lm(Sepal.Length ~ newvar, data=
         within(iris, newvar <- Sepal.Width * Petal.Length))) 

#...
#(Intercept) 4.252934   0.069396   61.28   <2e-16 ***
#newvar      0.142483   0.005632   25.30   <2e-16 ***
#...

Upvotes: 5

Related Questions