Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

DS 303 Homework 10

Due:  Nov .  28, 2022 on Canvas by 11:59 pm (CT)

Instructions: Homework is to be submitted on Canvas by the deadline stated above. Please clearly print your name and student ID number on your HW.

Show your work (including calculations) to receive full credit. Please work hard to make your submission as readable as you possibly can - this means no raw R output or code (unless it is asked for specically or needed for clarity).

Code should be submitted with your homework as a separate le (for example, a .R le, text le, Word le, or .Rmd are all acceptable). You should mark sections of the code that correspond to dierent homework problems using comments (e.g. #####  Problem  1 #####).

Problem 1:  MNIST handwritten digit database

Load the handwritten digits (MNIST) dataset into R using the R scripts we went over in class.

a.  Randomly select 3000 observations from the training set and randomly select 100 observations from the test set. Implement KNN classification. Report the following:

  Carry out 10-fold cross-validation on the training set to determine the optimal K . Try K = 1, 5, 7, 9. What is the optimal K?

 Use this optimal  K  to implement KNN classification on the test set.   Report your confusion matrix and misclassification error rate on the test set.

b.  Try to implement LDA on the MNIST dataset. What kind of error message do you obtain? Do some searching and explain what this error message means. Hint: check var(train$x[,1]).

c.  Try to implement multinomial logistic regression on the MNIST dataset. What kind of error message do you obtain?

d.  Discuss how this dataset highlights some of the advantages of using KNN for classification.

Problem 2:  Fashion MNIST

Many people consider the handwritten digits database to be far too easy for classifiers these days. More challenging datasets have appeared as new benchmarks. One example is the fashion MNIST dataset:  https://github.com/zalandoresearch/fashion-mnist.  Read up on the documentation for this dataset.  Then, just like we did with the handwritten digits database, download the training and test set for this data. Load it into R using the same R scripts used in Problem 1.

a.  Produce plots of the rst 5 observations in the training set. What do you see?

b.  Repeat Problem 1(a) for this dataset. How do your confusion matrices and misclassification error rates compare?

Problem 3:  Conceptual Review

a.  Suppose we obtained ten bootstrapped samples from a data set where Y can take two values: red or green.  We then apply a classification tree to each bootstrapped sample, and for a specific value of X, produce 10 estimates of P (Y = redlX):

0.1, 0.15, 0.2, 0.2, 0.55, 0.6, 0.6, 0.65, 0.7, 0.75.

There are two common ways to combine these results together into a single class prediction. One is the majority vote approach discussed in lecture.  The second approach is to classify based on the average probability. In this example, what is the nal classification under each of these two approaches?

b.  See Figure 1. Sketch the tree corresponding to the partition of the predictor space illustrated on the left-hand side of Figure 1. The numbers inside the boxes indicate the mean of Y within each region.

c.  See Figure 1.   Create a diagram  (similar to the left-hand size of Figure 1) using the tree illustrated in the right-hand side of Figure 1. You should divide up the predictor space into the correct regions, and indicate the mean for each region.

 

 

 

 

X  2

 

 

 

 

1

 

0

1

X

1

X2 < 1

|

 


X2 < 2

X1 < 0

2.49

 

- 1.06                   0.21

- 1.80                                   0.63

Figure 1: Figure corresponding to Problem 3 (b) and (c)

Problem 4:  Basics of Decision Trees

Use the OJ data set, which is part of the ISLR2 package, for this problem.

a.  Create a training set containing a random sample of 800 observations, and a test set containing the remaining observations.

b. Fit a tree to the training data with Purchase as the response and the other variables as predictors.  Produce summary statistics about the tree (using the summary() function) and describe the results obtained.  What is the training error?  How many terminal nodes does the tree have?

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.

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

e.  Predict the response on the test set and report the confusion matrix. What is the test error?

f. Apply cv .tree() to determine the optimal tree size.  Produce a plot with tree size on the

x-axis and cross-validated classification error rate on the y-axis.

g. What tree size corresponds to the lowest cross-validated classification error rate?

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

i.  Compare the training error rates between the pruned and un-pruned trees. Which is higher? Is this what you expect? Explain.

j.  Compare the test errors rates between the pruned and un-pruned trees. Which is higher? Is this what you expect? Explain.

Problem 5:  Bagging and Random Forests

We’ll use the Carseats for this problem;  it is part of the  ISLR2 library.   Convert Sales to a qualitative response, the same way we did in class.

a.  Split the data set into a training and test set.

b. Fit a classification tree to the training set. What splitting criteria did you use? Plot the tree here and interpret the results. What test MSE did you obtain?

c. Use cross-validation in order to obtain the optimal level of tree complexity. What size tree is optimal? What is the test MSE for the pruned tree?

d. Implementing bagging on the training set.  Set B = 500, where B is the number of trees. What test MSE do you obtain? Use the importance() function to determine which variables are the most important and report them here.

e. Implement random forests on the training. Experiment with different values of m and report the test MSE for different values of m in a table.

f. Looking at your table from part (e), would it be appropriate to choose the m that gives us the smallest test MSE? Explain.  (Hint: the answer is no.)

Problem 6:  Boosting

We’ll use the Hitters for this problem; it is part of the ISLR2 library.

a.  Remove the observations for whom the salary information is unknown, and then log-transform the salaries.

b.  Create a training set consisting of the rst 200 observations, and a test set consisting of the remaining observations.

c.  Perform a grid search on the training set to decide the optimal number of trees, the optimal λ, and optimal depth.  To keep things computationally simple, only considered 3 different values for each of the tuning parameters. You may decide what those values are.

d. Implement boosting on the training set with the tuning parameters you have selected from the grid search. Which variables appear to be the most important predictors in the boosted model?

e. What is the test MSE of the boosted model from part (d)?

f. Now apply bagging to the training set. What is the test set MSE for this approach?