Reputation: 28169
I have a randomForest object that I want to save for later use. I've tried some of the following but with no luck.
save(topDawg , file="myRFobject.RData")
This just saves a string "topDawg"
> formula(topDawg)
Error in formula.default(topDawg) : invalid formula
> save(getTree(topDawg))
Error in save(getTree(topDawg)) : object ‘getTree(topDawg)’ not found
Any suggestions?
Upvotes: 15
Views: 19717
Reputation: 91
Once a random forest model is created save the model as a file so that you can reuse it later.
#model
model_rf <- randomForest(y~ ., data = dataset, mtry=7,ntree=500)
#write model
saveRDS(model_rf , "model_rf.RDS")
#load model
readRDS("model_rf.RDS")
Upvotes: 0
Reputation: 3043
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)
Upvotes: 3
Reputation: 51
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)
Upvotes: 5
Reputation: 173677
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
Upvotes: 27