Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically include all 2-way interactions in a glm model in R

Tags:

r

Suppose you are using R and have data stored in a data frame, M. Then I know that

g <- glm(Y ~ ., data=M)  

will automatically fit a model where Y is the dependent variables and all other columns of M are the predictors. Is there an analogously simple way to additionally include every two way interaction?

like image 513
Macro Avatar asked Jul 24 '12 14:07

Macro


People also ask

How do you add interaction variables in R?

A two step process can be followed to create an interaction variable in R. First, the input variables must be centered to mitigate multicollinearity. Second, these variables must be multiplied to create the interaction variable.


1 Answers

You can do two-way interactions simply using .*. and arbitrary n-way interactions writing .^n. formula(g) will tell you the expanded version of the formula in each of these cases.

like image 186
MvG Avatar answered Sep 25 '22 20:09

MvG