EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials R Programming Tutorial Decision Tree in R
Secondary Sidebar
R programming Tutorial
  • Advanced
    • Statistical Analysis with R
    • R String Functions
    • Data Exploration in R
    • R CSV Files
    • KNN Algorithm in R
    • Sorting in R
    • lm Function in R
    • Hierarchical Clustering in R
    • R Normal Distribution
    • Binomial Distribution in R
    • Decision Tree in R
    • GLM in R
    • Arima Model in R
    • Linear Model in R
    • Predict Function in R
    • Survival Analysis in R
    • Standard Deviation in R
    • Statistical Analysis in R
    • Predictive Analysis?in R
    • T-test in R
    • Database in R
  • Basic
    • What is R Programming Language
    • Careers in R Programming
    • Install R
    • List of R Packages
    • Introduction of R Tools Technology
    • R Programming Language
    • DataSet in R
    • What is RStudio?
    • R-studio-Functions
    • R Packages
    • Time series?in R
    • R Data Types
    • R for data science
    • R Operators
    • R Data Frame
    • R Analytics Tool
    • R Tree Package
    • Vectors in R
  • Control statement
    • If Statement in R
    • If Else Statement in R
    • Else if in R
    • Switch Statement in R
  • Loops
    • Loops in R
    • For Loop in R
    • Nested For Loop in R
    • While Loop in R
    • Next in R
  • Chart/graphs
    • Graphs in R
    • Bar Charts in R
    • Pie Chart in R
    • Histogram in R
    • Line Graph in R
    • Plot Function in R
    • Scatterplot in R
    • R Boxplot labels
  • Regression in R
    • Simple Linear Regression in R
    • Linear Regression in R
    • Multiple Linear Regression in R
    • Logistic Regression in R
    • Poisson Regression in R
    • OLS Regression in R
    • P-Value in Regression
  • Anova in R
    • ANOVA in R
    • One Way ANOVA in R
    • Two Way ANOVA in R
  • Data Structure
    • R list
    • Arrays in R
    • Data Frames in R
    • Factors in R
    • R Vectors
  • Programs
    • Functions in R
    • Boxplot in R
    • R Program Functions
    • Factorial in R
    • Random Number Generator in R
  • Interview question
    • R Interview Questions

Related Courses

R Programming Certification Course

Statistical Analysis Course Training

All in One Data Science Courses

Decision Tree in R

By Priya PedamkarPriya Pedamkar

decision tree in R

Overview of Decision Tree in R

Decision Tree in R is a machine-learning algorithm that can be a classification or regression tree analysis. The decision tree can be represented by graphical representation as a tree with leaves and branches structure. The leaves are generally the data points and branches are the condition to make decisions for the class of data set. Decision trees in R are considered as supervised Machine learning models as possible outcomes of the decision points are well defined for the data set. It is also known as the CART model or Classification and Regression Trees. There is a popular R package known as rpart which is used to create the decision trees in R.

Decision tree in R

To work with a Decision tree in R or in layman terms it is necessary to work with big data sets and direct usage of built-in R packages makes the work easier. A decision tree is non- linear assumption model that uses a tree structure to classify the relationships. The Decision tree in R uses two types of variables: categorical variable (Yes or No) and continuous variables. The terminologies of the Decision Tree consisting of the root node (forms a class label), decision nodes(sub-nodes), terminal node (do not split further). The unique concept behind this machine learning approach is they classify the given data into classes that form yes or no flow (if-else approach) and represents the results in a tree structure. The algorithm used in the Decision Tree in R is the Gini Index, information gain, Entropy. There are different packages available to build a decision tree in R: rpart (recursive), party, random Forest, CART (classification and regression). It is quite easy to implement a Decision Tree in R.

For clear analysis, the tree is divided into groups: a training set and a test set. The following implementation uses a car dataset. This data set contains 1727 obs and 9 variables, with which classification tree is built. In this article lets tree a ‘party ‘package. The function creates () gives conditional trees with the plot function.

Implementation using R

The objective is to study a car data set to predict whether a car value is high/low and medium.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

i. Preparing Data

Installing the packages and load libraries

This module reads the dataset as a complete data frame and the structure of the data is given as follows:

data<-car // Reading the data as a data frame
str(data) // Displaying the structure and the result shows the predictor values.

Output:

Prepareing data

Determining Factordata$vhigh<-factor(data$vhigh)> View(car)
> data<-car

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,650 ratings)

ii. Partition a data

Splitting up the data using training data sets. A decision tree is split into sub-nodes to have good accuracy. The complexity is determined by the size of the tree and the error rate. Here doing reproductivity and generating a number of rows.

set. Seed (1234)
dt<-sample (2, nrow(data), replace = TRUE, prob=c (0.8,0.2))
validate<-data[dt==2,]

partition data

Fig: Showing data values

Next, making data value to 2

validate<-data[dt==2,]

Partition data 2

Partition data 3

Fig: Displaying R console in R Studio

Creating a Decision Tree in R with the package party

  • Click package-> install -> party. Here we have taken the first three inputs from the sample of 1727 observations on datasets. Creating a model to predict high, low, medium among the inputs.

Implementation:

library(party)
tree<-ctree(v~vhigh+vhigh.1+X2,data = train)
tree

Output:

Implementation

Plots Using Ctree

plots using ctree

Prediction:

Prob generates probability on scoring,

Implementation:

predict(tree,validate,type="prob")
predict(tree,validate)

Decision Tree in R - Implementation in plots

[1] vhigh vhigh vhigh vhigh vhigh vhigh vhigh vhigh vhigh vhigh vhigh

[12] vhigh vhigh vhigh vhigh vhigh vhigh vhigh vhigh vhigh vhigh vhigh

[23] vhigh vhigh vhigh vhigh vhigh vhigh vhigh vhigh vhigh vhigh vhigh

[34] vhigh vhigh vhigh vhigh vhigh vhigh vhigh vhigh vhigh vhigh vhigh

[45] vhigh vhigh vhigh vhigh vhigh vhigh vhigh vhigh vhigh vhigh vhigh

[56] vhigh vhigh vhigh vhigh vhigh vhigh vhigh vhigh vhigh vhigh vhigh

[67] vhigh vhigh vhigh vhigh vhigh vhigh vhigh vhigh vhigh vhigh vhigh

[78] vhigh vhigh vhigh high high high high high high high high

[89] high high high high high high high high high high high

[100] high  high  high  high  high  high  high  high  high  high  high

[111] high  high  high  high  high  high  high  high  high  high  high

[122] high  high  high  high  high  high  high  high  high  high  high

[133] high  high  high  high  high  high  high  high  high  high  high

[144] high  high  high  high  high  high  high  high  high  high  high

[155] high  high  high  high  high  high  high  high  high  high  high

[166] high  high  high  high  high  high  high  high  high  high  high

[177] high  high  high  high  med   med   med   med   med   med   med

[188] med   med   med   med   med   med   med   med   med   med   med

[199] med   med   med   med   med   med   med   med   med   med   med

[210] med   med   med   med   med   med   med   med   med   med   med

[221] med med med med med med med med med med med

[232] med med med med med med med med med med med

[243] med med med med med med med med med med med

[254] med   med   med   med   med   med   med   med   med   low   low

[265] low   low   low   low   low   low   low   low   low   low   low

[276] low   low   low   low   low   low   low   low   low   low   low

[287] low   low   low   low   low   low   low   low   low   low   low

[298] low   low   low   low   low   low   low   low   low   low   low

[309] low   low   low   low   low   low   low   low   low   low   low

[320] low   low   low   low   low   low   low   low   low   low   low

[331] low   low   low   low   low

Levels: high low med vhigh

Decision tree using rpart

To predict the class using rpart () function for the class method. rpart () uses the Gini index measure to split the nodes.

library(rpart)
tr<-rpart (v~vhigh+vhigh.1+X2, train)
library (rpart. plot)
rpart. plot(tr)

Decesion tree using Rpart“

rpart.plot(tr,extra=2)

This line plots the tree and to display the probability making extra features to set 2 and the result produced is given below.

Decision Tree in R - Line plots tree 

Misclassification error

The error rate prevents overfitting.

tbl<-table(predict(tree), train $v)
print(tbl)
tepre<-predict(tree,new=validate)

Output:

print(tbl)

high low med vhigh

high 332 0 0 0

low 0 359 0 0

med 0 0 350 0

vhigh 0 0 0 351

Conclusion

The decision tree is a key challenge in R and the strength of the tree is they are easy to understand and read when compared with other models. They are being popularly used in data science problems. These are the tool produces the hierarchy of decisions implemented in statistical analysis. Statistical knowledge is required to understand the logical interpretations of the Decision tree. As we have seen the decision tree is easy to understand and the results are efficient when it has fewer class labels and the other downside part of them is when there are more class labels calculations become complexed. This post makes one become proficient to build predictive and tree-based learning models.

Recommended Articles

This is a guide to Decision Tree in R. Here we discuss the introduction, how to use and implement using R language. You can also go through our other suggested articles to learn more –

  1. What is a Binary Tree in Java?
  2. R Programming Language
  3. What is Visual Studio Code?
  4. Introduction To Line Graph in R
Popular Course in this category
R Programming Training (13 Courses, 20+ Projects)
  13 Online Courses |  20 Hands-on Projects |  120+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Statistical Analysis Training (15 Courses, 10+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more