library(ISLR2)
library(tree)
library(rpart)
library(caret)
## Loading required package: ggplot2
## Loading required package: lattice
library(randomForest)
## randomForest 4.7-1.1
## Type rfNews() to see new features/changes/bug fixes.
##
## Attaching package: 'randomForest'
## The following object is masked from 'package:ggplot2':
##
## margin
p = seq(0, 1, 0.001)
gini.index = 2 * p * (1 - p)
class.error = 1 - pmax(p, 1 - p)
cross.entropy = - (p * log(p) + (1 - p) * log(1 - p))
matplot(p, cbind(gini.index, class.error, cross.entropy), ylab = "gini.index, class.error, cross.entropy", col = c("red", "green", "orange"))
(a) Create a training set containing a random sample of 800 observations, and a test set containing the remaining observations.
attach(OJ)
train.OJ= sample(1:nrow(OJ),800)
OJ.train= OJ[train,]
OJ.test = OJ[-train,]
(b) Fit a tree to the training data, with Purchase as the response and the other variables as predictors. Use the summary() function to produce summary statistics about the tree, and describe the results obtained. What is the training error rate? How many terminal nodes does the tree have? 7 variables were used int the tree construction. The training error rate is 0.095. There are 15 terminal nodes on the tree.
OJ.tree = tree(Purchase~., data = OJ.train)
summary(OJ.tree)
##
## Classification tree:
## tree(formula = Purchase ~ ., data = OJ.train)
## Variables actually used in tree construction:
## [1] "LoyalCH" "PriceDiff" "StoreID" "DiscMM"
## [5] "PriceCH" "SalePriceMM" "WeekofPurchase"
## Number of terminal nodes: 15
## Residual mean deviance: 0.3951 = 73.09 / 185
## Misclassification error rate: 0.095 = 19 / 200
plot(OJ.tree)
text(OJ.tree, pretty= 0)
(c) Type in the name of the tree object in order to get a
detailed text output. Pick one of the terminal nodes, and interpret the
information displayed. 13) LoyalCH > 0.70936 79 37.280 CH (
0.93671 0.06329 )
OJ.tree
## node), split, n, deviance, yval, (yprob)
## * denotes terminal node
##
## 1) root 200 242.600 CH ( 0.70500 0.29500 )
## 2) LoyalCH < 0.592 75 95.480 MM ( 0.33333 0.66667 )
## 4) LoyalCH < 0.060978 18 0.000 MM ( 0.00000 1.00000 ) *
## 5) LoyalCH > 0.060978 57 78.160 MM ( 0.43860 0.56140 )
## 10) PriceDiff < 0.065 25 27.550 MM ( 0.24000 0.76000 )
## 20) StoreID < 2.5 11 0.000 MM ( 0.00000 1.00000 ) *
## 21) StoreID > 2.5 14 19.120 MM ( 0.42857 0.57143 )
## 42) DiscMM < 0.3 6 5.407 MM ( 0.16667 0.83333 ) *
## 43) DiscMM > 0.3 8 10.590 CH ( 0.62500 0.37500 ) *
## 11) PriceDiff > 0.065 32 43.230 CH ( 0.59375 0.40625 )
## 22) StoreID < 1.5 5 0.000 CH ( 1.00000 0.00000 ) *
## 23) StoreID > 1.5 27 37.390 CH ( 0.51852 0.48148 )
## 46) PriceCH < 1.805 5 0.000 MM ( 0.00000 1.00000 ) *
## 47) PriceCH > 1.805 22 28.840 CH ( 0.63636 0.36364 )
## 94) SalePriceMM < 2.125 7 0.000 CH ( 1.00000 0.00000 ) *
## 95) SalePriceMM > 2.125 15 20.730 MM ( 0.46667 0.53333 ) *
## 3) LoyalCH > 0.592 125 64.700 CH ( 0.92800 0.07200 )
## 6) PriceDiff < 0.31 94 59.340 CH ( 0.90426 0.09574 )
## 12) LoyalCH < 0.70936 15 17.400 CH ( 0.73333 0.26667 ) *
## 13) LoyalCH > 0.70936 79 37.280 CH ( 0.93671 0.06329 )
## 26) WeekofPurchase < 248.5 24 21.630 CH ( 0.83333 0.16667 )
## 52) WeekofPurchase < 236.5 9 0.000 CH ( 1.00000 0.00000 ) *
## 53) WeekofPurchase > 236.5 15 17.400 CH ( 0.73333 0.26667 )
## 106) LoyalCH < 0.902367 10 6.502 CH ( 0.90000 0.10000 ) *
## 107) LoyalCH > 0.902367 5 6.730 MM ( 0.40000 0.60000 ) *
## 27) WeekofPurchase > 248.5 55 9.996 CH ( 0.98182 0.01818 )
## 54) DiscMM < 0.47 48 0.000 CH ( 1.00000 0.00000 ) *
## 55) DiscMM > 0.47 7 5.742 CH ( 0.85714 0.14286 ) *
## 7) PriceDiff > 0.31 31 0.000 CH ( 1.00000 0.00000 ) *
(d) Create a plot of the tree, and interpret the results.
plot(OJ.tree)
text(OJ.tree, pretty = 0)
(e) Predict the response on the test data, and produce a
confusion matrix comparing the test labels to the predicted test labels.
What is the test error rate? Test error rate is 27.8%
OJ.tree.pred = predict(OJ.tree, newdata = OJ.test, type = "class")
table(OJ.tree.pred,OJ.test$Purchase)
##
## OJ.tree.pred CH MM
## CH 404 134
## MM 108 224
(108+134)/870
## [1] 0.2781609
(f) Apply the cv.tree() function to the training set in order to determine the optimal tree size.
OJ.cv= cv.tree(OJ.tree,FUN = prune.misclass)
OJ.cv
## $size
## [1] 15 14 9 6 4 2 1
##
## $dev
## [1] 43 41 37 41 44 43 62
##
## $k
## [1] -Inf 0.0 0.2 1.0 2.5 3.0 25.0
##
## $method
## [1] "misclass"
##
## attr(,"class")
## [1] "prune" "tree.sequence"
(g) Produce a plot with tree size on the x-axis and cross-validated classification error rate on the y-axis.
plot(OJ.cv$size, OJ.cv$dev, type = "b", xlab = "Tree Size", ylab = "cv classification error rate")
(h) Which tree size corresponds to the lowest cross-validated
classi- fication error rate? Node six has the lowest
cross-validated classifcation error rate. (i) Produce a pruned
tree corresponding to the optimal tree size obtained using
cross-validation. If cross-validation does not lead to selection of a
pruned tree, then create a pruned tree with five terminal
nodes.
prune.OJ= prune.tree(OJ.tree,best = 7)
(j) Compare the training error rates between the pruned and unpruned trees. Which is higher?
summary(OJ.tree)
##
## Classification tree:
## tree(formula = Purchase ~ ., data = OJ.train)
## Variables actually used in tree construction:
## [1] "LoyalCH" "PriceDiff" "StoreID" "DiscMM"
## [5] "PriceCH" "SalePriceMM" "WeekofPurchase"
## Number of terminal nodes: 15
## Residual mean deviance: 0.3951 = 73.09 / 185
## Misclassification error rate: 0.095 = 19 / 200
summary(prune.OJ)
##
## Classification tree:
## snip.tree(tree = OJ.tree, nodes = c(21L, 3L))
## Variables actually used in tree construction:
## [1] "LoyalCH" "PriceDiff" "StoreID" "PriceCH" "SalePriceMM"
## Number of terminal nodes: 8
## Residual mean deviance: 0.5445 = 104.5 / 192
## Misclassification error rate: 0.11 = 22 / 200
(k) Compare the test error rates between the pruned and unpruned trees. Which is higher? The pruned tree has a higher test error rate of 0.4137255
OJ.tree.pred = predict(OJ.tree, newdata = OJ.test, type = "class")
table(OJ.tree.pred, OJ.test$Purchase)
##
## OJ.tree.pred CH MM
## CH 404 134
## MM 108 224
(108+134)/870
## [1] 0.2781609
prune.OJ.pred= predict(prune.OJ, newdata = OJ.test, type = "class")
table(prune.OJ.pred, OJ.test$Purchase)
##
## prune.OJ.pred CH MM
## CH 404 103
## MM 108 255
(108+103)/510
## [1] 0.4137255