
A decision tree uses rules based on features in the data. They help make predictions or classify data by breaking it into smaller steps. In this article, we will learn how to build decision trees in R.
How Decision Trees Work
Decision trees break down data into smaller parts through a series of questions, as outlined below:
- Input data: The process begins with the entire dataset. This data contains different features.
- Ask questions: At each step, the tree asks about one of the features.
- Split the data: Based on the answer to the question, the data is divided into two groups. This helps to separate different results.
- Create nodes: Each question becomes a node in the tree. The nodes show the decisions made from the data.
- Reach leaf nodes: Eventually, the process ends at leaf nodes. These nodes represent the final predictions. Each leaf shows the result based on the questions answered along the way.
- Final decision: To make a prediction, follow the path from the top of the tree to a leaf. The leaf node at the end shows the final prediction based on the input data.
Installing and Loading Libraries
Before creating decision trees in R, you should install and load the required libraries. We will use two main packages:
- rpart: This package helps you create decision trees.
- rpart.plot: This package is used to visualize the decision trees you create.
# Install packages
install.packages("rpart")
install.packages("rpart.plot")
# Load the libraries
library(rpart)
library(rpart.plot)
Preparing the Data
Here, we will use the iris dataset. It contains measurements of iris flowers.
# Load the iris dataset data(iris) # View the first few rows head(iris)

Splitting the Data
It’s important to split your data into training and testing sets to evaluate the model’s performance later.
# Set a random seed for reproducibility set.seed(123) # Load the iris dataset data(iris) # Determine the number of rows n <- nrow(iris) # Calculate the number of training samples (70% of the dataset) train_size <- round(0.7 * n) # Randomly sample row indices for the training set train_indices <- sample(1:n, train_size) # Create training and testing datasets iris_train <- iris[train_indices, ] iris_test <- iris[-train_indices, ]
Building the Decision Tree Model
Now, we can create the decision tree model using the rpart function.
# Build the decision tree model decision_tree <- rpart(Species ~ ., data = iris_train, method = "class") # View the model print(decision_tree)

Visualizing the Decision Tree
Visualizing the decision tree helps us understand how it makes decisions. We use the rpart.plot function to create a visual representation of our decision tree.
# Visualize the decision tree rpart.plot(decision_tree)
This will create a picture of the decision tree. The nodes show the different decisions made based on the feature values.

Making Predictions
Now that we have built our decision tree model, we can use it to make predictions.
# Make predictions on the test set predictions <- predict(decision_tree, iris_test, type = "class") # View the predictions print(predictions)

Evaluating the Model
To evaluate the performance of our model, we can create a confusion matrix to compare the predicted species with the actual species.
# Create a confusion matrix confusion_matrix <- table(iris_test$Species, predictions) # View the confusion matrix print(confusion_matrix)

Tuning Decision Trees
Decision trees can be tuned to improve performance by adjusting various parameters. For instance:
- cp (Complexity Parameter): Controls the size of the decision tree by pruning weak splits.
- minsplit: The minimum number of observations required to split a node.
- maxdepth: The maximum depth of the tree.
You can tune these parameters using the rpart.control() function.
# Tune the decision tree using rpart.control control_params <- rpart.control(minsplit = 20, maxdepth = 5, cp = 0.01) # Train a new decision tree with tuned parameters tuned_tree_model <- rpart(Species ~ ., data = iris, method = "class", control = control_params) # Visualize the tuned decision tree rpart.plot(tuned_tree_model)

Conclusion
Decision trees are a useful tool for making predictions and classifications. They are easy to understand and visualize. You can build and evaluate your decision trees by following the steps laid out in the article.
