0

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)
EDennnis
  • 191
  • 1
  • 11
  • I'm not sure what exactly you are trying todo here because at the top and bottom seem to be doing different things. Assuming some function did what you wanted, what exactly are the inputs to this function? You want to be able to specify different transformations for each column? – MrFlick May 10 '19 at 21:11
  • @MrFlick the difference I see is that I treat x1 and x2 in the edited version as matrices and x1 and x2 in the original as column vectors. However, you are correct that the edit version strips away the data.frame class. The objective is the same - take a df, make nonlinear transformations, pass those nonlinear transformations into some regression. – EDennnis May 13 '19 at 15:21

0 Answers0