I am using R package randomForest to build a Random Forest model for classification. Ultimately, I need to choose one of five programs for a group of individuals based on historical data. The final variable that is being predicted is a categorical "1, 2, 3, 4, or 5" variable.

I need to incorporate 30-40 variables--their importance will also be analyzed-- to reach a decision for each individual.

I have no problem training a model, initially. Below is a sample data set being trained with R's randomForest package. I am running up-to-date versions of both the package and RStudio.

train <- sample(1:nrow(Boston), 300)
Boston.rf <- randomForest(medv~., data = Boston, subset = train)

My question is: Once I have a model trained, tested, and cross-validated, how do I actually apply new data to this model? Is it viable to use that many variables--all categorical, some binary--for this?


1 Answer 1


The randomForest package supports various tasks using an existing randomForest object.

For instance, it offers the predict method which will perform prediction using a given trained forest and a given dataset. As an example, if you have a trained forest called mod and a dataset called my_data, predictions can be obtained by running predict(object=mod,newdata=my_data).

It's worth reading through the package documentation which gives good examples of using the functionality.

I would note that any new data you supply to a trained forest object should have the same encodings and transformations as the data used to train the forest with.

  • $\begingroup$ Thank you for the reply! Do you happen to know the difference between predict() and rfImpute()? I feel more inclined to use rfImpute() as it remains in the randomForest package, but this is still a new topic to me. Thanks! $\endgroup$
    – Canbo
    Commented May 23, 2019 at 13:47
  • $\begingroup$ rfImpute and predict are two different applications. rfImpute imputes missing data in the predictor variables (i.e. takes care of Na values in your dataset) while predict creates predictions of the target outcome using a (clean) dataset. $\endgroup$
    – bradS
    Commented May 23, 2019 at 14:07
  • 1
    $\begingroup$ Both functions are "in" the randomForest package (I believe R masks certain functions depending on the packages read in, so will use the randomForest version rather than the base stats version). If you want to ensure that the predict function you use is in randomForest, you can use randomForest::predict(object=mod,newdata=my_data). $\endgroup$
    – bradS
    Commented May 23, 2019 at 14:10
  • $\begingroup$ That makes sense. Since the model is not imputing missing historical data, but rather making predictions based on historical data, would predict be more appropriate? Does it make a difference between predict and rfImpute if I am filling in "Person A is more likely to choose Program 1"? Also, I was able to unmask predict using : randomForest:::predict.randomForest(). Thank you so much. $\endgroup$
    – Canbo
    Commented May 23, 2019 at 14:21
  • $\begingroup$ "More likely to choose" sounds like a prediction task, so predict would be more appropriate. $\endgroup$
    – bradS
    Commented May 23, 2019 at 14:32

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.