lm() Regression with interactions for an entire dataframe

etienne picture etienne · Oct 22, 2015 · Viewed 8k times · Source

I know there is a shortcut in Rto run an lm()regression on all a dataframe like this :

reg<-lm(y~.,data=df)

With df having explanatory variables x1, x2, ... x5, so it is the same as writing

reg<-lm(y~x1+x2+x3+x4+x5,data=df)

But this doesn't include interactions terms like x1:x2, ... Is there a shortcut in Rto run a regression on all columns of a dataframe with the interactions ? I am looking for 2 shortcuts which will have the same effects as

reg<-lm(y~x1*x2,x1*x3,x1*x4,x1*x5,x2*x3,...)
reg<-lm(y~x1*x2*x3*x4*x5) # this one will have interactions between the 5 variables

Answer

Gena picture Gena · Sep 14, 2018

The shortcut you are searching for is:

reg <- lm(y ~ (.)^2, data = df)

This will create a model with the main effects and the interactions between regressors.