【问题标题】:How should I get the coefficients of Lasso Model?我应该如何获得套索模型的系数?
【发布时间】:2017-06-07 10:59:07
【问题描述】:

这是我的代码:

library(MASS)
library(caret)
df <- Boston
set.seed(3721)
cv.10.folds <- createFolds(df$medv, k = 10)
lasso_grid <- expand.grid(fraction=c(1,0.1,0.01,0.001))
lasso <- train(medv ~ ., 
               data = df, 
               preProcess = c("center", "scale"),
               method ='lasso',
               tuneGrid = lasso_grid,
               trControl= trainControl(method = "cv", 
                                       number = 10, 
                                       index = cv.10.folds))  

lasso

与线性模型不同,我无法从摘要(套索)中找到套索回归模型的系数。我该怎么做?或者我可以使用 glmnet?

【问题讨论】:

  • 您可以从对象中提取finalModel,然后您可以在其上使用elasticnet 函数,例如predict(lasso$finalModel, type = 'coefficients'),或者只是深入挖掘对象:lasso$finalModel$beta.pure。可能有一种更符合插入符号 API 的方法,但我不确定它会是什么。
  • 有效!但为什么我会看到 15 行系数?
  • 我认为它通过改变收缩参数来执行逐步选择。您还可以使用其中的 Cp 值来选择最佳模型。不过,我不是弹性网络专家,所以我相信如果你在CrossValidated 提出这个问题,你会得到更好的答案。
  • @KAICHENGWANG 我也有同样的问题。我在这里也发布了一个类似的问题:(stackoverflow.com/questions/48079660/…)。你有没有发现如何提取与最佳调整参数对应的系数?

标签: r r-caret lasso-regression


【解决方案1】:

当你使用method="lasso" 训练时,来自 elasticnet 的 enet 被调用:

lasso$finalModel$call
elasticnet::enet(x = as.matrix(x), y = y, lambda = 0)

小插曲写道:

LARS-EN 算法计算完整的弹性网络解 同时针对同一文件中收缩参数的所有值 最小二乘拟合的计算成本

lasso$finalModel$beta.pure下,你有所有16组系数的系数,对应lasso$finalModel$L1norm下的L1范数的16个值:

length(lasso$finalModel$L1norm)
[1] 16

dim(lasso$finalModel$beta.pure)
[1] 16 13

您也可以使用 predict 来查看它:

predict(lasso$finalModel,type="coef")
$s
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16

$fraction
 [1] 0.00000000 0.06666667 0.13333333 0.20000000 0.26666667 0.33333333
 [7] 0.40000000 0.46666667 0.53333333 0.60000000 0.66666667 0.73333333
[13] 0.80000000 0.86666667 0.93333333 1.00000000

$mode
[1] "step"

$coefficients
          crim        zn       indus      chas        nox       rm        age
0   0.00000000 0.0000000  0.00000000 0.0000000  0.0000000 0.000000 0.00000000
1   0.00000000 0.0000000  0.00000000 0.0000000  0.0000000 0.000000 0.00000000
2   0.00000000 0.0000000  0.00000000 0.0000000  0.0000000 1.677765 0.00000000
3   0.00000000 0.0000000  0.00000000 0.0000000  0.0000000 2.571071 0.00000000
4   0.00000000 0.0000000  0.00000000 0.0000000  0.0000000 2.716138 0.00000000
5   0.00000000 0.0000000  0.00000000 0.2586083  0.0000000 2.885615 0.00000000
6  -0.05232643 0.0000000  0.00000000 0.3543411  0.0000000 2.953605 0.00000000
7  -0.13286554 0.0000000  0.00000000 0.4095229  0.0000000 2.984026 0.00000000
8  -0.21665925 0.0000000  0.00000000 0.5196189 -0.5933941 3.003512 0.00000000
9  -0.32168140 0.3326103  0.00000000 0.6044308 -1.0246080 2.973693 0.00000000
10 -0.33568474 0.3771889 -0.02165730 0.6165190 -1.0728128 2.967696 0.00000000
11 -0.42820289 0.4522827 -0.09212253 0.6407298 -1.2474934 2.932427 0.00000000
12 -0.62605363 0.7005114  0.00000000 0.6574277 -1.5655601 2.832726 0.00000000
13 -0.88747102 1.0150162  0.00000000 0.6856705 -1.9476465 2.694820 0.00000000
14 -0.91679342 1.0613165  0.09956489 0.6837833 -2.0217269 2.684401 0.00000000
15 -0.92906457 1.0826390  0.14103943 0.6824144 -2.0587536 2.676877 0.01948534

插入符号调整的超参数是最大 L1 范数的分数,因此在您提供的结果中,它将是 1,即最大值:

lasso
The lasso 

506 samples
 13 predictor

Pre-processing: centered (13), scaled (13) 
Resampling: Cross-Validated (10 fold) 
Summary of sample sizes: 51, 51, 51, 50, 51, 50, ... 
Resampling results across tuning parameters:

  fraction  RMSE      Rsquared   MAE     
  0.001     9.182599  0.5075081  6.646013
  0.010     9.022117  0.5075081  6.520153
  0.100     7.597607  0.5572499  5.402851
  1.000     6.158513  0.6033310  4.140362

RMSE was used to select the optimal model using the smallest value.
The final value used for the model was fraction = 1.

为了得到最优分数的系数:

predict(lasso$finalModel,type="coef",s=16)
$s
[1] 16

$fraction
[1] 1

$mode
[1] "step"

$coefficients
       crim          zn       indus        chas         nox          rm 
-0.92906457  1.08263896  0.14103943  0.68241438 -2.05875361  2.67687661 
        age         dis         rad         tax     ptratio       black 
 0.01948534 -3.10711605  2.66485220 -2.07883689 -2.06264585  0.85010886 
      lstat 
-3.74733185

【讨论】:

    【解决方案2】:

    我注意到使用上述方法可能会出现问题,如果一个人定义了自己的超参数调整网格。 Predict.enet 似乎强加了自己的网格,这通常与为 train() 定义的网格不对应。

    如果是这种情况,可以将“mode”参数设置为“fraction”,并提供从 train() 输出到“s”参数的分数向量:

    predict(lasso$finalModel, type = "coef", mode = "fraction", s = lasso$bestTune)
    

    “S”也可以是您的最佳调整参数,由 train() 确定:

    predict(lasso$finalModel, type = "coef", mode = "fraction", s = as.numeric(lasso$bestTune))
    

    reprex package (v0.3.0) 于 2020-09-11 创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-02
      • 1970-01-01
      • 1970-01-01
      • 2014-10-26
      • 2016-05-10
      • 1970-01-01
      • 2017-05-15
      • 2022-01-23
      相关资源
      最近更新 更多