Skip to content

Saving a Random Forest Model

2 messages · Lorenzo Isella, Rui Barradas

#
Dear All,
Please consider the snippet at the end of the email
The output of the randomForest model is rf1 (i.e. the trained model).
Now, is there a way to save this rf1 so that, in a different R session, I  
can just load it without repeating the analysis?
Of course I have in mind much more CPU-intensive simulations than in this  
artificial example (where it would be really unpractical to recalculate  
everything and the model has to be applied to different data sets at  
different time or needs to be combined with other models).
Any suggestion is welcome.
Cheers

Lorenzo



######################################
# simulate the data
x1=rnorm(1000)
x2=rnorm(1000,x1,1)
y=2*x1+rnorm(1000,0,.5)
df=data.frame(y,x1,x2,x3=rnorm(1000),x4=rnorm(1000),x5=rnorm(1000))
# run the randomForest implementation
library(randomForest)
rf1 <- randomForest(y~., data=df, mtry=2, ntree=50, importance=TRUE)
####################################
#
Hello,

?save can save any R object. To load it back into R, use ?load.

Hope this helps,

Rui Barradas

Em 23-03-2013 10:35, Lorenzo Isella escreveu: