-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecisiontree.R
More file actions
172 lines (139 loc) · 5.18 KB
/
decisiontree.R
File metadata and controls
172 lines (139 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# Data
#Students : Gender - (Male & Female) play sports
rollno = paste('S',1:30, sep='')
rollno
set.seed(1)
gender = sample(x=c('Male','Female'), size=30, replace=T, prob=c(0.5,0.5) )
gender
table(gender)
set.seed(1)
play = sample(x=c('Play','NotPlay'), size=30, replace=T, prob=c(15/30,15/30) )
play
table(play)
prop.table(table(play))
students1 = data.frame(gender, play)
rownames(students1) = rollno
students1
table(students1)
prop.table(table(students1))
#Model1
library(rpart)
?rpart
fit1 = rpart(play ~ gender, data=students1)
fit1 #print(fit1)
library(rpart.plot)
rpart.plot(fit1, main='Classification Tree',nn=T)
predict(fit1, newdata = data.frame(gender='Female'))
predict(fit1, newdata = data.frame(gender=c('Male','Female','Male')))
#---- Part -2 Add another column
set.seed(100)
gender = sample(x=c('Male','Female'), size=30, replace=T, prob=c(0.4,0.6) )
table(gender)
set.seed(101)
married = sample(x=c('Married','Single'), size=30, replace=T, prob=c(0.3,0.7) )
table(married)
set.seed(102)
play = sample(x=c('Play','NotPlay'), size=30, replace=T, prob=c(15/30,15/30) )
table(play)
students2 = data.frame(gender, married, play)
rownames(students2) = rollno
head(students2)
str(students2)
prop.table(ftable(students2))
# Model2
library(rpart)
fit2 = rpart(play ~ gender + married, data=students2)
summary(fit2)
rpart.plot(fit2,type=2,extra=104, tweak=1.2, under=T, shadow=c('brown', 'green','red'), nn=T)
fit2
prp(fit2)
prp(fit2, main="An Example",
type=4, fallen=T, branch=.3, round=0, leaf.round=9,
clip.right.labs=F, under.cex=1,
box.palette="GnYlRd",
prefix="Student\n", branch.col="gray", branch.lwd=2,
extra=101, under=T, lt=" < ", ge=" >= ", cex.main=1.5)
prp(fit2, branch.type=5)
labels(fit2)
new.tree <- prp(fit2, snip=TRUE)$obj # interactively trim the tree
prp(new.tree) # display the new tree
#Plot----
library(RColorBrewer)
library(rattle)
rpart.plot::rpart.plot(fit2, main='Classification Tree')
rpart.plot::rpart.plot(fit2, extra=104, box.palette="GnBu", branch.lty=3, shadow.col="gray", nn=TRUE)
rpart.plot::prp(fit2,fallen.leaves = F)
prp(fit2, type=2)
?prp
windows()
rattle::fancyRpartPlot(model = fit2, main = "Final CART Regression Tree", cex = 0.6, sub = "Model2")
#Predict
#
predict(fit2, newdata = data.frame(gender='Male', married='Married'), type='prob')
predict(fit2, newdata = data.frame(gender='Male', married='Married'), type='class')
predict(fit2, newdata = data.frame(gender='Male', married='Married'), type='vector')
predict(fit2, newdata = data.frame(gender='Male', married='Married'))
?predict
testdata = data.frame(gender=c('Male','Male','Female','Female'), married=c('Married','Single','Married','Single'))
testdata
(p1 = predict(fit2, newdata = testdata, type='vector')) #node/level
(p2 = predict(fit2, newdata = testdata, type='class')) #factor
(p3 = predict(fit2, newdata = testdata, type='prob')) # prob
cbind(testdata, p1, p2, p3)
#level number, class frequencies, probabilities
predict(fit2, newdata= testdata, type = "matrix")
head(students2)
#Parameters Setting : CP
printcp(fit2)
printcp(fit2, digits = getOption("digits") - 5)
plotcp(fit2)
fit2$where
students2[1:2,]
rownames(fit2$frame) [ fit2$where]
#--------------------------------------------------------
#add column with 3 classes and numeric and logical
set.seed(105)
education = sample(x=c('school','graduate', 'pg'), size=30, replace=T, prob=c(0.3,0.4,.3) )
education; table(education)
set.seed(106)
hostel = sample(x=c(TRUE, FALSE), size=30, replace=T, prob=c(.3,.7))
table(hostel)
set.seed(107)
marks = floor(runif(30,50,100))
mean(marks)
students3 = data.frame(gender, married, education, hostel,marks,play)
with(students3, ftable(education, hostel, gender, married,play))
# Model3
fit3a = rpart(play ~ ., data=students3)
fit3a
rpart.plot::rpart.plot(fit3a, main='Classification Tree')
#Model3b : change some parameters minbucket, minsplit
fit3b = rpart(play ~ ., data=students3, minsplit=4, minbucket=2) #control= rpart.control(cp=0.00001))#use low cp
fit3b
rpart.plot::rpart.plot(fit3b, main='Classification Tree', cex=.6, type=3, extra=104, nn=T)
rpart.plot::prp(fit3b)
rattle::fancyRpartPlot(model = fit3b, main = "Final CART Regression Tree", cex = 0.6, sub = "Model3")
prp(fit3b,box.col=c("Grey", "Orange")[fit3$frame$yval],varlen=0,
faclen=0, type=1,extra=4,under=TRUE, tweak=1.2)
#Lets see CP
plotcp(fit3b)
printcp(fit3b, digits = getOption("digits") - 5)
(bestcp= fit3b$cptable[which.min(fit3b$cptable[,'xerror']),'CP'])
#but this is at root node only, select next better
bestcp = 0.01
prp(fit3b)
fit3b2 = rpart(play ~ ., data=students3, minsplit=4, minbucket=2, control= rpart.control(cp=0.001))
fit3b2
prp(fit3b2)
fit3b.pruned = prune(fit3b, cp=bestcp)
fit3b.pruned
prp(fit3b.pruned)
rpart.plot(fit3b.pruned,cex=.6, extra=101, type=1)
#Predict Model3
fit3b.pruned$where
fit3b.pruned
path.rpart(fit3b.pruned, nodes=c(1,4,10,43), pretty = 0, print.it = TRUE)
testdata1= data.frame(gender='Male', married='Married', education='school', hostel=TRUE, marks=60)
testdata1
predict(fit3b.pruned, newdata = testdata1 )
# now practise with Marketing Data