forked from pjsio/ME114
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathME114_assignment4_LASTNAME_FIRSTNAME.Rmd
84 lines (51 loc) · 5.37 KB
/
ME114_assignment4_LASTNAME_FIRSTNAME.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
---
title: "Assignment 4 - Classification and non-linear models"
author: "Ken Benoit and Slava Mikhaylov"
output: html_document
---
Assignments for the course focus on practical aspects of the concepts covered in the lectures. Assignments are largely based on the material covered in James et al (2013). You will start working on the assignment in the lab sessions after the lectures, but may need to finish them after class.
You will be asked to submit your assignments via Moodle by 7pm on the day of the class. We will subsequently open up solutions to the problem sets.
You will need to load the core library for the course textbook:
```{r}
library(ISLR)
```
## Part I: Classification
### Exercise 4.1
This question should be answered using the `Weekly` data set, 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.
(a) Produce some numerical and graphical summaries of the `Weekly` data. Do there appear to be any patterns?
(b) Use the full data set 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?
(c) 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.
(d) 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).
(e) Experiment with different combinations of predictors, including possible transformations and interactions. Report the variables, method, and associated confusion matrix that appears to provide the best results on the held out data.
### Exercise 4.2
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.
(a) 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 data set containing both `mpg01` and the other `Auto` variables.
(b) 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.
(c) Split the data into a training set and a test set.
(d) 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?
### Exercise 4.3
This problem involves writing functions.
(a) Write a function, `power23()`, that prints out the result of raising 2 to the 3rd power. In other words, your function should compute $2^3$ and print out the results.
*Hint: Recall that `x^a` raises `x` to the power `a`. Use the `print()` function to output the result.*
(b) Create a new function, `power()`, that allows you to pass *any* two numbers, `x` and `a`, and prints out the value of `x^a`. You can do this by beginning your function with the line
```{r eval=FALSE}
power <- function(x,a) {
```
You should be able to call your function by entering, for instance,
```{r eval=FALSE}
power(3,8)
```
on the command line. This should output the value of $3^8$, namely, 6,561.
(c) Using the `power()` function that you just wrote, compute $10^3$, $8^{17}$, and $131^3$.
(d) Now using the `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.
(e) Create a function, `plotPower()`, 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, if you call
```{r eval=FALSE}
plotPower(1:10, 3)
```
then a plot should be created 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$.
### Exercise 4.4 (Optional)
This question relates to the `College` dataset from the `ISLR` package.
(a) Split the data into a training set and a test set. Using out-of-state tuition as the response and the other variables as the predictors, perform forward stepwise selection (*Hint: see Chapter 6 in James et al 2013*) on the training set in order to identify a satisfactory model that uses just a subset of the predictors.
(b) Fit a GAM on the training data, using out-of-state tuition as the response and the features selected in the previous step as the predictors. Plot the results, and explain your findings.
(c) Evaluate the model obtained on the test set, and explain the results obtained.
(d) For which variables, if any, is there evidence of a non-linear relationship with the response?