【问题标题】:How to get the coefficents of a Cross Validated Lasso for a specific lambda (Not the “1se” or “min” lambda)如何获取特定 lambda 的交叉验证套索的系数(不是“1se”或“min”lambda)
【发布时间】:2021-06-17 11:12:09
【问题描述】:

我在 R 中使用 cv.gamlr 函数运行 CV Lasso。我可以获得与“1se”或“min”标准相对应的 lambda 系数。

set.seed(123)
lasso<-cv.gamlr(x = X, y = Y, family ='binomial')
coef(lasso,select = "1se")
coef(lasso,select = "min")

但是,如果我想获取特定 lambda 的系数,并存储在 lasso$gamlr$lambda 向量中,该怎么办?有可能获得它们吗? 例如,要获取模型中第一个 lambda 的系数......像这样:

lambda_100<-  lasso$gamlr$lambda[100]  
coef(lasso,select = lambda_100)

当然,这会发送以下错误:

match.arg(select) 中的错误:“arg”必须为 NULL 或字符向量

谢谢:)

【问题讨论】:

    标签: r data-science cross-validation lasso-regression


    【解决方案1】:

    系数存储在lasso$gamlr$beta 下,在您的示例中,您可以像这样访问它们:

    library(gamlr)
    x = matrix(runif(500),ncol=5)
    y = rnorm(100)
    cvfit <- cv.gamlr(x, y, gamma=1)
    
    dim(cvfit$gamlr$beta)
    [1]   5 100
    
    length(cvfit$gamlr$lambda)
    [1] 100
    
    cvfit$gamlr$lambda[100]
        seg100 
    0.00125315 
        
    cvfit$gamlr$beta[,drop=FALSE,100]
    5 x 1 sparse Matrix of class "dgCMatrix"
           seg100
    1  0.12960060
    2 -0.16406246
    3 -0.46566731
    4  0.08197053
    5 -0.54170494
    

    或者如果你更喜欢向量:

    cvfit$gamlr$beta[,100]
             1           2           3           4           5 
     0.12960060 -0.16406246 -0.46566731  0.08197053 -0.54170494
    

    【讨论】:

      猜你喜欢
      • 2018-11-19
      • 2023-03-04
      • 2019-06-15
      • 2018-11-07
      • 2017-11-12
      • 2021-05-19
      • 2023-03-03
      • 2021-04-25
      • 2021-08-29
      相关资源
      最近更新 更多