Assignment 7

Chapter 08: 3, 8, 9

3) Consider the Gini index, classification error, and entropy in a simple classification setting with two classes. Create a single plot that displays each of these quantities as a function of ˆpm1. The x-axis should FIGURE 8.14. Left: A partition of the predictor space corresponding to Exercise 4a. Right: A tree corresponding to Exercise 4b. 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_mi <- seq(0, 1, 0.01)
gini <- p_mi * (1 - p_mi) * 2
entropy <- -(p_mi * log(p_mi) + (1 - p_mi) * log(1 - p_mi))
class.err <- 1 - pmax(p_mi, 1 - p_mi)
df = data.frame(p_mi = p_mi, gini = gini, entropy = entropy, class.err = class.err)

Make the data in long format

library(tidyr)

df_long=gather(df, key = "variable", value = "value", -p_mi)
#ket --> the name of the new col will have the original col names
# value --> the name of the new col will contain the corresponding values
# -p_mi specifies that the column should not be stacked
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.2.3
ggplot(df_long, aes(x = p_mi, y = value, color = variable)) +
  geom_line() +
  scale_color_manual(values = c("red", "black", "orange")) +
  labs(x = "p", y = "Values", title = "Graph Title")
## Warning: Removed 2 rows containing missing values (`geom_line()`).

9) This prob involves the OJ data set which is part of the ISLR2 package

a) create a training set containing a random sample of 800 obs, and a test set with the remaining obs

library(ISLR)
set.seed(1)
library(dplyr)

Default = as.data.frame(OJ)
Default$id <- 1:nrow(Default)

#use 74.76635514% of dataset as training set and the rest as the test set 
train <- Default %>% dplyr::sample_frac(0.7476635514)
test  <- dplyr::anti_join(Default, train, by = 'id')

dim(train)
## [1] 800  19
dim(test)
## [1] 270  19

b) fit a tree to the training data, with pruchases as the response and the other variables as predictors. Use the summary() function to produce summary stats about the tree, and describe the results. What is the error rate? How many terminal nodes does the tree have?

library(tree)


oj_tree = tree(Purchase ~., data = train)
summary(oj_tree)
## 
## Classification tree:
## tree(formula = Purchase ~ ., data = train)
## Variables actually used in tree construction:
## [1] "LoyalCH"       "PriceDiff"     "SpecialCH"     "ListPriceDiff"
## [5] "PctDiscMM"     "id"           
## Number of terminal nodes:  10 
## Residual mean deviance:  0.7286 = 575.6 / 790 
## Misclassification error rate: 0.1588 = 127 / 800

The tree uses 6 variables: [1] “LoyalCH” “PriceDiff” “SpecialCH” “ListPriceDiff” “PctDiscMM” “id”
The tree has 10 terminal nodes. The error rate (missclassification error) is 127/800 = .1588

c) Type in the name of the tree object in order to get details. Pick on terminal nodes, and interpret the info displayed

oj_tree
## node), split, n, deviance, yval, (yprob)
##       * denotes terminal node
## 
##  1) root 800 1073.00 CH ( 0.60625 0.39375 )  
##    2) LoyalCH < 0.5036 365  441.60 MM ( 0.29315 0.70685 )  
##      4) LoyalCH < 0.280875 177  140.50 MM ( 0.13559 0.86441 )  
##        8) LoyalCH < 0.0356415 59   10.14 MM ( 0.01695 0.98305 ) *
##        9) LoyalCH > 0.0356415 118  116.40 MM ( 0.19492 0.80508 ) *
##      5) LoyalCH > 0.280875 188  258.00 MM ( 0.44149 0.55851 )  
##       10) PriceDiff < 0.05 79   84.79 MM ( 0.22785 0.77215 )  
##         20) SpecialCH < 0.5 64   51.98 MM ( 0.14062 0.85938 ) *
##         21) SpecialCH > 0.5 15   20.19 CH ( 0.60000 0.40000 ) *
##       11) PriceDiff > 0.05 109  147.00 CH ( 0.59633 0.40367 ) *
##    3) LoyalCH > 0.5036 435  337.90 CH ( 0.86897 0.13103 )  
##      6) LoyalCH < 0.764572 174  201.00 CH ( 0.73563 0.26437 )  
##       12) ListPriceDiff < 0.235 72   99.81 MM ( 0.50000 0.50000 )  
##         24) PctDiscMM < 0.196196 55   73.14 CH ( 0.61818 0.38182 )  
##           48) id < 267 11    0.00 CH ( 1.00000 0.00000 ) *
##           49) id > 267 44   60.91 CH ( 0.52273 0.47727 ) *
##         25) PctDiscMM > 0.196196 17   12.32 MM ( 0.11765 0.88235 ) *
##       13) ListPriceDiff > 0.235 102   65.43 CH ( 0.90196 0.09804 ) *
##      7) LoyalCH > 0.764572 261   91.20 CH ( 0.95785 0.04215 ) *

Tbh, this looks like a nest for loop. Picking node 9, the splitting variable iis at “LoyalCH”. The splitting value is 0.0356415. There are 118 points in the subtree below this node. The deviance for all points contained in the region below this node is 116.40. The * denotes that it is a terminal node. The prediction at this node is Sales. About 19% of the values are representing nodes, and the remaining 81% of values have MM values as Sales

d) create a plot of the tree, and interpret the results

plot(oj_tree)
text(oj_tree, pretty = 0)

LoyalCH is the most important variable in the tree model. The next 2 conditions are also revolving around Loyal CH. Starting from the left side:

If loyalCH is < .28 we go to the left most node. Once again, if LoyalCH is less than .04, then it is MM, and MM once again is it is > .04 If loyalCH is > .28 we go to the right node. If PriceDiff is < .05, and Special CH < .05 we predict MM, ELSE, we are CH. The same process is repeated in the right side. Basically, it is a nest for loop.

e) Predict the response on the data, and produce a confusion matrix comparing the test labels to predict test labels. What is the test error rate?

oj_pred = predict(oj_tree, test, type = "class")
table(test$Purchase, oj_pred)
##     oj_pred
##       CH  MM
##   CH 160   8
##   MM  38  64
total_preds <- sum(table(test$Purchase, oj_pred))

incorrect_preds <- total_preds - sum(diag(table(test$Purchase, oj_pred)))

test_error_rate <- 100 * incorrect_preds / total_preds

test_error_rate
## [1] 17.03704

The test error rate is 17.03704

f) Apply the cv.tree()…

cv_oj = cv.tree(oj_tree, FUN = prune.tree)
cv_oj
## $size
##  [1] 10  9  8  7  6  5  4  3  2  1
## 
## $dev
##  [1]  704.9056  723.2379  723.4760  715.7605  715.7605  714.1093  725.4734
##  [8]  780.2099  790.0301 1074.2062
## 
## $k
##  [1]      -Inf  12.23818  12.62207  13.94616  14.35384  26.21539  35.74964
##  [8]  43.07317  45.67120 293.15784
## 
## $method
## [1] "deviance"
## 
## attr(,"class")
## [1] "prune"         "tree.sequence"

g) produce a plot with tree size on the x-axis…

plot(cv_oj$size, cv_oj$dev, type = "b", xlab = "tree size", ylab = "deviance")

#### h) which tree size corresponds to the lowest cv class error rate

The value with the lowest cross validation appears to be be at 5 (yes 10 is the lowest, but it be best to choose 5 to reduce Over Fitting)

i) Produce a pruned tree…

oj_pruned = prune.tree(oj_tree, best = 5)
plot(oj_pruned)
text(oj_pruned, pretty = 0)

#### j) compare the training error rates between the pruned and unpruned trees.

summary(oj_pruned)
## 
## Classification tree:
## snip.tree(tree = oj_tree, nodes = c(4L, 12L, 5L))
## Variables actually used in tree construction:
## [1] "LoyalCH"       "ListPriceDiff"
## Number of terminal nodes:  5 
## Residual mean deviance:  0.8239 = 655 / 795 
## Misclassification error rate: 0.205 = 164 / 800

Our pruned Misclassification error rate: 0.205 = 164 / 800

Unpruned: Misclassification error rate: 0.1588 = 127 / 800

Our missclassification error rate increased, but not by much. If we round, then the error rate is the same

k) compare the test error rates betweeen pruned and unpruned

pred_unpruned = predict(oj_tree, test, type = "class")
misclass_unpr = sum(test$Purchase != pred_unpruned)
misclass_unpr/length(pred_unpruned)
## [1] 0.1703704
pred_pruned = predict(oj_pruned, test, type = "class")
misclass_unpr = sum(test$Purchase != pred_pruned)
misclass_unpr/length(pred_pruned)
## [1] 0.1925926

The pruned misclassification rate (0.2037037), is higher than the unpruned misclassification rate (0.1703704). However, these values are really close, with a small difference. Depending on computational intensities, it may be more efficient, and interpretable to go with the pruned tree (however, this depends on a case by case scenario)