本文介绍了使用glmnet之前如何进行所有交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个8列的x矩阵.我想运行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", ...).
但是,如何获得x
来考虑所有单向交互?我是否必须手动重新制作数据框:如果是,是否有更简单的方法?我想我希望使用R公式来做些事情.
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.
推荐答案
是的,有一种简便的方法.其中的两个步骤很重要.
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)
这篇关于使用glmnet之前如何进行所有交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!