rmachine-learningxgboostr-carethyperparameters

Error in names(res$trainingData) %in% as.character(form[[2]]) : argument "form" is missing, with no default


When trying to tune the hyperparameters of an XGBoost model:

library(caret)

ctrl <- trainControl(
  method = "cv",      
  number = 2,
  verboseIter = TRUE,   
  search = "grid"     
)

param_grid <- expand.grid(
  nrounds = c(1000),          
  eta = c(0.01, 0.1, 0.3),              
  max_depth = c(3),              
  min_child_weight = c(1),     
  subsample = c(0.9),       
  colsample_bytree = c(0.9), 
  gamma = c(0)
)

xgb_tune <- train(
  formula = as.formula(paste("mpg", "~ .")),
  data = mtcars, 
  method = "xgbTree",
  trControl = ctrl,
  tuneGrid = param_grid
)

I get the following warning and error:

WARNING: src/learner.cc:767: 
Parameters: { "formula" } are not used.
Error in names(res$trainingData) %in% as.character(form[[2]]) : 
  argument "form" is missing, with no default

Solution

  • As warned, the train function does not include a formula argument, and you don't actually need it; change it to simply

    xgb_tune <- train(
      mpg~.,           # change here
      data = mtcars, 
      method = "xgbTree",
      trControl = ctrl,
      tuneGrid = param_grid
    )
    

    and you will be fine (just tested it).