How to make all interactions before using glmnet

只愿长相守 提交于 2020-05-09 18:46:26

问题


I have an x-matrix of 8 columns. I want to run glmnet to do a lasso regression. I know I need to call:

glmnet(x, y, family = "binomial", ...). 

However, how do I get x to consider all one way interactions as well? Do I have to manually remake the data frame: if so, is there an easier way? I suppose I was hoping to do something using an R formula.


回答1:


Yes, there is a convenient way for that. Two steps in it are important.

library(glmnet)
# Sample data
data <- data.frame(matrix(rnorm(9 * 10), ncol = 9))
names(data) <- c(paste0("x", 1:8), "y")
# First step: using .*. for all interactions
f <- as.formula(y ~ .*.)
y <- data$y
# Second step: using model.matrix to take advantage of f
x <- model.matrix(f, data)[, -1]
glmnet(x, y)



回答2:


f <- as.formula( ~ .^2) should also work for including main effects and all pairwise interactions



来源:https://stackoverflow.com/questions/27580267/how-to-make-all-interactions-before-using-glmnet

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