我正在使用 LASSO 对具有 102 个预测变量的数据集进行回归。数据集被正确读取,并且名称(谋杀)给了我正确的标题。
>library(glmnet)
>murder=read.csv(file="CommViolPredUnnormalizedData.csv", header = TRUE)
从 cv.glmnet 获得最佳 lambda 后,我想使用最佳 lambda 预测系数。但是,结果在第 26 个系数之后更改了 Header 名称,系数的 Header 复制了第 26 个系数的 Header,并在末尾添加了 5 位数字。这样我就有了以 OtherPerCapxxxxxx 命名的其余 77 个系数,如下所示。是不是因为默认情况下 predict(.. type="coefficients") 最多只能保存 26 个系数标头?非常感谢。
>lasso.out=glmnet(x,y,alpha=1,family="gaussian",lambda=grid)
>lasso.coef=predict(lasso.out,type="coefficients",s=bestlam2)[1:103,]
>lasso.coef
(Intercept) population householdsize racePctBlack racePctWhite
15.68589291 0.00000000 0.00000000 0.07242063 -0.12888132
racePctAsian racePctHisp agePct12t21 agePct12t29 agePct16t24
0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
agePct65up numbUrban pctUrban medIncome pctWWage
0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
pctWFarmSelf pctWInvInc pctWSocSec pctWPubAsst pctWRetire
0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
medFamInc perCapInc whitePerCap blackPerCap indianPerCap
0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
AsianPerCap **OtherPerCap0 OtherPerCap10000** OtherPerCap10004 OtherPerCap10005
0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
OtherPerCap10011 OtherPerCap10012 OtherPerCap10028 OtherPerCap10031 OtherPerCap10040
0.23747627 0.00000000 0.00000000 0.00000000 0.00000000
OtherPerCap10043 OtherPerCap10044 OtherPerCap10045 OtherPerCap10046 OtherPerCap10048
0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
OtherPerCap10050 OtherPerCap10051 OtherPerCap10063 OtherPerCap10082 OtherPerCap10088
0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
OtherPerCap10095 OtherPerCap10102 OtherPerCap10113 OtherPerCap10114 OtherPerCap10115
0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
OtherPerCap10118 OtherPerCap10133 OtherPerCap10146 OtherPerCap10152 OtherPerCap10156
0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
OtherPerCap10159 OtherPerCap1016 OtherPerCap10162 OtherPerCap10164 OtherPerCap10166
0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
OtherPerCap10173 OtherPerCap10174 OtherPerCap10176 OtherPerCap10182 OtherPerCap10195
0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
OtherPerCap10205 OtherPerCap10217 OtherPerCap10218 OtherPerCap10219 OtherPerCap1022
0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
OtherPerCap10221 OtherPerCap10244 OtherPerCap10249 OtherPerCap10250 OtherPerCap10254
0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
OtherPerCap10257 OtherPerCap10262 OtherPerCap10272 OtherPerCap10276 OtherPerCap10277
0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
OtherPerCap10280 OtherPerCap10290 OtherPerCap10295 OtherPerCap10300 OtherPerCap10301
0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
OtherPerCap10315 OtherPerCap10317 OtherPerCap10321 OtherPerCap10329 OtherPerCap10333
0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
OtherPerCap10337 OtherPerCap10348 OtherPerCap10351 OtherPerCap10359 OtherPerCap10362
0.00000000 0.00000000 -1.49402709 0.00000000 0.00000000
OtherPerCap10381 OtherPerCap10385 OtherPerCap10394 OtherPerCap10395 OtherPerCap1040
0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
OtherPerCap10401 OtherPerCap10423 OtherPerCap10425
0.00000000 0.00000000 0.00000000