Search code examples
rfunctionsyntax-errorglmnetlasso-regression

Issue running glmnet() for mtcars dataset


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?


Solution

  • 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