Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make all interactions before using glmnet

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.

like image 946
user1357015 Avatar asked Dec 19 '14 23:12

user1357015


2 Answers

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)
like image 63
Julius Vainora Avatar answered Oct 13 '22 18:10

Julius Vainora


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

like image 44
bgreenwell Avatar answered Oct 13 '22 19:10

bgreenwell