【问题标题】:Error extracting non-zero glmnet coefficients with "which"使用“which”提取非零 glmnet 系数时出错
【发布时间】:2020-01-27 13:40:21
【问题描述】:

我用 glmnet 安装了一个 cox 模型,例如:

fitL <- glmnet(
  X,
  Y,
  family = "cox",
  alpha = 1,
  lambda = cvL$lambda.min, #cvL obteined with cv.glmnet
  standardize = FALSE,
  thresh = thresh
)

我得到了:

str(coef(fitL) != 0)
Formal class 'lgCMatrix' [package "Matrix"] with 6 slots
  ..@ i       : int [1:24] 0 76 81 96 125 149 213 266 277 415 ...
  ..@ p       : int [1:2] 0 24
  ..@ Dim     : int [1:2] 1000 1
  ..@ Dimnames:List of 2
  .. ..$ : chr [1:1000] "001" "002" "003" "004" ...
  .. ..$ : chr "s0"
  ..@ x       : logi [1:24] TRUE TRUE TRUE TRUE TRUE TRUE ...
  ..@ factors : list()

我想提取非零系数(即所选变量),我使用了“which”,但出现了这个错误:

> which (coef (fitL)! = 0) 

base :: which (x, arr.ind, useNames, ...): 'which' 参数必须是逻辑类型

我还使用了coefplot 建议的here 包的extract.coef 函数。我有这个错误:

> library (coefplot)
> coefplot :: extract.coef (fitL)

UseMethod 中的错误(通用 =“extract.coef”,对象 = 模型):不 适用于“c”类对象的“extract.coef”方法 ('coxnet', 'glmnet')"

【问题讨论】:

    标签: r glmnet coefficients cox


    【解决方案1】:

    使用predict 代替参数type="nonzero"。

    【讨论】:

      【解决方案2】:

      对象(coef(fitL) != 0) 具有类lgCMatrix。尝试使用as.vector

      【讨论】:

      • 谢谢你的回答,它的工作:which(as.vector(coef(fitL) != 0)) [1] 1 77 82 97 126 150 214 267 278 416 522 617 621 701 714 810 822 858 859 [20] 876 886 ....
      猜你喜欢
      • 2019-07-25
      • 2020-06-05
      • 2021-09-26
      • 2020-11-15
      • 2017-06-07
      • 2013-09-08
      • 2018-05-23
      • 1970-01-01
      • 2015-03-04
      相关资源
      最近更新 更多