Search code examples
rloadingpredictgbmdismo

Why can't I use "predict" after I load a gbm computed with gbm.step?


I am creating a boosted regression tree using the function gbm.step:

M <- gbm.step(data = DF, 
              gbm.x = c("X1","X2","X3","X4","X5","X6"),
              gbm.y = "PresAbs", 
              family ="bernoulli",
              tree.complexity = 3, learning.rate = 0.01, bag.fraction = 0.6,
              plot.main = FALSE, verbose = FALSE) 

The model runs fine, and once I run it, I can make predictions on my test dataset to validate my output using:

TestDF$pred <- predict(M, TestDF, n.trees=M$gbm.call$best.trees, type="response")

However, I also want to save my model so that I can go back to it and make more predictions. The model is pretty big (R studio says it is a "Large gbm" file, with 41 elements and 73MB), and I save it using saveRDS:

saveRDS(M, "Mymodel.rds")

The rds file is large (237 MB). I can load it just fine on a new R studio session (using readRDS()), but when I try to make predictions (using the same predict() function as above), I get the following message:

Error in UseMethod("predict") : 
  no applicable method for 'predict' applied to an object of class "gbm"

This is weird to me, as the only thing that I am doing differently is to load the model instead of running it. I'd like to be able to avoid rerunning it each time I want to use it, as it takes ~2hrs to compute. Do you have an idea of what could be causing the issue / what could the solution be?

Thanks a lot for your help!


Solution

  • I found how to solve the issue: I needed to load the package "gbm". Somehow I didn't have to load it after having used the function gbm.step (this function probably loads it itself). Hope this can save someone else some trouble!