\[\newcommand{\E}{\mathbb{E}}\]

Green Question

For this problem, weâ€™ll use the `mtcars` data. Use the first 20 rows of `mtcars` to estimate a Lasso regression of mpg on cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb.

Using these estimates, predict `mpg` for the remaining 12 observations in `mtcars`. Which of these cars has the highest predicted `mpg`?

Rules:

• You can load `glmnet` and `glmnetUtils`, but cannot load any other external packages.

To win

Raise your hand, and tell me which car has the highest predicted `mpg`, and your team will advance to the championship code challenge.

Solution belowâ€¦

``````library(glmnetUtils)
train_data <- mtcars[1:20,]
test_data <- mtcars[21:(nrow(mtcars)), ]

lasso <- cv.glmnet(mpg ~ cyl + disp + hp + drat + wt + qsec + vs + am + gear + carb, data=mtcars,
use.model.frame=TRUE)

Yhat <- predict(lasso, newdata=test_data)

Yhat``````
``````##                  lambda.1se
## Toyota Corona      23.92724
## Dodge Challenger   17.86790
## AMC Javelin        18.06237
## Camaro Z28         16.57659
## Pontiac Firebird   16.97720
## Fiat X1-9          25.32226
## Porsche 914-2      24.70610
## Lotus Europa       26.01105
## Ford Pantera L     17.99759
## Ferrari Dino       21.10346
## Maserati Bora      16.66453
## Volvo 142E         23.13594``````
``max(Yhat)``
``## [1] 26.01105``

which is the predicted miles per gallon for the Lotus Europa