3 Classification


3.1 Seminar

You will need to load the core library for the course textbook:

library(ISLR)

3.1.1 Exercise

This question should be answered using the Weekly dataset, which is part of the ISLR package. This data contains 1,089 weekly stock returns for 21 years, from the beginning of 1990 to the end of 2010.

  1. Produce some numerical and graphical summaries of the Weekly data. Do there appear to be any patterns?

  2. Use the full dataset to perform a logistic regression with Direction as the response and the five lag variables plus Volume as predictors. Use the summary function to print the results. Do any of the predictors appear to be statistically significant? If so, which ones?

  3. Compute the confusion matrix and overall fraction of correct predictions. Explain what the confusion matrix is telling you about the types of mistakes made by logistic regression.

  4. Now fit the logistic regression model using a training data period from 1990 to 2008, with Lag2 as the only predictor. Compute the confusion matrix and the overall fraction of correct predictions for the held out data (that is, the data from 2009 and 2010).

  5. Repeat (d) using LDA.

  6. Repeat (d) using QDA.

  7. Repeat (d) using KNN with K = 1.

  8. Which of these methods appears to provide the best results on this data?

  9. Experiment with different combinations of predictors, including possible transformations and interactions, for each of the methods. Report the variables, method, and associated confusion matrix that appears to provide the best results on the held out data. Note that you should also experiment with values for K in the KNN classifier.

3.1.2 Exercise

In this problem, you will develop a model to predict whether a given car gets high or low gas mileage based on the Auto dataset from the ISLR package.

  1. Create a binary variable, mpg01, that contains a 1 if mpg contains a value above its median, and a 0 if mpg contains a value below its median. You can compute the median using the median() function. Note you may find it helpful to use the data.frame() function to create a single dataset containing both mpg01 and the other Auto variables.

  2. Explore the data graphically in order to investigate the association between mpg01 and the other features. Which of the other features seem most likely to be useful in predicting mpg01? Scatterplots and boxplots may be useful tools to answer this question. Describe your findings.

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

  4. Perform LDA on the training data in order to predict mpg01 using the variables that seemed most associated with mpg01 in (b). What is the test error of the model obtained?

  5. Perform QDA on the training data in order to predict mpg01 using the variables that seemed most associated with mpg01 in (b). What is the test error of the model obtained?

  6. Perform logistic regression on the training data in order to predict mpg01 using the variables that seemed most associated with mpg01 in (b). What is the test error of the model obtained?

  7. Perform KNN on the training data, with several values of K, in order to predict mpg01. Use only the variables that seemed most associated with mpg01 in (b). What test errors do you obtain? Which value of K seems to perform the best on this dataset?

3.1.3 Exercise

This problem involves writing functions.

  1. Write a function calc_square(), that returns the result of raising a number to the 2nd power. For example, calling calc_square(5) should return the result of \(5^2\) or 25.

    Hint: Recall that x^a raises x to the power a.

  2. Write a new function calc_power(), that allows you to pass any two numbers, x and a, and prints out the value of x^a. For example, you should be able to call calc_power(3,8) and your function should return \(3^8\) or 6561.

  3. Using the calc_power() function that you just wrote, compute \(10^3\), \(8^{17}\), and \(131^3\).

  4. Now using the calc_power() function, create a plot of \(f(x) = x^2\). The \(x\)-axis should display a range of integers from 1 to 10, and the \(y\)-axis should display \(x^2\). Label the axes appropriately, and use an appropriate title for the figure. Consider displaying either the \(x\)-axis, the \(y\)-axis, or both on the log-scale. You can do this by using log="x", log="y", or log="xy" as arguments to the plot() function.

  5. Write a function plot_power(), that allows you to create a plot of x against x^a for a fixed a and for a range of values of x. For instance:

plot_power(1:10, 3)

The result should be a plot with an \(x\)-axis taking on values \(1,2,\dots ,10\), and a \(y\)-axis taking on values \(1^3,2^3,\dots ,10^3\).