1

I want to run a lasso and ridge regression for a out of sample predictions of wages.

I have 13 explanatory variables and want generate all possible interactions terms and also maybe ^2 them order ^3 them and so on till maybe ^5. I also want to ^2 t and ^3 and so on the explanatory variables on there own.

Is there a possible way to do this automatically?

With for example model.matrix(y~(V1+V2+V3)^2, dataFrame) I get interactions therms but they are not ^2 only v1*V2 + V1*V3 + V2*V3. I also want (V1*V2)^2 maybe and so on.

zx8754
  • 52,746
  • 12
  • 114
  • 209
Dima Ku
  • 243
  • 2
  • 11
  • 1
    Check the documentation of `formula`. From there: "For example `(a+b+c)^2` is identical to `(a+b+c)*(a+b+c)` which in turn expands to a formula containing the main effects for `a`, `b` and `c` together with their second-order interactions". Also, probably a duplicate of [this question](https://stackoverflow.com/questions/27577420/how-to-generate-all-first-order-interaction-terms-for-lasso-logistic-regression). – Alexis Jun 21 '18 at 21:34
  • Possible duplicate of [How to generate all first-order interaction terms for Lasso Logistic Regression?](https://stackoverflow.com/questions/27577420/how-to-generate-all-first-order-interaction-terms-for-lasso-logistic-regression) – Alexis Jun 21 '18 at 21:37
  • i edit my questions – Dima Ku Jun 21 '18 at 21:50
  • I think the answer to this thread meets your needs: https://stackoverflow.com/questions/27580267/how-to-make-all-interactions-before-using-glmnet – Bryan Nov 18 '19 at 18:41

0 Answers0