Sunday, October 7, 2018

Basics of R- Session 18- Decision Tree-1


library(rpart)
library(tree)
library(partykit)
library(rpart.plot)


library(CHAID)
car1<-read.csv("C:/Users/Administrator.vaio1/Desktop/data car.csv")
fix(car1)
dim(car1)

#CHAID-, Chi Square automated identification detector
# all are categorical variable
#Minimumsplit is the minimum number of units before split 10% or atleast 20
#Minbucket is the minimum number of units in a leaf
# maxdepth length of the tree, 1 - single tree

chaid1<-chaid(Car_accaptability~Buying+maintenance+safety, control=chaid_control(minprob = 0.0, minsplit = 500, minbucket = 1), data=car1)
plot(chaid1)
plot(chaid1, margin =0.1)  # page margin
plot(chaid1, margin =1)
plot(chaid1, margin =5)
print(chaid1)

#--------------------------------------------------------#

#CHART

chart1<-rpart(Car_accaptability~Buying+maintenance+safety,data = car1,method = "class", minsplit=500, minbucket=1)
#cp complexity parameter
chart1<-rpart(Car_accaptability~Buying+maintenance+safety,data = car1,method = "class", minsplit=500, minbucket=1, parms = list(split="gini"),cp=0.01)


chart1$parms
chart1$functions
chart1$frame
chart1$splits
chart1$csplit

plot(chart1)
text(chart1)

plot(chart1, uniform = TRUE)
text(chart1, all = TRUE, use.n = TRUE)

summary(chart1)

library(rpart.plot)
rpart.plot(chart1)

library(rattle)
fancyRpartPlot(chart1)

#-----------------------------------------------#

dtm<-rpart(Species~Sepal.Length+Sepal.Width+Petal.Length+Petal.Width, data = iris_train, method = "class")
dtm

print(dtm)
summary(dtm)
plot(dtm) #not a good
text(dtm) #not a good
rpart.plot(dtm)

#prediction can also be done
predict1<-predict(dtm,iris_test, type = "class")
table(iris_test$Species,predict1)

library(caret)
library(e1071)
confusionMatrix(predict1,iris_test$Species)
#----------------------------------------------------------------#