



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Introduction to Statistical Learning (James/Witten/Hastie/Tibshirani)
Typology: Exercises
1 / 5
This page cannot be seen from the preview
Don't miss anything!




(a). Create a training set containing a random sample of 800 observations, and a test set containing the remaining observations.
library (ISLR) attach (OJ) set.seed (1)
train = sample ( dim (OJ)[1], 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 predic- tors. 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?
library (tree) oj.tree = tree (Purchase ~ ., data = OJ.train) summary (oj.tree)
As shown in the output, the tree 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.
oj.tree
The * symbols denote terminal nodes. If we select the one labeled “7)", we can see that there are 278 observations with a value for LoyalCH > 0.764572, and they are classified as “CH"
(d). Create a plot of the tree, and interpret the results.
plot (oj.tree) text (oj.tree, pretty = 0)
cv.oj = cv.tree (oj.tree, FUN = prune.tree)
(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?
(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.tree (oj.tree, best = cv.oj$size[ which.min (cv.oj$dev)])
(j). Compare the training error rates between the pruned and unpruned trees. Which is higher?
summary (oj.pruned)
The pruned tree is higher
(k). Compare the test error rates between the pruned and unpruned trees. Which is higher?
pred.unpruned = predict (oj.tree, OJ.test, type = "class") misclass.unpruned = sum (OJ.test$Purchase != pred.unpruned) misclass.unpruned/ length (pred.unpruned)
pred.pruned = predict (oj.pruned, OJ.test, type = "class") misclass.pruned = sum (OJ.test$Purchase != pred.pruned) misclass.pruned/ length (pred.pruned)
The pruned tree has a higher test error