---
title: "STAT 2450 Assignment 7, Winter 2020 - due Monday, March 23"
author: "Your name here"
date: 'Banner:  B00??????'
output:
  pdf_document: default
  html_document: default
  word_document: default
---

1.  ISLR, chapter 8, problem 9.

+ YOU HAVE TO TREAT ALL QUESTIONS (a. to k.)

+ USE the ISLR BOOK : http://faculty.marshall.usc.edu/gareth-james/ISL/ISLR%20Seventh%20Printing.pdf


a)

```{r}
set.seed(666)
library(ISLR)
index=sample(1:nrow(OJ),800,replace=F)
OJtrain=OJ[index,]
OJtest=OJ[-index,]

```

b)
   
```{r}
library(tree)
# OJtraintree=tree( enter your model specification here ,data=OJtrain)
```

(4 points for fitting tree, reporting training error rate and number terminal nodes.
 Seed wasn't specified prior to "sample" command, so different individuals are likely
 to have different trees.)

(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.

(3 points for sensible interpretation for the node chosen.)

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

(3 points for plot of tree with text.)

(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?

(4 points: 3 for the confusion matrix, 1 for reporting the correct test error rate)

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

(g) Produce a plot with tree size on the x-axis and cross-validated
classification error rate on the y-axis.

(5 points for the plot)

(h) Which tree size corresponds to the lowest cross-validated classification error rate?

(3 points for reporting the correct 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.

(3 points for the plot, with text)

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

(3 points) Conclusion may differ if a different initial seed was used.

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

(2 points) Conclusion may differ if a different initial seed was used.

\newpage


2.  ISLR, chapter 8, problem 4.   

Sketch the tree (part a), and the partition of the predictor space (part b), by hand.

(10 points, 5 for part a, 5 for part b)



2.  ISLR, chapter 8, problem 4.   Sketch the tree (part a), and the partition of the predictor space (part b), by hand.

 
