调整参数网格应该有列mtry

时间:2018-01-17 02:20:20

标签: r r-caret

我正在尝试通过Caret使用游侠。有趣的是,它弹出一条错误信息:

Error in train.default(x <- as.matrix(train_data[, !c(excludeVar), with = FALSE]),  : 
  The tuning parameter grid should have columns mtry

所以我检查一下:

> model_grid
  mtry splitrule min.node.size
1    5      gini            10

我用的代码:

 mtry <- round(sqrt(ncol(train_data) - 3),0)      # ignore ID fields and target fields

 # parameters
 model_grid <- expand.grid(
   mtry = mtry                                    # mtry specified here
   ,splitrule = "gini"
   ,min.node.size = 10
 )
 model_trcontrol <- trainControl(
   method = "cv",
   number = 2,                                     
   search = "grid",
   verboseIter = FALSE,
   returnData = FALSE,
   savePredictions = "none",          
   classProbs = TRUE,
   summaryFunction = twoClassSummary,
   sampling = "up",                                # over-sampling
   allowParallel = TRUE
 )

 # training
 targetVar = target_fields[i]
 excludeVar = c(ID_fields,targetVar)
 model_train <- train(
   x <- as.matrix(train_data[,!c(excludeVar),with = FALSE]),
   y <- eval(parse(text = paste0("as.factor(train_data$",targetVar,")"))),
   trControl = model_trcontrol,
   tuneGrid = model_grid,
   method = "ranger"
 )

这些代码适用于我的本地PC Rstudio(当我使用一小部分数据时),但不适用于虚拟机Rstudio。

出现这种情况的任何可能原因?如何解决?

1 个答案:

答案 0 :(得分:1)

我遇到了类似的问题。在我使用devtools从GitHub重新安装插入符后,它已经解决了。

devtools::install_github('topepo/caret/pkg/caret')