Save a random Forest object
I'm not sure exactly what you're trying to do here, since normally you save
an object and then load
it later, like this:
set.seed(71)
> irisrf <- randomForest(Species ~ ., data=iris, importance=TRUE,
+ proximity=TRUE)
> save(irisrf,file = "irisrf.RData")
>
> rm(irisrf)
> print(irisrf)
Error in print(irisrf) : object 'irisrf' not found
>
> load("irisrf.RData")
> print(irisrf)
Call:
randomForest(formula = Species ~ ., data = iris, importance = TRUE, proximity = TRUE)
Type of random forest: classification
Number of trees: 500
No. of variables tried at each split: 2
OOB estimate of error rate: 4.67%
Confusion matrix:
setosa versicolor virginica class.error
setosa 50 0 0 0.00
versicolor 0 47 3 0.06
virginica 0 4 46 0.08
Here is a solution if you would like to load the model under another name
library(randomForest)
# 1. Create data set
set.seed(100)
df_iris <- randomForest(Species ~ ., data = iris, importance = TRUE, proximity = TRUE)
# 2. Save model
file_name <- "model_iris.rds"
saveRDS(df_iris, file_name)
# 2.3. Load model under another name
df_iris_loaded <- readRDS(file_name)
df_iris_loaded
# 2.4. Test two models
identical(df_iris, df_iris_loaded, ignore.environment = TRUE)
I had the same problem (loading RandomForest object resulted in character string) and something like this seemed to have worked for me:
forest = get(load("forestGOOG.RData"))
(I have a random forest object 'forestGOOG' saved in the working directory)