0

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.

3
  • 2
    I'm not quite sure what your question is. The way you show it should work just fine, a ~ b + b:c + b:d + b:e. You can use parentheses to save a tiny bit of typing, a ~ b + b:(c + d + e). You can use . as a stand-in for all non-response variables in the data, a ~ b:(.). Is that what you're looking for? Commented May 24, 2022 at 16:28
  • I have many variables to add. I am looking for a way that does not imply a 30min typing ^^
    – Shunrei
    Commented May 24, 2022 at 16:32
  • 3
    Okay, b:(.) it is then. Commented May 24, 2022 at 16:32

1 Answer 1

3

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 
2
  • If I want to select specific columns and no all the responses variables ?
    – Shunrei
    Commented May 24, 2022 at 19:09
  • 2
    At some point if there are variable you want and variables you don't want, you'll have to let R know which are which. There's probably a way to make it work using the names or even the column numbers of either set (those you want, or those you don't want). Or maybe you can specify a naming pattern (all variables starting with "year_"). If you want more help, ask another question that's more specific, perhaps giving an example of how you'd like to specify the variables you want (or don't want): name, naming pattern, column number, something else? Commented May 24, 2022 at 19:32

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.