There's more...
We haven't yet discussed how to find the optimal lambda value. This can be done using the cv.glmnet function. In this case, we won't use the loop we have used so far, but we will define a simple dataset and show you how to get the best lambda. This technique uses cross validation to determine the best value (the dataset is split into several parts, and k-1 parts are used to train, and 1 part is used to test the model—this is done several times). In the following plot, we have the log of lambda on the x axis, and on the y axis we have the mean squared error (MSE). The upper part of the plot shows the number of non-zero variables. There are two vertical lines: the one on the left marks where the minimum MSE is, and the one on ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access