How to do it...

In this recipe, we will work with the mushroom dataset that we used in the first recipe of this chapter and we will fit a random forest classifier. Random forests are built by combining the predictions of several (usually hundreds) of trees, trained using different subsets of features, and bootstrapped datasets obtained using the original data. The way these bootstrap samples are built, is by sampling with replacement; this implies that some of the observations might appear multiple times for each dataset, whereas some of them won't appear at all:

  1. We first load the mushroom dataset, and we assign the column names, as they are missing from the .csv file.Both the target and the features are categorical:
library(caret)set.seed(11) ...

Get R Statistics Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.