Gru
Gru

Reputation: 95

{caret}xgbTree model not running when weights included, runs fine without them

I have a dataset off which I have no problem building an xgbTree model without weights, but once I include weights -- even if the weights are just all 1 -- the model doesn't converge. I get the Something is wrong; all the RMSE metric values are missing: error and when I print the warnings, I get In nominalTrainWorkflow(x = x, y = y, wts = weights, info = trainInfo, ... :There were missing values in resampled performance measures. as the last message.

This is a drive link to the RData file containing the info -- it was too big to print, and smaller samples didn't always reproduce the error.

It contains 3 objects: input_x, input_y, and wts -- the last one is just a vector of 1s, but it should eventually it should be able to accept numbers on the interval (0,1), ideally. The code I used is shown below. Note the comment next to the weight argument that produces the error.

nrounds<-1000

tune_grid <- expand.grid(
  nrounds = seq(from = 200, to = nrounds, by = 50),
  eta = c(0.025, 0.05, 0.1, 0.3),
  max_depth = c(2, 3, 4, 5),
  gamma = 0,
  colsample_bytree = 1,
  min_child_weight = 1,
  subsample = 1
)

tune_control <- caret::trainControl(
  method = "cv", 
  number = 3, 
  verboseIter = FALSE, 
  allowParallel = TRUE 
)

xgb_tune <- caret::train(
    x = input_x,
    y = input_y,
    weights = wts, # If I remove this line, the code works fine. When included, even if just 1s, it throws an error.
    trControl = tune_control,
    tuneGrid = tune_grid,
    method = "xgbTree",
    verbose = TRUE
  )

Upvotes: 1

Views: 562

Answers (2)

missuse
missuse

Reputation: 19756

EDIT 13.10.2021. thanks to @waterpolo

The correct way to specify weights is via the weights argument to caret::train

xgb_tune <- caret::train(
    x = input_x,
    y = input_y,
    weights = wts,
    trControl = tune_control,
    tuneGrid = tune_grid,
    method = "xgbTree",
    verbose = TRUE
  )

see a more verbose answer here: Non-tree model error when using xgbTree method with Caret and weights to target variable when applying the varImp function

Old incorrect answer below:

According to the function source weights argument is called wts.

Line:

if (!is.null(wts))
  xgboost::setinfo(x, 'weight', wts)

Running

xgb_tune <- caret::train(
    x = input_x,
    y = input_y,
    wts = wts,
    trControl = tune_control,
    tuneGrid = tune_grid,
    method = "xgbTree",
    verbose = TRUE
  )

should produce the desired result.

Upvotes: 2

waterpolo
waterpolo

Reputation: 36

Just wanted to add @missuse response from another post (Non-tree model error when using xgbTree method with Caret and weights to target variable when applying the varImp function). The correct argument is weights .

Code:

xgb_tune <- caret::train(x = input_x,
    y = input_y,
    weights = wts,
    trControl = tune_control,
    tuneGrid = tune_grid,
    method = "xgbTree",
    verbose = TRUE
  )

The other thing that I found was that I needed to use weights > 1 or I would receive the same error message as you. For example, if I used inverse weighting I would receive the same message as you. Hope this helps.

Thanks @missuse for the lovely response in the other thread!

Upvotes: 0

Related Questions