I know there is a shortcut in R
to 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 R
to 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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With