如何从稳定性选择模型输出中提取特定值

时间:2016-12-06 17:24:14

标签: r extract

我有以下摘要输出,我想从选定的变量中提取结果(仅变量名= X10)。

>  stab.glmnet
    Stability Selection with unimodality assumption

Selected variables:
X10 
10 

Selection probabilities:
X2   X1   X7   X3   X6   X4   X5   X8   X9  X10 
0.02 0.06 0.20 0.22 0.25 0.32 0.35 0.37 0.41 1.00 

---
Cutoff: 0.75; q: 3; PFER (*):  0.918 
(*) or expected number of low selection probability variables
PFER (specified upper bound):  1 
PFER corresponds to signif. level 0.0918 (without multiplicity adjustment)

我试过(下面),但它只给了我另一个值10

var <- stab.glmnet$selected[[1]]

数据:

set.seed(1001)
n <- 100
Y <- c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,    0,0,0,0,0,0,0,0,0)
X1 <- sample(x=c(0,1,2), size=n, replace=TRUE, prob=c(0.1,0.4,0.5))
X2 <- sample(x=c(0,1,2), size=n, replace=TRUE, prob=c(0.5,0.25,0.25))
X3 <- sample(x=c(0,1,2), size=n, replace=TRUE, prob=c(0.3,0.4,0.4))
X4 <- sample(x=c(0,1,2), size=n, replace=TRUE, prob=c(0.35,0.35,0.3))
X5 <- sample(x=c(0,1,2), size=n, replace=TRUE, prob=c(0.1,0.2,0.7))
X6 <- sample(x=c(0,1,2), size=n, replace=TRUE, prob=c(0.8,0.1,0.1))
X7 <- sample(x=c(0,1,2), size=n, replace=TRUE, prob=c(0.1,0.1,0.8))
X8 <- sample(x=c(0,1,2), size=n, replace=TRUE, prob=c(0.35,0.35,0.3))
X9 <- sample(x=c(0,1,2), size=n, replace=TRUE, prob=c(0.35,0.35,0.3))
X10 <- c(2,2,2,2,2,2,2,2,2,2,1,2,2,2,1,2,2,1,2,2,2,2,2,2,2,2,2,2,1,2,2,2,2,
2,1,2,1,1,2,1,2,1,2,1,2,1,1,2,1,2,0,0,0,0,0,0,0,0,1,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,   
1,1,1,1,1,0,0,0,0,0,0,0,1,0,0,0,0)

datasim <- data.frame(Y=as.factor(Y),X1,X2,X3,X4,X5,X6,X7,X8,X9,X10)

包和稳定性选择代码

library("stabs")
library("glmnet")
x <- model.matrix(Y~.,datasim)[,-1]
y <- datasim$Y
y <- as.numeric(y)

stab.glmnet <- stabsel(x,y ,fitfun = glmnet.lasso, cutoff = 0.75,PFER = 1)

1 个答案:

答案 0 :(得分:3)

运行代码时出错。在任何情况下,如果您查看print.stabsel函数,您可以看到每个摘要输出所在的模型对象中的位置。 print.stabsel的代码粘贴在下面。

如果您想要,例如,所选变量的选择概率,您可以看到所选变量的索引可以在stab.glmnet$selected中找到。选择概率在stab.glmnet$max中。所以我们可以做到以下几点:

stab.glmnet$max[stab.glmnet$selected]

要查看模型对象中的其他内容,请查看str(stab.glmnet)的输出。

print.stabsel的代码:

getAnywhere(print.stabsel)
function (x, decreasing = FALSE, print.all = TRUE, ...) 
{
    cat("\tStability Selection")
    if (x$assumption == "none") 
        cat(" without further assumptions\n")
    if (x$assumption == "unimodal") 
        cat(" with unimodality assumption\n")
    if (x$assumption == "r-concave") 
        cat(" with r-concavity assumption\n")
    if (length(x$selected) > 0) {
        cat("\nSelected variables:\n")
        print(x$selected)
    }
    else {
        cat("\nNo variables selected\n")
    }
    cat("\nSelection probabilities:\n")
    if (print.all) {
        print(sort(x$max, decreasing = decreasing))
    }
    else {
        print(sort(x$max[x$max > 0], decreasing = decreasing))
    }
    cat("\n---\n")
    print.stabsel_parameters(x, heading = FALSE)
    cat("\n")
    invisible(x)
}