importance ranking: error must be an object of class xgb.Booster

733 Views Asked by At

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

There are 1 best solutions below

2
On

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