Как исправить ошибки mtry в Boruta? - PullRequest
0 голосов
/ 29 января 2020

Я должен применить алгоритм Боруты с каретой. Борута была удалена из каретки, поэтому я применил эту функцию:

Boruta <- list(
label      = "Random Forest with Additional Feature Selection",

library    = c("Boruta", "randomForest"),

type       = c("Regression", "Classification"),

parameters = data.frame(
parameter = c('mtry'),
class     = c("numeric"),
label     = c("#Randomly Selected Predictors")
),

grid  = function(x, y, len = NULL, search = "grid") {
if (search == "grid") {
  out <- data.frame(mtry = caret::var_seq(
    p = ncol(x),
    classification = is.factor(y),
    len = len
  ))
  } else {
  out <- data.frame(
    mtry = unique(sample(1:ncol(x),
                         replace = TRUE,
                         size    = len
    ))
  )
  }
out
},

loop = NULL,

fit  = function(x, y, wts, param, lev, last, classProbs, ...) {
fs <- Boruta(x, y, mtry = param$mtry, ...)
keepers <- as.character(
  names(fs$finalDecision)[fs$finalDecision == "Confirmed"]
)
out <- randomForest(x[, keepers, drop = FALSE], y,
                    mtry = param$mtry, ...
)
out$Boruta <- fs
out
},

predict = function(modelFit, newdata, submodels = NULL) {
predict(modelFit, newdata)
},

prob    = function(modelFit, newdata, submodels = NULL) {
predict(modelFit, newdata, type = "prob")
},

tags = c(
"Tree-Based Model",
"Ensemble Model",
"Feature Selection Wrapper",
"Random Forest"
 ),

levels = function(x) x$classes,

sort   = function(x) x[order(x[, 1]), ]
)

После этого я вызвал Боруту в карету :: train

 boruta_time <- system.time(
 boruta_mem <- pryr::mem_change(
    boruta_model <- train(
        x = train_tree[-1],
        y = train_tree[[1]],
        method     = Boruta,
        metric     = "RMSE",
        maximize   = FALSE,
        trControl  = trainControl(
            method            = 'repeatedcv',
            number            = n_folds,
            repeats           = n_reps,
            search            = "grid",
            selectionFunction = "oneSE",
            savePredictions   = "final",
            seeds             = caret_seeds
        ),                   
        tuneGrid   = expand.grid(
            mtry = seq.int(
                from = 2,
                to   = 12  # simple bests are 5-10, oneSE best is 3
            )
        )
    )
  )
)

R возвращает мне эту ошибку: «Ошибка: mtry не может быть больше, чем количество переменных в данных. Ranger будет EXIT сейчас. "

Кто-нибудь, кто мог бы помочь это исправить? Заранее спасибо

...