简体   繁体   English

R:具有自定义变量重要性 permimp 的 Caret rfe

[英]R: Caret rfe with a customized variable importance permimp

I want to run a recursive feature elimination with caret rfe() with the alternative variable importance algorithm permimp .我想使用 caret rfe()和替代变量重要性算法permimp运行递归特征消除。 The permimp permutation importance uses cforest with cforest_unbiased() . permimp置换重要性使用cforestcforest_unbiased() Which other caret functions do I need to customize in order run rfe with permimp() and cforest ?为了使用permimp()cforest运行rfe ,我还需要自定义哪些其他插入符号函数?

This is my code so far:到目前为止,这是我的代码:

library(caret)

permimpRFE <- list(summary = defaultSummary,
                   fit = function(x, y, first, last, ...){
                     library(party)
                     tmp <- as.data.frame(x, stringsAsFactors = TRUE)
                     tmp$y <- y
                     party::cforest(y ~ ., data = tmp,
                                    control = party::cforest_unbiased(ntree = 50))
                   },
                   pred = function(object, x)  predict(object, x),
                   rank = function(object, x, y) {
                     library(permimp)
                     vimp <- permimp::permimp(object, conditional = TRUE, threshold = .95, do_check = FALSE)
                     vimp <- as.data.frame(vimp$values)
                     colnames(vimp) <- "Overall"
                     vimp <- vimp[order(vimp$Overall, decreasing = TRUE),, drop = FALSE]
                     if (ncol(x) == 1) {
                       vimp$var <- colnames(x)
                     } else vimp$var <- rownames(vimp)
                     vimp
                   },
                   selectSize = pickSizeBest,
                   selectVar = pickVars)


# specify rfeControl
contr <- caret::rfeControl(functions=permimpRFE, method="repeatedcv", number=3, repeats=2, 
                           saveDetails = TRUE)

dat <- as.data.frame(ChickWeight)[1:50,]

preds <- dat[,2:4]
response <- dat[,1]

# recursive feature elimination caret (Algorithm 2)
set.seed(43, kind = "Mersenne-Twister", normal.kind = "Inversion")
results <- caret::rfe(x = preds, 
                      y = response,
                      sizes=c(1:3),
                      metric= "RMSE", 
                      rfeControl=contr)

I get the error Error in {: task 1 failed - "invalid 'x' type in 'x && y'"我收到Error in {: task 1 failed - "invalid 'x' type in 'x && y'"

How can I get the rfe running with permimp and cforest ?如何让rfepermimpcforest运行?

When trying to customize the variable importance function for rfe() , it is important to implement the exact syntax of the desired function as well as its dependent functions.当尝试为rfe()自定义变量重要性 function 时,实现所需 function 及其相关函数的确切语法非常重要。

In my case, I had to change predict(object, x) to predict(object, newdata = x) .就我而言,我不得不将predict(object, x)更改为predict(object, newdata = x)

Now the code snippet is working:现在代码片段正在工作:

library(caret)

permimpRFE <- list(summary = defaultSummary,
                   fit = function(x, y, first, last, ...){
                     library(party)
                     tmp <- as.data.frame(x, stringsAsFactors = TRUE)
                     tmp$y <- y
                     party::cforest(y ~ ., data = tmp,
                                    control = party::cforest_unbiased(ntree = 50))
                   },
                   pred = function(object, x) {
                     x <- as.data.frame(x, stringsAsFactors = TRUE)
                     predict(object, newdata = x)
                   },
                   rank = function(object, x, y) {
                     library(permimp)
                     vimp <- permimp::permimp(object, conditional = TRUE, threshold = .95, do_check = FALSE)
                     vimp <- as.data.frame(vimp$values)
                     colnames(vimp) <- "Overall"
                     vimp <- vimp[order(vimp$Overall, decreasing = TRUE),, drop = FALSE]
                     if (ncol(x) == 1) {
                       vimp$var <- colnames(x)
                     } else vimp$var <- rownames(vimp)
                     vimp
                   },
                   selectSize = pickSizeBest,
                   selectVar = pickVars)


# specify rfeControl
contr <- caret::rfeControl(functions=permimpRFE, method="repeatedcv", number=3, repeats=2, 
                           saveDetails = TRUE)

dat <- as.data.frame(ChickWeight)[1:50,]

preds <- dat[,2:4]
response <- dat[,1]

# recursive feature elimination caret (Algorithm 2)
set.seed(43, kind = "Mersenne-Twister", normal.kind = "Inversion")
results <- caret::rfe(x = preds, 
                      y = response,
                      sizes=c(1:3),
                      metric= "RMSE", 
                      rfeControl=contr)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM