0

Whenever I run glmnet(mpg ~ ., data = mtcars, alpha=1) (from the glmnet package) I get the following error:

"Error in glmnet(mpg ~ ., data = mtcars, alpha = 1) : unused argument (data = mtcars)"

Any ideas for how to deal with this?

I think its because the glmnet() function is supposed to take in x and y as separate arguments. If I need separate x and y arguments, how would I write the formula so that glmnet::glmnet() runs for all variables of mtcars?

bob
  • 610
  • 5
  • 23
  • 1
    That tutorial you linked to is about the `glmnetUtils` package while you are probably referring to the `glmnet` package. – markus Dec 01 '18 at 20:22
  • @markus you're right - I have edited the question – bob Dec 01 '18 at 20:27
  • 1
    Take a look at the docs (`?glmnet::glmnet`). It doesn't have a `data` argument; rather it takes a matrix `x` and a vector or matrix `y` for the response. The syntax you're using looks more like base functions such as `lm` that take formulas. Docs have several examples as well – camille Dec 01 '18 at 20:42

1 Answers1

1

As the commenter suggests you need to use the glmnet method like so:

fit <- glmnet(as.matrix(mtcars[-1]), mtcars$mpg, alpha=1)

plot(fit)

enter image description here

Zelazny7
  • 39,946
  • 18
  • 70
  • 84
  • thank you for your answer - could you interpret the plot for me? Just in general, but also, what does the x-axis represent? – bob Dec 02 '18 at 17:02
  • 1
    The X-axis represents the absolute value of the sum of the model coefficients. LASSO works by adding a penalty to your regression loss function. This penalty is the absolute value of the coefficients. As you relax the penalty (make it larger) more variables come into the model. The coefficients of the variables is denoted by the y-axis. – Zelazny7 Dec 03 '18 at 00:57