1

I'm trying to use ranger via Caret. Interestingly, it pops out an error message:

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

So I check:

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

Codes I used:

 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"
 )

The codes work on my local PC Rstudio (when I used a small sample of data), but not on a virtual machine Rstudio.

Any possible reason why it happens? How to fix it?

Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
LeGeniusII
  • 900
  • 1
  • 10
  • 28
  • 3
    try doing: model_grid <- expand.grid(.mtry = mtry, .splitrule = "gini", .min.node.size = 10) – missuse Jan 17 '18 at 08:23
  • Not working unfortunately, but thanks for the suggestion. – LeGeniusII Jan 18 '18 at 03:56
  • I tried it and it worked on my machine, Did you add a `.` prior to each parameter name? – missuse Jan 18 '18 at 06:06
  • 1
    consider installing caret from github: `devtools::install_github('topepo/caret/pkg/caret')` and the latest ranger library on CRAN. – missuse Jan 18 '18 at 10:25
  • Possible duplicate of [R: using ranger with caret, tuneGrid argument](https://stackoverflow.com/questions/48334929/r-using-ranger-with-caret-tunegrid-argument) – arvi1000 Mar 05 '18 at 17:01

1 Answers1

1

I encountered the similar problem. It's solved after I perform a reinstallation of caret from GitHub using devtools.

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

HappyCoding
  • 5,029
  • 7
  • 31
  • 51