ecpolley / SuperLearner

Current version of the SuperLearner R package
272 stars 72 forks source link

Problem when predicting from loaded SL object #144

Open tkasci opened 2 years ago

tkasci commented 2 years ago

Hi, I built an SL model and saved it to disk. However, it contains a BART learner, so I get this error when trying to do anything with it:

Error in check_serialization(object) : 
  This bartMachine object was loaded from an R image but was not serialized.
  Please build bartMachine using the option "serialize = TRUE" next time.

What would be the best way to fix this? Is there a way to pass options to individual commands via SuperLearner?

ecpolley commented 2 years ago

Hi, you might need to modify the SL.bartMachine wrapper to add the serialize = TRUE argument and retrain the SL model. Here is an example new wrapper (and then use SL.mybartMachine in the SL.library):

SL.mybartMachine <- function(Y, X, newX, family, obsWeights, id, num_trees = 50, 
    num_burn_in = 250, verbose = FALSE, alpha = 0.95, beta = 2, k = 2, 
    q = 0.9, nu = 3, num_iterations_after_burn_in = 1000, ...) 
{
require("bartMachine")
model = bartMachine::bartMachine(X = X, 
                                y = Y, 
                                num_trees = num_trees, 
                                num_burn_in = num_burn_in, 
                                verbose = verbose, 
                                alpha = alpha, 
                                beta = beta, 
                                k = k, 
                                q = q, 
                                nu = nu, 
                                num_iterations_after_burn_in = num_iterations_after_burn_in,
                                serialize = TRUE)
    pred <- predict(model, newX)
    fit <- list(object = model)
    class(fit) <- c("SL.bartMachine")
    out <- list(pred = pred, fit = fit)
    return(out)
}