# Predict function tuning for random 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). • Can you maybe explain why you want to do that? Generally the more trees you use, the better is your prediction. – PhilippPro Dec 27 '16 at 10:42 • Let's say I found that some trees are better than others so I only want to use the "good" ones. – user2974951 Dec 27 '16 at 10:45 • How did you find out, that they are better? – PhilippPro Dec 27 '16 at 10:47 • Suppose I did.. – user2974951 Dec 27 '16 at 10:54 ## 2 Answers 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.

• I had hoped for a more automated process but this will do just fine. – user2974951 Dec 27 '16 at 12:19
• More automated in what sense? – Dex Groves Dec 27 '16 at 12:28
• As in a parameter that you could set inside the predict function that would choose the subset. – user2974951 Dec 27 '16 at 12:37
• From the matrix, that's easy :). gist.github.com/DexGroves/d6c055addf870b30d678862cc0fa8a88 – Dex Groves Dec 27 '16 at 12:41
• I will use that. – user2974951 Dec 27 '16 at 12:47

Ok, so for your usecase you can set predict.all = TRUE when predicting on new data (see the help file via ?predict.randomForest). Then you get a list element called individual, which contains the prediction of each tree.