# Random Forest Usage

Random Forest Usage: I have run random forest in R. It gave me confusion matrix and variable importance. Variable importance can be used to rank importance of variables in the model. My question is how i can use random forest for classification, like decision tree. I have also run decision tree. It gave me RULES to classify my data. Can random forest produce same classification? Or any kind of classification criteria? Did i miss something in random forest code?

Yes, you can use random forests to get predictions, but it's not something that you can easily do "by hand" as for a classification tree.

Random forests generate a large number of classification trees, and then does a majority vote to generate prediction so you don't get a single "set of rules" like in a classification tree.

For instance:

library(randomForest)

# Set RNG seed, for reproducibility
set.seed(12345)

# We'll use the iris data
# Let's split it into 30% test set and 70% training set
training.set <- sample(seq(nrow(iris)), nrow(iris)*0.7)

# Generate a random forest with 500 trees
rf <- randomForest(Species~., iris[training.set,], ntree = 500)

print(rf)


Which gives:

        OOB estimate of  error rate: 3.81%
Confusion matrix:
setosa versicolor virginica class.error
setosa         37          0         0  0.00000000
versicolor      0         32         3  0.08571429
virginica       0          1        32  0.03030303


Now, to test it on the test set

res <- predict(rf, iris[-training.set,1:4])
table(real=iris[-training.set, 5], predicted=res)

predicted
real         setosa versicolor virginica
setosa         13          0         0
versicolor      0         15         0
virginica       0          3        14


If you want to look at single trees used by your random forest you can use the getTree function.

You may also want to have a look at:

How to actually plot a sample tree from randomForest::getTree()?