To use random forest models for classification, follow these steps:
- Load the randomForest and caret packages:
> library(randomForest) > library(caret)
- Read the data and convert the response variable to a factor:
> bn <- read.csv("banknote-authentication.csv") > bn$class <- factor(bn$class)
- Select a subset of the data to build the model. In random forests, we do not need to actually partition the data for model evaluation as the tree construction process has partitioning inherent in every step. However, we keep aside some of the data here just to illustrate the process of using the model for prediction and also to get an idea of the model's performance:
> set.seed(1000) > sub.idx <- createDataPartition(bn$class, p=0.7, ...