Have a dataset with many predictor variables and want to take nonlinear of transformation of all the variables WITHOUT manually coding all of them.
This answer (short formula call for many variables when building a model) provides how to include all predictor variables but does not include how to include nonlinear transformation such as poly(x,2).
I am trying to obtain the following regression results
#data.frame
df <- data.frame(y = rnorm(100), x1 = rnorm(100,2,1), x2 = rnorm(100,5,3))
#ols regression
lm(y ~ ., data = df)
#ols regression with poly -> output I am trying to get
lm(y ~ poly(x1,2) + I(x2^2), data = df)
In theory, x1 and x2 are matrices of predictors NOT column vectors. I am looking for a way to pass through large number of predictors and get nonlinear transformation of these.
Any help on this would be appreciated.
EDIT
This code works but is rather clumsy. Allows for transformations in block matrices
#estimate OLS
g <- lm(y ~ ., data = df)
#obtain model matrix and transformation
X <- df %>% dplyr::select(., x1,x2) %>% as.matrix()
X2 <- model.matrix(g)[,-1]^2
X3 <- model.matrix(g)[,-1]^3
y <- df$y
lm(y ~ X + X2 + X3)