21
$\begingroup$

I'm currently fitting random forests for a classification problem using the randomForest package in R, and am unsure about how to report training error for these models.

My training error is close to 0% when I compute it using predictions that I get with the command:

predict(model, data=X_train)

where X_train is the training data.

In an answer to a related question, I read that one should use the out-of-bag (OOB) training error as the training error metric for random forests. This quantity is computed from predictions obtained with the command:

predict(model)

In this case, the OOB training error is much closer to the mean 10-CV test error, which is 11%.

I am wondering:

  1. Is it generally accepted to report OOB training error as the training error measure for random forests?

  2. Is it true that the traditional measure of training error is artificially low?

  3. If the traditional measure of training error is artificially low, then what two measures can I compare to check if the RF is overfitting?

$\endgroup$

2 Answers 2

17
$\begingroup$

To add to @Soren H. Welling's answer.

1. Is it generally accepted to report OOB training error as the training error measure for random forests?

No. OOB error on the trained model is not the same as training error. It can, however, serve as a measure of predictive accuracy.

2. Is it true that the traditional measure of training error is artificially low?

This is true if we are running a classification problem using default settings. The exact process is described in a forum post by Andy Liaw, who maintains the randomForest package in R, as follows:

For the most part, performance on training set is meaningless. (That's the case for most algorithms, but especially so for RF.) In the default (and recommended) setting, the trees are grown to the maximum size, which means that quite likely there's only one data point in most terminal nodes, and the prediction at the terminal nodes are determined by the majority class in the node, or the lone data point. Suppose that is the case all the time; i.e., in all trees all terminal nodes have only one data point. A particular data point would be "in-bag" in about 64% of the trees in the forest, and every one of those trees has the correct prediction for that data point. Even if all the trees where that data points are out-of-bag gave the wrong prediction, by majority vote of all trees, you still get the right answer in the end. Thus basically the perfect prediction on train set for RF is "by design".

To avoid this behavior, one can set nodesize > 1 (so that the trees are not grown to maximum size) and/or set sampsize < 0.5N (so that fewer than 50% of trees are likely to contain a given point $(x_i,y_i)$.

3. If the traditional measure of training error is artificially low, then what two measures can I compare to check if the RF is overfitting?

If we run RF with nodesize = 1 and sampsize > 0.5, then the training error of the RF will always be near 0. In this case, the only way to tell if the model is overfitting is to keep some data as an independent validation set. We can then compare the 10-CV test error (or the OOB test error) to the error on the independent validation set. If the 10-CV test error is much lower than the error on the independent validation set, then the model may be overfitting.

$\endgroup$
3
$\begingroup$

[edited 21.7.15 8:31 AM CEST]

I suppose you used RF for classification. Because in this case, the algorithm produces fully grown trees with pure terminal nodes of only one target class.

predict(model, data=X_train)

This line of coding is like a dog chasing [~66% of] its own tail. The prediction of any training sample is the class of the training sample itself. For regression RF stops if node has 5 or less samples in it or if node is pure. Here prediction error will be small but not 0%.

In machine learning we often work with large hypothesis spaces. This means there will always be many not yet falsified hypothesis/explanations/models to data structure of our training set. In classical statistics is the hypothesis space often small and therefore the direct model-fit is informative accordingly to some assumed probability theory. In machine learning does the direct lack-of-fit relate to the bias of the model. Bias is the "inflexibility" of the model. It does not in anyway provide a approximation of generalization power(the ability to predict new events). For algorithmic models cross-validation is the best tool to approximate generalization power, as no theory is formulated. However, if model assumptions of independent sampling fail, the model may be useless anyhow, even when a well performed cross-validation suggested otherwise. In the end, the strongest proof is to satisfyingly predict a number external test-sets of various origin.

Back to CV: Out-of-bag is often a accepted type of CV. I would personally hold that OOB-CV provides similar results as 5-fold-CV, but this is a very small nuisance. If to compare let's say RF to SVM, then OOB-CV is not usefull as we would normally avoid to bag SVM. Instead then both SVM and RF would be embedded in the exact same cross-validation scheme e.g. 10-fold 10-repeats with matching partitions for each repeat. Any feature engineering steps would often also be needed to be cross-validated. If to keep things clean the entire data pipe-line could be embedded in the CV.

If you tune your model with your test-set(or cross-validation) you're again inflating your hypothesis space and the validated prediction performance is likely over-optimistic. Instead you will need a calibration-set(or calibration CV-loop) to tune and a test validation set(or validation CV-loop) to assess your final optimal model.

In the extreme sense, your validation score will only be unbiased if your never act on this result, when you see it. This is the paradox of validation, as why would we obtain a knowledge which is only true if you do not act on it. In practice the community willingly accepts some publication bias, where those researchers who got a over-optimistic validation at random are more likely to publish, than those who unluckily good a over-pessimistic validation. Therefore sometimes why can't reproduce others models.

$\endgroup$
4
  • $\begingroup$ Thank you for this. Two questions: (1) How is the prediction of any training sample the class of the training sample itself? There must be trees that do not use the training sample that are used in the majority vote procedure no? (2) So how would you assess if an RF measure is overfitting? $\endgroup$
    – Berk U.
    Commented Jul 20, 2015 at 23:49
  • $\begingroup$ (1) well yes I was a little fast there, I edited the answer. Any sample will be inbag in ~66% of the trees. If a sample in a tree was inbag during training, it will end up in that same node during prediction in ~66%, and that is enough to win majority vote every time. Confirm this by lowering sampsize to e.g. 30% of train samples, and notice a rise from 0% classification error. (2) I think I answered that. RF does not per se overfit, but you can create a context during e.g. tuning where you will get a over-optimistic OOB-CV result. Embed your entire process in a new CV to know for sure. $\endgroup$ Commented Jul 21, 2015 at 6:48
  • $\begingroup$ Imagine that we train a RF with a training dataset and we saved the model, and we forgot to save the training dataset. So we lost the training dataset.... But one day we meet it again without knowing that it is the training dataset, what should we do to evaluate the this dataset? $\endgroup$
    – John Smith
    Commented Nov 1, 2020 at 18:16
  • $\begingroup$ the learner might store some information e.g. the target vector or accuracy metrics. Given you have some prior on where your datasets come from and understand the process of random forest, then you can compare the old trained RF-model with a new model trained on the candidate dataset. If the new and old model share enough similarities you may argue it is unlikely the models has not been trained on the same or almost same data. If you're being very nerdy about it, you realize RF models segmentize the feature space into hypercubes and each observation must fall into a set of cubes. $\endgroup$ Commented Nov 2, 2020 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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