从插入符递归特征消除 (rfe) 结果中检索选定的变量

2024-03-25

在我的工作项目中,我使用 caret 包中的 rfe 函数来进行递归特征消除。我用一个玩具例子来说明我的观点。

library(mlbench)
library(caret)
data(PimaIndiansDiabetes)

rfFuncs$summary <- twoClassSummary
control <- rfeControl(functions=rfFuncs, method="cv", number=10)
results <- rfe(PimaIndiansDiabetes[,1:8], PimaIndiansDiabetes[,9], sizes=c(1:8), rfeControl=control, metric="ROC")

选择的最佳变量基于在过程中给出最高 auroc 的那些变量,并且可以通过以下方式检索results$optVariables。 但是,我想做的是使用“1 个标准错误规则”来选择较少的功能(代码如下)。识别的变量数量为 4。

# auc that is 1-se from the highest auc 
df.results = results$results %>% dplyr::mutate(ROCSE = ROCSD/sqrt(10-1))
idx = which.max(df.results$ROC)
ROC.1se = df.results$ROC[idx] - df.results$ROCSE[idx]

# plot ROC vs feature size
g = ggplot(df.results, aes(x=Variables, y=ROC)) + 
    geom_errorbar(aes(ymin=ROC-ROCSE, ymax=ROC+ROCSE), 
                  width=.2, alpha=0.4, linetype=1) +
    geom_line() + 
    geom_point()+
    scale_color_brewer(palette="Paired")+
    geom_hline(yintercept = ROC.1se)+
    labs(x ="Number of Variables", y = "AUROC")
print(g) 

我确定的变量数量是 4。现在我需要知道是哪四个变量。我做了如下:

results$variables %>% filter(Variables==4) %>% distinct(var)

它显示了 5 个变量!

有谁知道我如何检索这些变量?基本上它适用于获取任意数量的选定变量的这些变量。

预先非常感谢!


一行回答

如果您知道您只需要 rfe 重采样中最好的 4 个变量,这将为您提供所需的内容。

results$optVariables[1:4]
# [1] "glucose"  "mass"     "age"      "pregnant"

dplyr Answer

# results$variables %>%
#    group_by(var) %>%
#    summarize(Overall = mean(Overall)) %>%
#    arrange(-Overall)
#
# A tibble: 8 x 2
#   var      Overall
#   <chr>      <dbl>
# 1 glucose    34.2 
# 2 mass       15.8 
# 3 age        12.7 
# 4 pregnant    7.92
# 5 pedigree    5.09
# 6 insulin     4.87
# 7 triceps     3.25
# 8 pressure    1.95

为什么你的尝试给出了超过 4 个变量

您正在过滤 40 个观察值。最好的 4 个变量的 10 倍。每次折叠中最好的 4 个变量并不总是相同。因此,为了在重新采样中获得最佳的前 4 个变量,您需要像上面的代码那样在折叠中平均它们的性能。更简单的是,里面的变量optVariables按此顺序排序,因此您可以只获取前 4 个(如我的单行答案所示)。要证明这种情况,需要深入研究源代码(如下所示)。

详细信息:深入研究源代码

首先处理从函数返回的对象,例如rfe是尝试类似的功能print, summary, or plot。通常会存在自定义方法,它们将为您提供非常有用的信息。例如...

# Run rfe with a random seed
# library(dplyr)
# library(mlbench)
# library(caret)
# data(PimaIndiansDiabetes)
# rfFuncs$summary <- twoClassSummary
# control <- rfeControl(functions=rfFuncs, method="cv", number=10)
# set.seed(1)
# results <- rfe(PimaIndiansDiabetes[,1:8], PimaIndiansDiabetes[,9], sizes=c(1:8), 
# rfeControl=control, metric="ROC")
# 
# The next two lines identical...
results
print(results)
# Recursive feature selection
#
# Outer resampling method: Cross-Validated (10 fold)
#
# Resampling performance over subset size:
#
# Variables    ROC  Sens   Spec   ROCSD  SensSD  SpecSD Selected
#          1 0.7250 0.870 0.4071 0.07300 0.07134 0.10322         
#          2 0.7842 0.840 0.5677 0.04690 0.04989 0.05177         
#          3 0.8004 0.824 0.5789 0.02823 0.04695 0.10456         
#          4 0.8139 0.842 0.6269 0.03210 0.03458 0.05727         
#          5 0.8164 0.844 0.5969 0.02850 0.02951 0.07288         
#          6 0.8263 0.836 0.6078 0.03310 0.03978 0.07959         
#          7 0.8314 0.844 0.5966 0.03075 0.04502 0.07232         
#          8 0.8316 0.860 0.6081 0.02359 0.04522 0.07316        *
#
# The top 5 variables (out of 8):
#    glucose, mass, age, pregnant, pedigree

嗯,这给出了 5 个变量,但你说你想要 4 个。我们可以很快地深入到源代码中,探索它是如何计算并返回这 5 个变量作为前 5 个变量的。

print(caret:::print.rfe)
#
# Only a snippet code shown below...
#    cat("The top ", min(top, x$bestSubset), " variables (out of ", 
#        x$bestSubset, "):\n   ", paste(x$optVariables[1:min(top, 
#            x$bestSubset)], collapse = ", "), "\n\n", sep = "")

所以,基本上它是直接从results$optVariables。那里的人口如何增加?

# print(caret:::rfe.default)
#
# Snippet 1 of code...
#    bestVar <- rfeControl$functions$selectVar(selectedVars, 
    bestSubset)
#
# Snippet 2 of code...
#        bestSubset = bestSubset, fit = fit, optVariables = bestVar,

Ok, optVariables正在被填充rfeControl$functions$selectVar.

print(rfeControl)
#
# Snippet of code...
# list(functions = if (is.null(functions)) caretFuncs else functions, 

从上面我们可以看出caretFuncs$selectVar正在使用...

详细信息:正在填充的源代码optVariables

print(caretFuncs$selectVar)
# function (y, size)
# {
#    finalImp <- ddply(y[, c("Overall", "var")], .(var), function(x) mean(x$Overall, 
#        na.rm = TRUE))
#    names(finalImp)[2] <- "Overall"
#    finalImp <- finalImp[order(finalImp$Overall, decreasing = TRUE), 
#        ]
#    as.character(finalImp$var[1:size])
# }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从插入符递归特征消除 (rfe) 结果中检索选定的变量 的相关文章

随机推荐