Random forest: bagging, feature sampling and out-of-bag error

Updated

A random forest is a collection of decision trees, each grown on a bootstrap sample of the rows and allowed to choose each split from only a random subset of the inputs. The forest predicts by averaging the trees (for a measured outcome) or by a majority vote (for a class). Averaging cancels most of the instability of a single tree, and the rows each tree did not see give a built-in estimate of the prediction error, called the out-of-bag error.

When to use it

  • You have tabular process data with several to many inputs and want good predictions with little tuning.
  • The relationships may be nonlinear, with thresholds and interactions you cannot specify in advance.
  • You want to rank inputs by how much they help prediction, as a guide to which ones deserve a designed experiment.
  • You have at least a few hundred rows. With a few dozen, a regression model or a single small tree is easier to trust.

How it works

  1. Bootstrap. Draw n rows with replacement from the n rows of data. Some rows appear twice or more; about 37% do not appear at all.
  2. Grow a tree with feature sampling. At every split, choose the best split among only m randomly picked inputs, not all p. Trees are grown deep, stopping at a small minimum leaf size.
  3. Repeat and average. Grow B trees this way. The forest's prediction is their average.
Prediction(x) = (1/B) Σ treeᵦ(x) Share of rows left out of a bootstrap sample = (1 − 1/n)ⁿ ≈ e⁻¹ ≈ 0.368 Var(average of B trees) = ρσ² + (1 − ρ)σ² / B

The last line explains the design. σ² is the variance of one tree's prediction and ρ the correlation between trees. More trees shrink only the second term; the first stays. Feature sampling lowers ρ: when strong inputs are not always available, the trees take different shapes, and their errors cancel better. Common starting values are m ≈ p/3 for a measured outcome and m ≈ √p for a class.

Out-of-bag (OOB) error. For each row, average the predictions of only the trees whose bootstrap sample left that row out, and compare with the actual value. This behaves like cross-validation without refitting.

Permutation importance. For each tree, shuffle one input in its out-of-bag rows and measure how much the error grows. An input the forest relies on causes a large increase; an unused one causes none.

Example

A molding plant has records for 200 parts from a process characterization: warpage (mm) with five inputs, melt temperature (220 to 260 °C), mold temperature (40 to 80 °C), hold pressure (400 to 800 bar), hold time (2 to 8 s) and cooling time (10 to 30 s). Warpage averages 1.29 mm with a standard deviation of 0.27 mm. The data are simulated, so the true structure is known: warpage jumps when cooling time is below 15 s, falls with hold pressure and rises with mold temperature, mainly when cooling is short. Melt temperature and hold time have small effects.

A forest of 300 trees, with 2 inputs tried at each split and at least 5 parts per leaf, gives an OOB RMSE of 0.112 mm and an OOB R² of 0.821. The individual trees of the forest, each judged on its own out-of-bag rows, reach only 0.178 mm. On average 36.9% of rows were out of bag for each tree, close to the theoretical 36.7% for n = 200.

Out-of-bag error versus number of trees, molded part warpage0.110.120.130.14OOB RMSE (mm)2 inputs per splitAll 5 inputs per split (bagging)102550100150200250300Number of trees
Out-of-bag error versus number of trees, molded part warpage

The error levels off by about 50 trees. The number of inputs per split matters more:

Inputs per split (m)12345
OOB RMSE (mm)0.1390.1120.1060.1060.107

Here only two inputs carry most of the signal, so with m = 1 many splits are forced onto weak inputs. m = 3 is best, and plain bagging (m = 5) is close behind.

Permutation importance (increase in OOB mean squared error, mm²) ranks the inputs in the order of the true model:

Permutation importance of each input0.000.010.020.030.04Increase in OOB MSE (mm²)Cooling timeHold pressureMold temperatureHold timeMelt temperature
Permutation importance of each input

How to read the results

  • OOB error is the honest number. The training error of a forest is always much lower and means little.
  • A flat OOB curve means enough trees. More trees cost time, never accuracy.
  • Importance is about prediction, not cause. Two correlated inputs share importance, and an input can rank high because it tracks the real cause.
  • To see the shape of an effect, plot the forest's average prediction as one input varies (a partial dependence plot).

Common mistakes

  • Treating repeated measurements as independent rows. If each part was measured three times, copies of the same part land both in and out of the bootstrap sample, and the OOB error looks far better than it is. Average the repeats first.
  • Extrapolating. A forest predicts a flat value beyond the range of the data.
  • Tuning m and the leaf size on the OOB error and then reporting that same error. It becomes slightly optimistic; confirm on held-out data.
  • Trusting split-count or impurity importance with mixed inputs. It favors inputs with many distinct values. Prefer permutation importance.

Frequently asked questions

How many trees do I need?

Enough for the OOB curve to flatten: here about 50 to 100. Use a few hundred if computing time is cheap, which makes importance values more stable.

Can a random forest overfit?

Adding trees does not increase the error. The trees themselves can fit noise, so a very small leaf size with noisy data can hurt; a minimum leaf size of about 5 is a common starting point for a measured outcome.

What is the difference between bagging and a random forest?

Bagging uses all p inputs at every split (m = p). A random forest uses m < p to make the trees less alike.

Can I get rules out of a forest?

Not directly; it is hundreds of trees. Use importance and partial dependence plots, or fit one small tree to the forest's predictions as an approximate summary.

Try it with your own data

StatLab SPC is free, open-source statistics for engineers. It runs in your browser, works offline, and never uploads your data.

Open StatLab