Create a single plot that displays each of these quantities as a function of \(ˆpm1\). The x-axis should display \(ˆpm1\), 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ˆm1 = 1 − pˆm2\). You could make this plot by hand, but it will be much easier to make in R.
p=seq(0,1,0.01)
gini=p*(1-p)*2
class.error=1-pmax(p,1-p)
entropy=-(p*log(p)+(1-p)*log(1-p))
matplot(p,cbind(gini,class.error,entropy),col=c("magenta","navy", "cyan"))
(a) Create a training set containing a random sample of 800 observations, and a test set containing the remaining observations.
detach(Carseats)
library(ISLR2)
## Warning: package 'ISLR2' was built under R version 4.2.2
##
## Attaching package: 'ISLR2'
## The following objects are masked from 'package:ISLR':
##
## Auto, Credit
attach(OJ)
set.seed(5)
train=sample(dim(OJ)[1],800)
train.OJ=OJ[train,]
test.OJ=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?
oj.tree=tree(Purchase~.,data=train.OJ)
summary(oj.tree)
##
## Classification tree:
## tree(formula = Purchase ~ ., data = train.OJ)
## Variables actually used in tree construction:
## [1] "LoyalCH" "PriceDiff" "ListPriceDiff"
## Number of terminal nodes: 9
## Residual mean deviance: 0.7347 = 581.1 / 791
## Misclassification error rate: 0.1662 = 133 / 800
The error rate we got with this tree is .1662 and the number of terminal nodes the tree has is 9. The variables used in this tree were LoyalCH, PriceDiff, and ListPriceDiff.
(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.
oj.tree
## node), split, n, deviance, yval, (yprob)
## * denotes terminal node
##
## 1) root 800 1068.00 CH ( 0.61250 0.38750 )
## 2) LoyalCH < 0.5036 346 412.40 MM ( 0.28324 0.71676 )
## 4) LoyalCH < 0.280875 164 125.50 MM ( 0.12805 0.87195 )
## 8) LoyalCH < 0.0356415 56 10.03 MM ( 0.01786 0.98214 ) *
## 9) LoyalCH > 0.0356415 108 103.50 MM ( 0.18519 0.81481 ) *
## 5) LoyalCH > 0.280875 182 248.00 MM ( 0.42308 0.57692 )
## 10) PriceDiff < 0.05 71 67.60 MM ( 0.18310 0.81690 ) *
## 11) PriceDiff > 0.05 111 151.30 CH ( 0.57658 0.42342 ) *
## 3) LoyalCH > 0.5036 454 362.00 CH ( 0.86344 0.13656 )
## 6) PriceDiff < -0.39 31 40.32 MM ( 0.35484 0.64516 )
## 12) LoyalCH < 0.638841 10 0.00 MM ( 0.00000 1.00000 ) *
## 13) LoyalCH > 0.638841 21 29.06 CH ( 0.52381 0.47619 ) *
## 7) PriceDiff > -0.39 423 273.70 CH ( 0.90071 0.09929 )
## 14) LoyalCH < 0.705326 135 143.00 CH ( 0.77778 0.22222 )
## 28) ListPriceDiff < 0.255 67 89.49 CH ( 0.61194 0.38806 ) *
## 29) ListPriceDiff > 0.255 68 30.43 CH ( 0.94118 0.05882 ) *
## 15) LoyalCH > 0.705326 288 99.77 CH ( 0.95833 0.04167 ) *
I picked terminal node 28, and this node has 67 observations in it. Its splitting point is .255. The branch has a deviance of 89.43, and this branch predicts that Citrus Hill is being purchased. However, this is only correct on 61.19% of the observations.
(d) Create a plot of the tree, and interpret the results.
plot(oj.tree)
text(oj.tree,pretty=0)
The splitting variable at the top is LoyalCH, which is the loyalty of CH’s customers to the brand. Therefore, it seems that LoyalCH is the most important variable, with PriceDiff and ListPriceDiff being the next most important variables here. The less loyal customers are to Citrus Hill, the more likely they are to purchase Minute Maid. With the more loyal Citrus Hill Customers, it takes a bigger price difference to get them to jump ship.
(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?
oj.pred=predict(oj.tree,newdata=test.OJ,type="class")
table(test.OJ$Purchase,oj.pred)
## oj.pred
## CH MM
## CH 148 15
## MM 32 75
mean(oj.pred!=test.OJ$Purchase)
## [1] 0.1740741
The test error rate here is 17.41%.
(f) Apply the cv.tree() function to the training set in order to determine the optimal tree size.
cv.oj=cv.tree(oj.tree,FUN=prune.misclass)
cv.oj
## $size
## [1] 9 6 5 3 2 1
##
## $dev
## [1] 155 156 156 172 170 310
##
## $k
## [1] -Inf 0.0 1.0 8.5 9.0 150.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(cv.oj$size,cv.oj$dev,type="b",xlab="Tree Size",ylab="Deviance")
(h) Which tree size corresponds to the lowest cross-validated classification error rate?
The tree size with the lowest CV error rate is a tree that has 9 nodes.
(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.
oj.pruned=prune.misclass(oj.tree,best=9)
plot(oj.pruned)
text(oj.pruned,pretty=0)
(j) Compare the training error rates between the pruned and unpruned trees. Which is higher?
summary(oj.pruned)
##
## Classification tree:
## tree(formula = Purchase ~ ., data = train.OJ)
## Variables actually used in tree construction:
## [1] "LoyalCH" "PriceDiff" "ListPriceDiff"
## Number of terminal nodes: 9
## Residual mean deviance: 0.7347 = 581.1 / 791
## Misclassification error rate: 0.1662 = 133 / 800
The test error rate is the same between the pruned and unpruned trees because the pruned tree is not actually pruned. Using CV, it was determined that the best size was with 9 terminal nodes.
(k) Compare the test error rates between the pruned and unpruned trees. Which is higher?
ojprune.pred=predict(oj.pruned, newdata=test.OJ, type="class")
mean(ojprune.pred != test.OJ$Purchase)
## [1] 0.1740741
Again, we get the same test error rate because CV found that the unpruned tree was best.