2
$\begingroup$

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).

$\endgroup$
  • $\begingroup$ Can you maybe explain why you want to do that? Generally the more trees you use, the better is your prediction. $\endgroup$ – PhilippPro Dec 27 '16 at 10:42
  • $\begingroup$ Let's say I found that some trees are better than others so I only want to use the "good" ones. $\endgroup$ – user2974951 Dec 27 '16 at 10:45
  • $\begingroup$ How did you find out, that they are better? $\endgroup$ – PhilippPro Dec 27 '16 at 10:47
  • $\begingroup$ Suppose I did.. $\endgroup$ – user2974951 Dec 27 '16 at 10:54
1
$\begingroup$

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.

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

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.

$\endgroup$

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.