#Install rpart.
install.packages("rpart", dependencies = TRUE, repos = "http://cran.us.r-project.org")
library(rpart)
#Data obtained from the party package https://www.tutorialspoint.com/r/r_decision_tree.htm.
#Import data via clipboard.
Reading_Skills <- read.table(file="clipboard", header = TRUE)
#Check imported data.
head(Reading_Skills)
## nativeSpeaker age shoeSize score
## 1 yes 5 24.83189 32.29385
## 2 yes 6 25.95238 36.63105
## 3 no 11 30.42170 49.60593
## 4 yes 7 28.66450 40.28456
## 5 yes 11 31.88207 55.46085
## 6 yes 10 30.07843 52.83124
#Build decision tree.
TREE_READING_SKILLS <- rpart(nativeSpeaker ~ age + shoeSize + score,
data = Reading_Skills)
TREE_READING_SKILLS
## n= 105
##
## node), split, n, loss, yval, (yprob)
## * denotes terminal node
##
## 1) root 105 52 no (0.5047619 0.4952381)
## 2) score< 38.61006 43 9 no (0.7906977 0.2093023) *
## 3) score>=38.61006 62 19 yes (0.3064516 0.6935484)
## 6) age>=8.5 42 19 yes (0.4523810 0.5476190)
## 12) score< 50.5448 23 4 no (0.8260870 0.1739130) *
## 13) score>=50.5448 19 0 yes (0.0000000 1.0000000) *
## 7) age< 8.5 20 0 yes (0.0000000 1.0000000) *
#Plot decision tree
library(rpart.plot)
rpart.plot(TREE_READING_SKILLS, extra = 102)