Chapter 8: 3, 8, 9

Question 3

Consider the Gini indez, classification error, and entropy in a simple classification setting wtih two classes. Create a single plot that displays each of the quantities as a function of p hat m2. The x-axis should displasy p hat m1, ranging from 0 to 1, and the y-axis should display the value of the Gini index, classification error, and entropy.

Hint: In a setting with two classes, p hat m2 = 1 - phatm2. You could make this plot by hand, but it will be much easier to make in R.

p = seq(0, 1, 0.01)
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), col=c("red", "green", "blue"))

Quetion 8

Question 9

This problem involves the OJ data set which is part of the ISLR package.

a) Create a training set containing a random sample of 800 observations, and a test set containing the remaining observations.

set.seed(0)
train = 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?

tree.oj = tree(Purchase~., data = OJ.train)
summary(tree.oj)
## 
## Classification tree:
## tree(formula = Purchase ~ ., data = OJ.train)
## Variables actually used in tree construction:
## [1] "LoyalCH"   "PriceDiff" "StoreID"  
## Number of terminal nodes:  8 
## Residual mean deviance:  0.7679 = 608.2 / 792 
## Misclassification error rate: 0.1588 = 127 / 800
  • The fitted tree has 8 terminal nodes and the training rate is 0.1588.

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.

tree.oj
## node), split, n, deviance, yval, (yprob)
##       * denotes terminal node
## 
##  1) root 800 1067.000 CH ( 0.61375 0.38625 )  
##    2) LoyalCH < 0.48285 297  329.000 MM ( 0.24242 0.75758 )  
##      4) LoyalCH < 0.0356415 60   10.170 MM ( 0.01667 0.98333 ) *
##      5) LoyalCH > 0.0356415 237  289.400 MM ( 0.29958 0.70042 )  
##       10) PriceDiff < 0.49 228  268.800 MM ( 0.27632 0.72368 )  
##         20) PriceDiff < -0.34 16    0.000 MM ( 0.00000 1.00000 ) *
##         21) PriceDiff > -0.34 212  258.000 MM ( 0.29717 0.70283 ) *
##       11) PriceDiff > 0.49 9    6.279 CH ( 0.88889 0.11111 ) *
##    3) LoyalCH > 0.48285 503  453.800 CH ( 0.83300 0.16700 )  
##      6) LoyalCH < 0.764572 233  288.100 CH ( 0.69099 0.30901 )  
##       12) PriceDiff < 0.015 83  113.600 MM ( 0.43373 0.56627 )  
##         24) StoreID < 3.5 44   49.490 MM ( 0.25000 0.75000 ) *
##         25) StoreID > 3.5 39   50.920 CH ( 0.64103 0.35897 ) *
##       13) PriceDiff > 0.015 150  135.200 CH ( 0.83333 0.16667 ) *
##      7) LoyalCH > 0.764572 270   98.180 CH ( 0.95556 0.04444 ) *
  • Terminal nodes are indicated with an asterisk. Looking at node 7, we see the split criterion is Loyal CH > 0.76, the number of observations in that branch is 270 with a deviance of 98.180 and an overall prediction for the branch of CH. Approximately 96% of the observations in this branch take the value of CH while the remaining 4% take the value of MM.

d) Create a plot of the tree, and interpret the results.

plot(tree.oj)
text(tree.oj, pretty = 0)

* It appears that an important predictor for purchase is the variable of Loyal CH with the first three nodes containing Loyal CH.

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?

tree.pred = predict(tree.oj, OJ.test, type="class")
table(tree.pred, OJ.test$Purchase)
##          
## tree.pred  CH  MM
##        CH 134  24
##        MM  28  84
1 - (134+84)/(134+84+28+24)
## [1] 0.1925926
  • The test error rate is about 19%

f) Apply the cv.tree() function to the training set in order to determine the optimal tree size.

cv.oj = cv.tree(tree.oj, FUN = prune.misclass)
cv.oj
## $size
## [1] 8 7 5 2 1
## 
## $dev
## [1] 146 146 152 155 309
## 
## $k
## [1]       -Inf   0.000000   3.500000   7.333333 153.000000
## 
## $method
## [1] "misclass"
## 
## attr(,"class")
## [1] "prune"         "tree.sequence"

g) Produce a plot with tree seize on the x-axis and cross-validation classification error rate on the y-axis.

plot(cv.oj$size, cv.oj$dev, type = "b", xlab = "Tree size", ylab = "Deviance")

h) Which tree size correspond to the lowest cross-validation classification error rate?

  • The seven node tree is the smallest tree that corresponds to the lowest classification 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.misclass(tree.oj, best = 5)
plot(prune.oj)
text(prune.oj, pretty = 0)

j) Compare the training error rates between the pruned and unpruned trees. Which is higher?

summary(tree.oj)
## 
## Classification tree:
## tree(formula = Purchase ~ ., data = OJ.train)
## Variables actually used in tree construction:
## [1] "LoyalCH"   "PriceDiff" "StoreID"  
## Number of terminal nodes:  8 
## Residual mean deviance:  0.7679 = 608.2 / 792 
## Misclassification error rate: 0.1588 = 127 / 800
summary(prune.oj)
## 
## Classification tree:
## snip.tree(tree = tree.oj, nodes = 2L)
## Variables actually used in tree construction:
## [1] "LoyalCH"   "PriceDiff" "StoreID"  
## Number of terminal nodes:  5 
## Residual mean deviance:  0.8336 = 662.7 / 795 
## Misclassification error rate: 0.1675 = 134 / 800
  • The training error rate appears to be higher for the pruned tree in this example (0.1675 vs 0.1588).

k) Compare the test error rates between the pruned and unpruned trees. Which is higher?

prune.pred = predict(prune.oj, OJ.test, type = "class")
table(prune.pred, OJ.test$Purchase)
##           
## prune.pred  CH  MM
##         CH 133  21
##         MM  29  87
1 - (140+82)/(140+26+22+82)
## [1] 0.1777778
  • The test error rate for the pruned tree is comparatively lower at 0.178 while the test error rate for the unpruned tree was 0.193.