Training a random forest model is inherently non-deterministic (absent control over the random number generator), but is predict() also non-deterministic? That is, if I construct randomForest (with an odd number for ntree per the caveat in the doc) and save an .rda, will loading that .rda give me identical results given identical inputs?

I tried answering this myself by looking through https://github.com/cran/randomForest , and it seems that nobody along the predict() path is calling for a random number, but I'm very new to R and rather rusty with C and may be missing something. Pointers into the code or the docs will be appreciated.

  • $\begingroup$ The model should indeed provide the same output if you supply the same input. You aren't retraining or adjusting any of the trees. The input samples are simply being tested across each tree and an average/consensus is taken. $\endgroup$ – zacdav Sep 26 '17 at 4:18

The model will not change unless you re-train it. The same input sample should always have the same output value for a given model. changing the seed, saving and reloading, etc should have no impact on the results.

The training itself is indeed non-deterministic, predict is not.

# sample 80% of data to train
split = sample(1:nrow(iris), floor(nrow(iris) * 0.8))
df_train = iris[split,]
df_test = iris[-split,]
# rf model
mod = randomForest(Species ~ ., df_train)
# predictions
res1 = predict(mod, df_test)
res2 = predict(mod, df_test)
identical(res1, res2)
  • $\begingroup$ @DaveW.Smith I'm not entirely sure this is correct. The iris data is small, well separated, and you're comparing categorical predictions not probabilities. I've definitely seen differences in predict based on seed, at least using the caret package. $\endgroup$ – HEITZ Sep 27 '17 at 22:21
  • $\begingroup$ @HEITZ then demonstrate it. If your predictions involve preprocessing specific to the input then yea that'd maybe change it but unless you can demonstrate... $\endgroup$ – zacdav Sep 27 '17 at 22:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.