【问题标题】:Extracting variables chosen by glmnet or lars packages in r提取 r 中 glmnet 或 lars 包选择的变量
【发布时间】:2014-07-02 10:24:28
【问题描述】:

我正在处理一个大 p 小 n 问题(p>1000,n=150),并决定使用 glmnet 和 lars 来选择可能的解释变量(据我所知是它们的功能),而不是手动执行此操作。

我已经从函数中得到了一个 lars 和一个 glmnet 对象,但现在不知道如何从这些对象中提取函数选择的系数/最佳模型的名称。

任何帮助将不胜感激!

【问题讨论】:

    标签: r


    【解决方案1】:

    这就是coef 函数的用途。详情请见?coef.glmnet

    # fit a glmnet object
    obj <- glmnet(x, y)
    
    # matrix of coefficients
    # each row is a variable, each column is one step in the glmnet path
    coef(obj)
    
    # coefficients for a specific model in the glmnet sequence
    coef(obj, s=obj$lambda[1])
    coef(obj, s=0.01)
    

    【讨论】:

    • 对不起,我犯了一个错误,我的意思是变量而不是系数!哎呀!
    【解决方案2】:

    我也认为这应该更容易,但这是一种假设您知道要使用正则化路径的方法,即您知道感兴趣的 Lambda。

    mod <- glmnet(x, y)
    LambdaIndex <- 50 # just randomly picking one of the 100 Lambas tried
    nonZerosIndices <- mod$beta[, LambdaIndex]!=0 
    nonZeroVars <- names(mod$beta[nonZerosIndices, LambdaIndex])
    

    【讨论】:

      猜你喜欢
      • 2022-01-14
      • 2016-04-26
      • 2014-05-22
      • 2019-08-30
      • 2014-10-06
      • 2015-03-04
      • 2016-09-29
      • 2019-07-25
      • 1970-01-01
      相关资源
      最近更新 更多