Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save a random Forest object

Tags:

r

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?

like image 352
screechOwl Avatar asked Oct 23 '11 02:10

screechOwl


People also ask

How do you save RF models?

In R, after running "random forest" model, I can use save. image("***. RData") to store the model. Afterwards, I can just load the model to do predictions directly.

How do you select the number of trees in random forest?

Summary. It is important to tune the number of trees in the Random Forest. To tune number of trees in the Random Forest, train the model with large number of trees (for example 1000 trees) and select from it optimal subset of trees. There is no need to train new Random Forest with different tree numbers each time.


1 Answers

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
like image 124
joran Avatar answered Oct 13 '22 05:10

joran