user1357015 - 1 year ago 128

R Question

I have an x-matrix of 8 columns. I want to run

`glmnet`

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

However, how do I get

`x`

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

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)
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**