调整参数网格应该有列mtry

问题描述 投票:1回答:1

我正试图通过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。

它发生的任何可能原因?怎么解决?

r r-caret
1个回答
1
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.