How to generate all first-order interaction terms for Lasso Logistic Regression?

风格不统一 提交于 2019-12-12 14:26:21

问题


Is there a way in glmnet to do first order interactions?

For instance, if my X matrix was:

V1 V2 V3
0  1   0
1  0   1
1  0   0
...

Is there a way to specify that it do something along the lines of `y~ V1 + V2 + V3 + V1*V2 + V2 *V3 + V1*V3' without manually creating the columns? My actual matrix is larger and would be a pain to create all first order cross products by hand.


回答1:


The proper R syntax for such a formula is

y~(V1+V2+V3)^2

For example

set.seed(15)
dd <- data.frame(V1=runif(50), V2=runif(50), V3=runif(50), y=runif(50))
lm(y~(V1+V2+V3)^2, dd)

Call:
lm(formula = y ~ (V1 + V2 + V3)^2, data = dd)

Coefficients:
(Intercept)           V1           V2           V3        V1:V2        V1:V3        V2:V3  
    0.54169     -0.10030     -0.01226     -0.10150      0.38521     -0.03159      0.01200 

Or, if you want to model all variables other than y,

lm(y~(.)^2, dd)

Call:
lm(formula = y ~ (.)^2, data = dd)

Coefficients:
(Intercept)           V1           V2           V3        V1:V2        V1:V3        V2:V3  
    0.54169     -0.10030     -0.01226     -0.10150      0.38521     -0.03159      0.01200 

Both are the same as

lm(y~V1+V2+V3+V1:V2+V1:V3+V2:V3, dd)

Call:
lm(formula = y ~ V1 + V2 + V3 + V1:V2 + V1:V3 + V2:V3, data = dd)

Coefficients:
(Intercept)           V1           V2           V3        V1:V2        V1:V3        V2:V3  
    0.54169     -0.10030     -0.01226     -0.10150      0.38521     -0.03159      0.01200  

You can use these formula with model.matrix to create a matrix

model.matrix(y~(V1+V2+V3)^2,dd)


来源:https://stackoverflow.com/questions/27577420/how-to-generate-all-first-order-interaction-terms-for-lasso-logistic-regression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!