Solved – Predict function tuning for random forest

rrandom forest

I have created a random forest object in R (using the randomForest package) with ntree = N. Now I would like to predict some new data on it using a subset of N, that is using only n trees for the prediction. Is this possible?

For the random forest object the forest is located at fit$forest, but I don't know how to extract them (if possible).

Best Answer

Sounds like you want to set predict.all = TRUE. This will cause predict.randomForest to return a list containing a vector of the aggregate predictions and a matrix of the individual tree predictions. You can then ensemble the individual trees at your leisure.

library("randomForest")
data(mtcars)

rf <- randomForest(mpg ~ ., data = mtcars, ntree = 10)
preds <- predict(rf, newdata = mtcars, predict.all = TRUE)

preds$aggregate   # Aggregate predictions
preds$individual  # Invididual tree predictions

Make sure you set newdata = <something> or this trigger fails for some reason.

Related Question