Skip to content

Parameter tuning with train fails when using 'method = xgbTree' #1412

Description

@CeresBarros

I've been a user of caret for several years now and want to start by saying, THANKS for the awesome package.

I've recently started using it to tune parameters for an XGBoost model, but so far without success.
I think I've traced the issue to a problem with assigning colnames(x) to the xgboost model object.

Here's a reprex:

library(caret)
install.packages('xgboost', repos = c('https://dmlc.r-universe.dev'))

data(iris)
param_grid1 <- data.frame(nrounds = 200,
                          ## defaults in xgboost:
                          max_depth = 6,
                          eta = c(0.01, 0.1, 1),
                          gamma = 0,
                          colsample_bytree = 1,
                          min_child_weight = 1,
                          subsample = 1)

xgb_trcontrol <- trainControl(
  method="cv",
  number = 5,
  verboseIter = TRUE,
  returnData = FALSE,
  returnResamp = "all",
  allowParallel = TRUE,
  savePredictions = "final"
)

xgb_train_1 <- train(Species ~ .,
                     data = iris,
                     trControl = xgb_trcontrol,
                     tuneGrid = param_grid1,
                     method="xgbTree")

the output

+ Fold1: eta=0.01, max_depth=6, gamma=0, colsample_bytree=1, min_child_weight=1, subsample=1, nrounds=200 
model fit failed for Fold1: eta=0.01, max_depth=6, gamma=0, colsample_bytree=1, min_child_weight=1, subsample=1, nrounds=200 Error in modelFit$xNames <- colnames(x) : 
  ALTLIST classes must provide a Set_elt method [class: XGBAltrepPointerClass, pkg: xgboost]
 
(...)
- Fold5: eta=0.10, max_depth=6, gamma=0, colsample_bytree=1, min_child_weight=1, subsample=1, nrounds=200 
+ Fold5: eta=1.00, max_depth=6, gamma=0, colsample_bytree=1, min_child_weight=1, subsample=1, nrounds=200 
model fit failed for Fold5: eta=1.00, max_depth=6, gamma=0, colsample_bytree=1, min_child_weight=1, subsample=1, nrounds=200 Error in modelFit$xNames <- colnames(x) : 
  ALTLIST classes must provide a Set_elt method [class: XGBAltrepPointerClass, pkg: xgboost]
 
- Fold5: eta=1.00, max_depth=6, gamma=0, colsample_bytree=1, min_child_weight=1, subsample=1, nrounds=200 
Aggregating results
Something is wrong; all the Accuracy metric values are missing:
    Accuracy       Kappa    
 Min.   : NA   Min.   : NA  
 1st Qu.: NA   1st Qu.: NA  
 Median : NA   Median : NA  
 Mean   :NaN   Mean   :NaN  
 3rd Qu.: NA   3rd Qu.: NA  
 Max.   : NA   Max.   : NA  
 NA's   :3     NA's   :3    
Error: Stopping
In addition: There were 46 warnings (use warnings() to see them)

And my session info:

R version 4.5.1 (2025-06-13 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 22631)

Matrix products: default
  LAPACK version 3.12.1

locale:
[1] LC_COLLATE=English_United Kingdom.utf8  LC_CTYPE=English_United Kingdom.utf8   
[3] LC_MONETARY=English_United Kingdom.utf8 LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.utf8    

time zone: America/Vancouver
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] caret_7.0-1    lattice_0.22-7 ggplot2_3.5.2 

loaded via a namespace (and not attached):
 [1] future_1.58.0        generics_0.1.4       class_7.3-23         stringi_1.8.7       
 [5] pROC_1.19.0.1        listenv_0.9.1        digest_0.6.37        magrittr_2.0.3      
 [9] grid_4.5.1           timechange_0.3.0     RColorBrewer_1.1-3   iterators_1.0.14    
[13] jsonlite_2.0.0       xgboost_3.0.5.1      foreach_1.5.2        plyr_1.8.9          
[17] Matrix_1.7-3         e1071_1.7-16         ModelMetrics_1.2.2.2 nnet_7.3-20         
[21] survival_3.8-3       purrr_1.0.4          scales_1.4.0         codetools_0.2-20    
[25] lava_1.8.1           cli_3.6.5            rlang_1.1.6          hardhat_1.4.2       
[29] parallelly_1.45.0    future.apply_1.20.0  splines_4.5.1        withr_3.0.2         
[33] prodlim_2025.04.28   tools_4.5.1          parallel_4.5.1       reshape2_1.4.4      
[37] dplyr_1.1.4          recipes_1.3.1        globals_0.18.0       vctrs_0.6.5         
[41] R6_2.6.1             rpart_4.1.24         proxy_0.4-27         stats4_4.5.1        
[45] lifecycle_1.0.4      lubridate_1.9.4      stringr_1.5.1        MASS_7.3-65         
[49] pkgconfig_2.0.3      pillar_1.10.2        gtable_0.3.6         glue_1.8.0          
[53] data.table_1.17.8    Rcpp_1.0.14          tibble_3.3.0         tidyselect_1.2.1    
[57] rstudioapi_0.17.1    farver_2.1.2         nlme_3.1-168         ipred_0.9-15        
[61] timeDate_4041.110    gower_1.0.2          compiler_4.5.1  

Activity

  1. selbouhaddani commented on Dec 24, 2025

    @selbouhaddani

    Background
    I have the same issue, and after some digging, I suspect that the problem is how caret handles xgboost attributes. The result of xgb.train() doesn't have 'normal' list elements that can be accessed with fit$name. However, when looking into caret:::varImpDependencies("xgbTree"), it does try to access elements that don't exist.

    Suspected issue
    The main issue in your (and my) workflow is probably after fitting, when caret calls caret:::varImpDependencies("xgbTree")$prob(fit, newdata = dmatrix), which results in a more informative error, namely

    Error in matrix(out, ncol = length(modelFit$obsLevels), byrow = TRUE) : 
      data is too long
    Called from: matrix(out, ncol = length(modelFit$obsLevels), byrow = TRUE)
    

    Inspecting this line of code reveals that modelFit$obsLevels doesn't exist and its length is 0L. Note that this specific function caret:::varImpDependencies may only be used by varImp, but it's likely that the core of the problem is the same.

    Potential solution
    I think that the solution is to internally adjust this code to use xgboost::xgb.config(fit) as much as possible, or any other function used by xgboost to access the relevant elements. I don't know enough about caret to confirm that this is really the issue, but bypassing the if (length(modelFit$obsLevels) == 2) { seems to work.

  2. John-Polo commented on Mar 19, 2026

    @John-Polo

    Same issue as you. You might consider switching to a new workflow. As the age of the updates in the codebase indicate, this library is in maintenance mode. I say that based on the comment from Mr. Flick here: https://stackoverflow.com/questions/79849114/new-version-of-xgboost-package-is-not-working-under-caret-environment

  3. CeresBarros commented on Mar 19, 2026

    @CeresBarros
    Author

    Thank you @John-Polo and @selbouhaddani.
    Unfortunately switching the workflow is not doable in this particular case, but good to know that tidymodels can accomodate XGBoost.

    I have since forked the caret repository and found a way to fix the problem for the XGBoost implementation that I needed.
    I can't guarantee that it'll will work for all XGBoost implementations, but it's a start.

    Feel free to see if it works for you:
    https://github.com/CeresBarros/caret

  4. bappa10085 commented on Sep 21, 2026

    @bappa10085

    @topepo I kindly request you to resolve this issue, as you have said here that you are actively working on caret bug fixes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions