Open In App

Tree-Based Models Using R

Last Updated : 19 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Tree-based models are a group of supervised machine learning algorithms used for both classification and regression tasks. These models work by recursively splitting a dataset into smaller subsets based on certain feature values. The structure formed by these splits is represented as a decision tree. At each node, the algorithm makes a decision based on the values of one input feature. This process continues until the model reaches a final prediction at the leaf nodes.

One of the key benefits of tree-based models is their interpretability. Because the decision process is represented in tree form, it's comparatively simple to comprehend how predictions are generated. This is particularly helpful in areas such as healthcare or finance, where knowing the reasons for predictions are important.

Types of Tree Models

There are two types of tree models in tree-based models.

1. Classification trees

Classification tress are used in categorical data and hence predict categories..They use Gini index or entropy to measure impurity, by which they choose there levels and select features to give accurate predictions.

2. Regression trees

Regression trees are used in cases where the data is continuous hence they predict numerical values . They minimize squared error in child nodes, to make the predictions better.

Different Algorithms using Tree-Based Models

1. Decision Trees

A Decision tree is a basic tree-based algorithm used for both classification and regression. The tree is built by recursively splitting the data at each internal node based on the value of a feature. Each branch represents a decision rule, and each leaf node holds a class label or prediction. Decision trees are simple to interpret and can handle both numerical and categorical data. The Decision Tree algorithm recursively partitions the data based on the values of features and selects the best-split criteria to minimize the impurity or maximize the information gain.

Example of Decision Tree (Classifier) with the Iris Dataset

R
install.packages("rpart")
library(rpart)
data(iris)

iris.tree <- rpart(Species ~ ., data = iris,
                   method = "class")

plot(iris.tree, main = "Decision Tree for Iris Dataset")
text(iris.tree, use.n = TRUE,
     all = TRUE, cex = 0.8)

Output:

DT-1
Decision tree for iris dataset

Example of Decision Tree (Regressor) for Boston Housing Dataset

R
install.packages("rpart")
install.packages("MASS")
library(party)
library(MASS)
data(Boston)

boston.tree <- ctree(medv ~ ., data = Boston)

plot(boston.tree,
     main = "Decision Tree for Boston Housing Dataset")

Output:

Decision tree for Boston housing dataset
Decision tree for Boston housing dataset

2. Random Forest

Random Forest is an ensemble learning algorithm that aggregates many Decision Trees to achieve better performance and avoid overfitting. Random Forest randomly samples the data and features and trains various Decision Trees on subsets of the data. During prediction, the Random Forest aggregates the predictions of all the Decision Trees to make a final prediction. It can handle high-dimensional and noisy data and can handle both classification and regression tasks. It is widely used in various applications such as image recognition, text classification, and bioinformatics.

R
install.packages("randomForest")
library(randomForest)

data(iris)

iris.rf <- randomForest(Species ~ .,
                        data = iris)

varImpPlot(iris.rf)

Output:

random_forest
Variable importance plot for the iris dataset

3. Gradient Boosting

Gradient Boosting (GB) is a boosting method that builds an ensemble of Decision Trees by minimizing the loss function iteratively. GBM begins by training a Decision Tree on the data and then computing the residuals or the model errors. Next, it trains another Decision Tree on the residuals and sums the predictions of the new model to the existing model. This is done iteratively until a specified number of models is achieved or until the model's performance no longer improves.

4. Extreme Gradient Boosting (XGBoost)

XGBoost is a tuned version of the GB algorithm that utilizes a gradient-boosting method to enhance performance and minimize training time. XGBoost utilizes a number of techniques, including parallel processing, regularization,and tree pruning, to enhance the speed and accuracy of the algorithm. XGBoost can process big data, high-dimensional data, and  missing values, and has the ability to perform regression and classification tasks. XGBoost is used in many applications across different areas, including image recognition, natural language processing, and time-series forecasting.


Next Article

Similar Reads