mercredi 13 mai 2020

R - random forest not properly loading

I'm having issues loading a random forest model and applying it to a raster with raster::predict.

Normally, when I create a random forest model in an R session, type its name and hit enter I receive the following print out:

> rf_model
Call:
 randomForest(formula = AGB_mean ~ B1_med + B2_med + B4_med +      B5_med + B6_med + B7_med + B1_sd + B2_sd + B4_sd + B5_sd +      B6_sd + B7_sd + NDVI + EVI + EVI2, data = all.training, importance = TRUE, na.action = na.roughfix) 
               Type of random forest: regression
                     Number of trees: 500
No. of variables tried at each split: 5

          Mean of squared residuals: 4866.287
                    % Var explained: 52.48

When I apply this in-session model to a raster using predict, I can make successful predictions.

When I instead load a saved random forest model using readRDS and type the model name like so:

> rf_model <- readRDS('model.rds')
> rf_model

I receive a full print out of all the information in rf_model (i.e. rf_model$call, rf_model$type... rf_model$terms), and when I try to make predictions I receive the following error:

Error in UseMethod("predict") : 
  no applicable method for 'predict' applied to an object of class "c('randomForest.formula', 'randomForest')"

Is there something I'm missing here on properly loading a random forest object?




Aucun commentaire:

Enregistrer un commentaire