Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lm() Regression with interactions for an entire dataframe

Tags:

r

regression

lm

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
like image 854
etienne Avatar asked Oct 22 '15 12:10

etienne


1 Answers

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.

like image 135
Gena Avatar answered Sep 18 '22 05:09

Gena