3

I ran a xgboost regression forecast (also tried to complete it with the xgb.Booster.complete). When trying to get the xgb.importance, I get the error massage

Error in xgboost::xgb.importance(case_xgbm) : model: must be an object of class xgb.Booster

However, when verifying, R says it is an "xgb.Booster" class. Any idea what is going on?

library(xgboost)
library(caret)        
somedata <- MASS::Boston
    
    indexes = createDataPartition(somedata$medv, p = .85, list = F) #medv is the y
    train = somedata[indexes, ]
    test = somedata[-indexes, ]
    
    train_x = data.matrix(train[, -13])
    train_y = train[,13]
    
    xgb_train = xgb.DMatrix(data = train_x, label = train_y)
    xgbc = xgboost(data = xgb_train, max.depth = 2, nrounds = 50)
    class(xgbc)
    xgboost::xgb.importance(xgbc)
    
    xgbc2 = xgb.Booster.complete(xgbc, saveraw = TRUE)
    class(xgbc2)
    xgboost::xgb.importance(xgbc2)

1 Answers1

3

try xgboost::xgb.importance(model=xgbc) this worked for me

Isabel
  • 55
  • 5
  • 1
    Now it works for me as well. I wonder if it has something to do with me reinstalling the package yesterday –  Mar 11 '22 at 07:52
  • glad it worked for you! it only works for me when I specify that xgbc is a model – Isabel Mar 11 '22 at 08:08