I'm modeling "full linear model" (main effects, interactions of the second order and quadratic terms) in R but wondering on how to create all of these possible combinations in the simplest way, e.g., by model.matrix. For example, model with main effects and all possible interactions of the second order can be done such as
M1<-matrix(rnorm(36),nrow=6)
colnames(M1)<-LETTERS[1:6]
X<-as.data.frame(M1[,1:5])
Y<-M1[,6]
f <- as.formula( ~ .^2)
MM<-model.matrix(f, X)[,-1]
lin_model<-lm(Y ~ MM)
Is there any simple way how to adjust f formula in the code above to be able to add quadratic terms into formula? E.g. if I have >10 feature I'd like to avoid explicit way of doing that.
Maybe you are looking for this.
This might look more appealing, but as noted by @onyambu actually it's the same what you get with
poly.