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))
par(bg = "papayawhip")
matplot(p, cbind(gini.index, class.error, cross.entropy), pch=c(15,17,19) ,ylab = "gini.index, class.error, cross.entropy",col = c("darkolivegreen4" , "wheat", "tomato2"), type = 'b')
legend('bottom', inset=.01, legend = c('gini.index', 'class.error', 'cross.entropy'), col = c("darkolivegreen4" , "wheat", "tomato2"), pch=c(15,17,19))
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(9)
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" "ListPriceDiff"
## Number of terminal nodes: 8
## Residual mean deviance: 0.7319 = 579.7 / 792
## Misclassification error rate: 0.1512 = 121 / 800
Comment: The tree has a misclassification error rate of 15.12%, which isn’t bad at all, and it has 8 terminal nodes.
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 1053.000 CH ( 0.63125 0.36875 )
## 2) LoyalCH < 0.48285 288 321.700 MM ( 0.24653 0.75347 )
## 4) LoyalCH < 0.280875 159 120.300 MM ( 0.12579 0.87421 )
## 8) LoyalCH < 0.035047 53 9.922 MM ( 0.01887 0.98113 ) *
## 9) LoyalCH > 0.035047 106 99.690 MM ( 0.17925 0.82075 ) *
## 5) LoyalCH > 0.280875 129 173.100 MM ( 0.39535 0.60465 )
## 10) PriceDiff < 0.25 84 98.620 MM ( 0.27381 0.72619 ) *
## 11) PriceDiff > 0.25 45 59.670 CH ( 0.62222 0.37778 ) *
## 3) LoyalCH > 0.48285 512 437.000 CH ( 0.84766 0.15234 )
## 6) LoyalCH < 0.764572 252 297.800 CH ( 0.72222 0.27778 )
## 12) PriceDiff < -0.165 39 46.400 MM ( 0.28205 0.71795 ) *
## 13) PriceDiff > -0.165 213 211.500 CH ( 0.80282 0.19718 )
## 26) ListPriceDiff < 0.135 36 49.800 CH ( 0.52778 0.47222 ) *
## 27) ListPriceDiff > 0.135 177 144.200 CH ( 0.85876 0.14124 ) *
## 7) LoyalCH > 0.764572 260 71.450 CH ( 0.96923 0.03077 ) *
Comment: The 8th node is picked. It has a split criterino of < 0.035, 57 observations in the branch with a deviance of 9.922. only 1.7% of the observations in that branch take the value of CH, and the remaining 98% take the value of MM.
d: Create a plot of the tree, and interpret the results
plot(tree.oj)
text(tree.oj, pretty = 0)
Comment: From the plot above, we can see that the most important predictor for Purchase is LoyalCH because it’s set as the first branch which determines the intensity of customer brand loyalty.
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 132 37
## MM 16 85
tree.test.error = round(mean(tree.pred != OJ.test$Purchase)*100,2)
tree.test.error
## [1] 19.63
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 6 4 2 1
##
## $dev
## [1] 143 143 149 154 295
##
## $k
## [1] -Inf 0.0 5.5 8.5 146.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?
Answer: We can see that tree size 8 corresponds to the lowest cross-validated classification error rate.
j: Compare the training error rates between the pruned and unpruned trees. Which is higher?
prune.oj = prune.misclass(tree.oj, best=2)
summary(prune.oj)
##
## Classification tree:
## snip.tree(tree = tree.oj, nodes = 2:3)
## Variables actually used in tree construction:
## [1] "LoyalCH"
## Number of terminal nodes: 2
## Residual mean deviance: 0.9507 = 758.7 / 798
## Misclassification error rate: 0.1862 = 149 / 800
summary(tree.oj)
##
## Classification tree:
## tree(formula = Purchase ~ ., data = OJ.train)
## Variables actually used in tree construction:
## [1] "LoyalCH" "PriceDiff" "ListPriceDiff"
## Number of terminal nodes: 8
## Residual mean deviance: 0.7319 = 579.7 / 792
## Misclassification error rate: 0.1512 = 121 / 800
Comment: The misclassification error rate for tree.oj (unpruned) is slightly lower (15%) than prune.oj (18%)
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 125 32
## MM 23 90
pruned.test.error = round(mean(prune.pred != OJ.test$Purchase)*100,2)
pruned.test.error
## [1] 20.37
Comment: The test error rates is slightly higher for pruned tree than unpruned (20.37%)