-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecision_tree.R
More file actions
56 lines (39 loc) · 1.26 KB
/
Copy pathdecision_tree.R
File metadata and controls
56 lines (39 loc) · 1.26 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
#####################################
# Decision Trees #
#####################################
require(ISLR)
require(tree)
attach(Carseats)
hist(Sales)
# create a binary variable
High = ifelse(Sales<=8,"No","Yes")
# adding the new variable to the data set
Carseats = data.frame(Carseats, High)
# deep level tree
tree.carseats = tree(High~.-Sales, data=Carseats)
summary(tree.carseats)
plot(tree.carseats)
text(tree.carseats, pretty=0)
tree.carseats
# create a train and test set
set.seed(1011)
# sample 250 indexes without replacement
train=sample(1:nrow(Carseats), 250)
tree.carseats=tree(High~.-Sales, data=Carseats, subset=train)
plot(tree.carseats)
text(tree.carseats, pretty=0)
# make predictions
tree.pred = predict(tree.carseats, Carseats[-train,], type="class")
# classification table
with(Carseats[-train,], table(tree.pred,High))
# use CV to prune the tree optimally
cv.carseats = cv.tree(tree.carseats, FUN=prune.misclass)
cv.carseats
plot(cv.carseats)
# pickup the best pruned tree, i.e. size=13
prune.carseats=prune.misclass(tree.carseats, best=13)
plot(prune.carseats)
text(prune.carseats, pretty=0)
# check mis-classification
tree.pred = predict(prune.carseats, Carseats[-train,], type="class")
with(Carseats[-train,], table(tree.pred,High))