R语言 如何将一行二进制指示符转换为LASSO选择的一行不同(标称)标记的变量

gcmastyq  于 2022-12-30  发布在  其他
关注(0)|答案(1)|浏览(103)

我有一组候选回归变量(又称自变量或“IVs”),这些变量是由我刚刚以以下形式运行的LASSO回归选择的:

True_IVs
  X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 X16 X17 X18 X19 X20
1  0  0  0  1  0  0  0  0  0   1   0   0   1   0   0   0   0   0   1   1
  X21 X22 X23 X24 X25 X26 X27 X28 X29 X30
1   0   1   0   0   1   1   1   1   0   1

dput(True_IVs)
structure(list(X1 = "0", X2 = "0", X3 = "0", X4 = "1", X5 = "0", 
    X6 = "0", X7 = "0", X8 = "0", X9 = "0", X10 = "1", X11 = "0", 
    X12 = "0", X13 = "1", X14 = "0", X15 = "0", X16 = "0", X17 = "0", 
    X18 = "0", X19 = "1", X20 = "1", X21 = "0", X22 = "1", X23 = "0", 
    X24 = "0", X25 = "1", X26 = "1", X27 = "1", X28 = "1", X29 = "0", 
    X30 = "1"), row.names = 1L, class = "data.frame")

其中,True_IVs是“数据.frame”,其结构为:

'data.frame':   1 obs. of  30 variables:
 $ X1 : chr "0"
 $ X2 : chr "0"
 $ X3 : chr "0"
 $ X4 : chr "1"
 $ X5 : chr "0"
 $ X6 : chr "0"
 $ X7 : chr "0"
.
.
.
 $ X27: chr "1"
 $ X28: chr "1"
 $ X29: chr "0"
 $ X30: chr "1"

我希望将此输出转换为与在Console中生成以下输出的内容等效的内容,以便于对这些选择进行评分:

True_Regressors
[1] "X4" "X10" "X13" "X19" "X20" "X22" "X25" "26" "X27" "X28" "30"

澄清一下,上面的代码不是实际的代码,而是我希望创建的表单的伪代码。
到目前为止,我所尝试的是下面的内容以及控制台中的相应输出:

True_Regressors <- do.call(
   rbind.data.frame,
   lapply(
   paste("X", True_IVs),
     function(x) {
       s <- paste(x, "")
       c(s[[1]], s[[2]]) } ))

#Error in s[[2]] : subscript out of bounds
xfb7svmp

xfb7svmp1#

您可以简单地执行以下操作:

names(True_IVs)[True_IVs == 1]

相关问题