Preparing the Test Dataset
Explore the process of preparing test datasets in R for random forest models. Learn to split data using stratified sampling, ensure factor levels match training data, and perform necessary transformations to maintain consistency for accurate model evaluation.
We'll cover the following...
Splitting the data
The first step of any machine learning project is splitting the data into training and test datasets. The training dataset is used throughout crafting machine learning models, including exploratory data analysis (EDA), feature engineering, training, and tuning. The test dataset is used at the end of the project as the final test of a machine learning model’s prediction quality.
The rsample package offers the initial_split(), training(), and testing() functions for splitting data. The following code demonstrates using the Adult Census Income dataset:
The initial_split() function randomly splits data into training and test datasets. As this data split is only performed once at the beginning of the project, it’s best practice to set the random seed via the set.seed() function to allow for reproducibility.
In the call to initial_split(), the prop parameter is set to use 80 ...