Decision Tree in R Programming

The decision tree uses the branching method to show every possible output for the specific input. We can draw a decision tree by hand or create it using specialized software or a graphics program. A decision tree is a supervised machine learning technique that performs both tasks, i.e., regression and classification.

A decision tree graph is useful for focusing discussion when a group has to decide. A decision tree is a probability tree that enables the user to decide about any process. A decision tree is interpreted by nodes, branches and leaf nodes. Each attribute of the tests is represented at the nodes, and the outcome is defined at the branches. The class labels are represented at the leaf nodes.

An example of the decision tree is as follows:

  • Choosing the best manufacturing item amongst all the items. i.e., item A, item B, item C or item D.
  • Choosing the best stock for long-term investing, i.e., stock A, stock B, stock C or stock D.
  • Checking whether an individual is healthy or unhealthy.

Working of Decision Tree:

DECISION TREE IN R PROGRAMMING
  • PARTITIONING:
    • Partitioning is the process of splitting the data set into subsets.
    • Algorithms such as chi-square and Gini index are used for partitioning purposes, and the algorithm whose efficiency is best is chosen.
  • PRUNING:
    • In this process, branch nodes are converted into leaf nodes, and it results in the shortening of the branches of the tree.
    • We can avoid the concept of overfitting by simpler trees.
  • SELECTION OF TREE:
    • In this process, our main aim is to select the smallest tree which fits the data.

Important factors to consider while selecting the tree in R programming:

  • Entropy
  • Information Gain

To better understand the decision tree concept, we will take an example.

EXAMPLE:

CODE 1:

# DECISION TREE
# CODE:
# BEFORE STARTING THE DECISION TREE, WE WILL IMPORT THE NEEDED LIBRARY INTO OUR DATASET.
# AS MENTIONED BELOW:


library(datasets)
library(caTools)
library(party)
library(dplyr)
library(magrittr)

OUTPUT 1:

DECISION TREE IN R PROGRAMMING

CODE 2:

# DECISION TREE
# CODE:
# We will import the reading skills dataset in RStudio as shown below:


print(data("readingSkills"))


# head() function will print starting rows of our dataset.
head(readingSkills)

OUTPUT 2:

DECISION TREE IN R PROGRAMMING

CODE 3:

# DECISION TREE
# EXAMPLE


# Here, in this step, we are loading the party package. 
# It will automatically load other dependent and required packages.
library(party)


# Creating the input data frame in this step.
input.data <- readingSkills[c(1:105),]


# In this step, we are creating the tree.
output.tree <- ctree(
  nativeSpeaker ~ age + shoeSize + score, 
  data = input.data)


# At last, we are plotting the tree.
plot(output.tree)

OUTPUT 3:

DECISION TREE IN R PROGRAMMING

CONCLUSION:

From the above decision tree, we can conclude that anyone whose reading skills score is less than 38.3 and the age is more than six is not considered a native speaker.


Related Topics

R Basic Data Types

Data types are used to define the size and type of the variable. In R, there is no need to declare a variable as some data types. The variables are...

4 minutes read.

Normal Distribution in R Programming

In statistics, the normal distribution is a type of probability function. Normal distribution tells the user about the distribution of data values in the dataset. It is a very important...

3 minutes read.

R If Else Statement

If statement It is the most simple decision-making statement. It checks the condition if the condition is true then only it will execute the block of statements written in the ‘if...

2 minutes read.

R Operators

A symbol that tells the compiler to perform specific mathematical or logical operations is called operator. R language supports mainly 5 different types of operators, which are listed below: Arithmetic...

3 minutes read.

Poisson Distribution in R Programming

Poisson distribution is a type of distribution that deals with the probability distribution of the data values by taking the mean into consideration. Poisson distribution will estimate the probability value for...

3 minutes read.

Linear Regression in R Programming

Linear Regression is an unsupervised machine learning algorithm in R programming. Linear Regression is a type of regression analysis that shows the relationship between two or more variables. And one...

3 minutes read.

R Variables, Constants and Reserved Words

R Variables Variables are very similar to an open box, where you can put any value of your wish. We can change the values as well. Variables are used to store...

4 minutes read.

Measures of Central Tendency in R Programming

Central Tendency or CT is one of the features of descriptive statistics. The Central tendency will let us know how the various groups of data are clustered around the central...

4 minutes read.

R Tutorial

Introduction to R Programming R is nothing but a programming language, also a free software environment. This software used for predictive analysis, statistical analysis, graphical representation, data modeling, and reporting. R...

9 minutes read.

Analysis of Covariance in R Programming

Analysis of Covariance can also be named ANCOVA. We know that we use the concept of regression analysis for creating models which can explain the effect of the variation in...

2 minutes read.

Logistic Regression in R Programming

Logistic Regression is a classification supervised machine learning algorithm in R programming. Logistic Regression can also be termed Binomial Logistic Regression, Binary Logistic Regression, or Logit Model. Logistic Regression is...

4 minutes read.

R Functions

In programming, a function is a group of instructions that you want to use repeatedly, because of their complexity, are better self-contained in sub-program and called when needed. Hence, we can...

4 minutes read.

First R Program

First  Hello World R Program As a convention, our first R program will be the “Hello World!” program. We can run our R program either at R command prompt or we...

3 minutes read.

R Date and Time

R provides very large range of capabilities to deal with times and dates. Generally, dates are internally stored as integers, and depending on the operations we will need, we could...

3 minutes read.

Bubble Chart in R Programming

In this article, we will talk about another type of chart in R programming, i.e., the Bubble chart. A bubble chart is a type of data visualization chart that helps display multiple...

4 minutes read.

How to make Boxplots in R

R Boxplots Boxplot is a measure of how well the data is distributed in a data set. It is used to give a summary of one or several numeric variables. The...

3 minutes read.

R Line Graphs

Line graph or line chart is a graph that connects a series of points by drawing segments between them. A line graph represents the relationship between 2 variables. These points...

2 minutes read.

Clustering in R Programming

The clustering technique is an unsupervised machine learning in R programming. Before discussing clustering techniques, let's have a look at what unsupervised machine learning is?  Unsupervised learning is training the model with...

3 minutes read.

Binomial Distribution in R Programming

In this article, we will talk about the Binomial distribution in R programming. The binomial distribution is a type of probability distribution. As it is a discrete distribution, it will...

3 minutes read.

How to create Histograms in R

R Histograms A Histogram is the graphical representation of the distribution of numeric data. It takes only one numeric variable as input. The variable is cut into several bars (also called...

2 minutes read.