Home > Software design >  Interaction terms of the same variable with multiple variables
Interaction terms of the same variable with multiple variables

Time:05-25

I have a model

lm = a ~ b

I would like to include c, d , e that represent interactions terms with b =>

lm = a ~ b b:c b:d b:e.

Is there a rapid way to obtain this result without taping each variable? I have more than 10 variables.

Thank you !

CodePudding user response:

In R formulas, . is a placeholder for all non-response variables present in the data.

coef(lm(mpg ~ wt:(.), data = mtcars))
 #  (Intercept)            wt        wt:cyl       wt:disp         wt:hp       wt:drat       wt:qsec 
 # 41.044844014 -16.742131313   0.028771226   0.006485611  -0.005048411   0.447194218   0.423909109 
 #        wt:vs         wt:am       wt:gear       wt:carb 
 # -0.087023686   0.402891966  -0.142805986   0.156345459 
  • Related