官术网_书友最值得收藏!

L2 penalty in action

To see how the L2 penalty works, we can use the same simulated linear regression problem we used for the Ll penalty. To fit a ridge regression model, we use the glmnet() function from the glmnet package. As mentioned previously, this function can actually fit the L1 or the L2 penalties, and which one occurs is determined by the argument, alpha. When alpha = 1, it fits lasso, and when alpha = 0, it fits ridge regression. This time, we choose alpha = 0. Again, we evaluate a range of lambda options and tune this hyper-parameter automatically using cross-validation. This is accomplished by using the cv.glmnet() function. We plot the ridge regression object to see the error for a variety of lambda values:

m.ridge.cv <- cv.glmnet(X[1:100, ], y[1:100], alpha = 0)
plot(m.ridge.cv)
Figure 3.11: Ridge regularization

Although the shape is different from lasso in that the error appears to asymptote for higher lambda values, it is still clear that, when the penalty gets too high, the cross-validated model error increases. As with lasso, the ridge regression model seems to do well with very low lambda values, perhaps indicating the L2 penalty does not improve out-of-sample performance/generalizability by much.

Finally, we can compare the OLS coefficients with those from lasso and the ridge regression model:

> cbind(OLS = coef(m.ols),Lasso = coef(m.lasso.cv)[,1],Ridge = coef(m.ridge.cv)[,1])
OLS Lasso Ridge
(Intercept) 2.958 2.99 2.9919
X[1:100, ]1 -0.082 1.41 0.9488
X[1:100, ]2 2.239 0.71 0.9524
X[1:100, ]3 0.602 0.51 0.9323
X[1:100, ]4 1.235 1.17 0.9548
X[1:100, ]5 -0.041 0.00 -0.0023

Although ridge regression does not shrink the coefficient for the fifth predictor to exactly 0, it is smaller than in the OLS, and the remaining parameters are all slightly shrunken, but quite close to their true values of 3, 1, 1, 1, 1, and 0.

主站蜘蛛池模板: 邹城市| 深水埗区| 安福县| 祁连县| 县级市| 正安县| 德化县| 苏州市| 巴彦县| 肃宁县| 乌拉特中旗| 淮安市| 长治县| 千阳县| 荥经县| 吉林省| 洪洞县| 石狮市| 香河县| 民丰县| 会同县| 江陵县| 宁城县| 鄱阳县| 杂多县| 紫金县| 景东| 钟山县| 迁安市| 吴江市| 平和县| 新邵县| 抚顺县| 鹿邑县| 谢通门县| 通辽市| 云南省| 尖扎县| 彭水| 祁门县| 垦利县|