# Applying randomforest algorithm (fit) on new data without recomputing the fit [closed]

I have a need to do realtime predictions for individual rows of data based on a previously computed randomForest algorithm. How can I run the "predict" command without recomputing "fit" on the entire training data set each time?

I am using R and here's the line of code that computes "fit" by applying the randomForest algorithm on the training set.

fit <- randomForest(formula2, data=training, importance=TRUE, ntree=2000, na.action = na.omit)


And here's the predict command - I want to be able to run this without having to recompute fit every time. Is this possible?

outp_rf <- predict(fit, testing)


For LogisticRegression, I know the coefficients so I can rerun the logistic function to compute the outcome. However not sure how I can do it for RandomForest.

• The predict() method doesn't refit the random Forest model at all, but instead uses the information in fit to generate predictions for new observations provided to argument newdata. Hence I am somewhat confused as to what you mean by this question. Is this just a misunderstanding of what the predict() method is doing or is your question more subtle than that? If the latter you will need to rephrase your question with more detail of what you foresee as your problem. Commented Mar 29, 2015 at 17:01
• I am interested in getting the value of "fit" without having to recompute the entire algorithm on training data. As you said "predict method uses the information in fit to generate predictions for new observations" - how do I get the "information in fit" without running the randomForest function (which can be time consuming) again? In other words, is there a way to store the information in "fit" in, say a database table, and reconstruct it so that I can use it in the predict function? Commented Mar 29, 2015 at 17:11
• That is what the predict() method does! The fit object contains and the trees of the forest and each of these has the rules. This is stored in a format that allows fast Fortran (or C) code to apply the rules to the new observations. Is your question how to do this outside of R? If you want to do this in R, predict() does everything you want to do. Outside of R, you may be out of luck. You should be able to extract the model structure in a PMML and then perhaps you can do something with that outside of R. Commented Mar 29, 2015 at 17:23
• Thank you for your answers. I understand how predict() works. I am looking to see if it's possible to avoid running the previous step which is the randomForest() function everytime before the predict() function. I think you answered my question though. PMML looks very promising and I'll look into it further. Looks like this will help me export my model (or the fit object) and reconstruct it later. Commented Mar 29, 2015 at 17:37
• You don't have recreate fit before wanting to predict from it. Just save (serialise) fit to disk and load the serialised object before you want to do the predictions. I would do: saveRDS(fit, "my-rf-object.rds"). Then when you want to do predictions, load randomForest then do fit <- loadRDS("my-rf-object.rds"). Then you can do predict(fit, testing) as if you'd just fitted the RF. For future reference, what would have clarified your Q (for me) would have been some mention of wanting to do predictions in new/other R sessions. Commented Mar 29, 2015 at 17:54

If you want to continue doing predictions in R, just serialise the fit object to disk and load that into your R session whenever you want to do some new predictions. The same would work for the logistic regression model as well.

For this I would use saveRDS(), but save() would work too:

saveRDS(fit, "my-fitted-rf.rds")

## or via save
save(fit, "my-fitted-rf.rda")


library("randomForest")

## if used saveRDS

predict(fit, testing)

save() stores the object with its name, but serialising via saveRDS doesn't. That can be an advantage if you already have an object fit in the workspace: save() would overwrite it...