/
Ch3_RLab_OLD.qmd
executable file
·320 lines (276 loc) · 24.6 KB
/
Ch3_RLab_OLD.qmd
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# `R`-Lab Ch 4: Classification
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
We will begin by examining some numerical and graphical summaries of the \texttt{Smarket} data, which is part of the \texttt{ISLR library}. This data set consists of percentage returns for the $\mathrm{S} \& \mathrm{P} 500$ stock index over 1,250 days, from the beginning of 2001 until the end of $2005.$ For each date, we have recorded the percentage returns for each of the five previous trading days, \texttt{Lag1} through \texttt{Lag5}. We have also recorded volume (the number of shares traded on the previous day, in billions), \texttt{Today} (the percentage return on the date in question) and \texttt{Direction} (whether the market was \texttt{Up} or \texttt{Down} on this date).
```{r}
library("ISLR")
names(Smarket)
dim(Smarket)
summary(Smarket)
# pairs(Smarket)
```
The \texttt{cor()} function produces a matrix that contains all of the pairwise
correlations among the predictors in a data set. The first command below
gives an error message because the \texttt{Direction} variable is qualitative.
```{r}
# cor(Smarket)
cor(Smarket[,-9])
```
As one would expect, the correlations between the lag variables and today's returns are close to zero. In other words, there appears to be little correlation between today's returns and previous days' returns. The only substantial correlation is between \texttt{Year} and \texttt{Volume}. By plotting the data we see that \texttt{Volume} is increasing over time. In other words, the average number of shares traded daily increased from 2001 to 2005.
```{r}
attach(Smarket)
plot(Volume)
```
## Logistic Regression
Next, we will fit a logistic regression model in order to predict \texttt{Direction} using \texttt{Lag1} through \texttt{Lag5} and \texttt{Volume}. The \texttt{glm()} function fits generalized linear models, a class of models that includes logistic regression. The syntax of the \texttt{glm()} function is similar to that of \texttt{lm()}, except that we must pass in the argument \texttt{family=binomial} in order to tell \texttt{R} to run a logistic regression rather than some other type of generalized linear model.
```{r}
glm.fits <- glm(Direction~Lag1+Lag2+Lag3+Lag4+Lag5+Volume,data=Smarket,family=binomial)
contrasts(Direction)
summary(glm.fits)
```
The smallest p-value here is associated with \texttt{Lag1}. The negative coefficient
for this predictor suggests that if the market had a positive return yesterday,
then it is less likely to go up today (coding: \texttt{UP}$=1$, \texttt{Down}$=0$). However, at a value of 0.15, the p-value
is still relatively large, and so there is no clear evidence of a real association
between \texttt{Lag1} and \texttt{Direction}.
We use the \texttt{coef()} function in order to access just the coefficients for this
fitted model. We can also use the \texttt{summary()} function to access particular
aspects of the fitted model, such as the p-values for the coefficients.
```{r}
coef(glm.fits)
summary(glm.fits)$coef
summary(glm.fits)$coef[,4]
```
The \texttt{predict()} function can be used to predict the probability that the
market will go up, given values of the predictors. The \texttt{type="response"}
option tells \texttt{R} to output probabilities of the form $P(Y = 1|X)$, as opposed
to other information such as the logit-value. If no data set is supplied to the
\texttt{predict()} function, then the probabilities are computed for the training
data that was used to fit the logistic regression model. Here we have printed
only the first ten probabilities. We know that these values correspond to
the probability of the market going up, rather than down, because the
\texttt{contrasts()} function indicates that \texttt{R} has created a dummy variable with
a 1 for \texttt{Up}.
```{r}
glm.probs <- predict(glm.fits,type="response")
glm.probs[1:10]
contrasts(Direction)
```
In order to make a prediction as to whether the market will go up or
down on a particular day, we must convert these predicted probabilities
into class labels, \texttt{Up} or \texttt{Down}. The following two commands create a vector
of class predictions based on whether the predicted probability of a market
increase is greater than or less than $0.5$.
```{r}
glm.pred <- rep("Down",1250)
glm.pred[glm.probs>.5] <- "Up"
```
The first command creates a vector of 1,250 ``\texttt{Down}'' elements. The second line transforms to \texttt{Up} all of the elements for which the predicted probability of a market increase exceeds $0.5.$ Given these predictions, the \texttt{table()} function can be used to produce a \emph{confusion matrix} in order to determine how many observations were correctly or incorrectly classified.
```{r}
table(glm.pred, Direction) # confusion matrix
(507+145)/1250 # fraction of correct classifications (option 1)
mean(glm.pred==Direction) # fraction of correct classifications (option 2)
```
The diagonal elements of the confusion matrix indicate correct predictions, while the off-diagonals represent incorrect predictions. Hence our model correctly predicted that the market would go up on 507 days and that it would go down on 145 days, for a total of $507+145=652$ correct predictions. The \texttt{mean()} function can be used to compute the fraction of days for which the prediction was correct. In this case, logistic regression correctly predicted the movement of the market $52.2 %$ of the time. (In other words, $100-52.2=47.8 %$ is the training error rate.)
At first glance, it appears that the logistic regression model is working a little better than random guessing. However, this result is misleading because we trained and tested the model on the same set of 1,250 observations. As we have seen previously, the \emph{training error} rate is often overly optimistic--it tends to underestimate the test error rate. In order to better assess the accuracy of the logistic regression model in this setting, we can fit the model using part of the data, and then examine how well it predicts the held out data. This will yield a more realistic error rate, in the sense that in practice we will be interested in our model's performance not on the data that we used to fit the model, but rather on days in the future for which the market's movements are unknown.
To implement this strategy, we will first create a vector corresponding
to the observations from 2001 through 2004. We will then use this vector
to create a held out data set of observations from 2005.
```{r}
train <- (Year<2005) # Boolean-variable for selecting data before 2005
Smarket_2005 <- Smarket[!train,] # !TRUE == FALSE
dim(Smarket_2005)
Direction_2005 <- Direction[!train]
```
The object \texttt{train} is a vector of 1250 elements, corresponding to the observations in our data set. The elements of the vector that correspond to observations that occurred before 2005 are set to \texttt{TRUE}, whereas those that correspond to observations in 2005 are set to \texttt{FALSE}. The object \texttt{train} is a \emph{Boolean} vector, since its elements are \texttt{TRUE} and \texttt{FALSE}. Boolean vectors can be used to obtain a subset of the rows or columns of a matrix. For instance, the command \texttt{Smarket[train,]} would pick out a submatrix of the stock market data set, corresponding only to the dates before 2005, since those are the ones for which the elements of \texttt{train} are \texttt{TRUE}. The \texttt{!} symbol can be used to reverse all of the elements of a Boolean vector. That is, \texttt{!train} is a vector similar to \texttt{train}, except that the elements that are \texttt{TRUE}
in \texttt{train} get swapped to \texttt{FALSE} in \texttt{!train}, and the elements that are \texttt{FALSE} in \texttt{train} get swapped to \texttt{TRUE} in \texttt{!train}. Therefore, \texttt{Smarket[!train,]} yields a submatrix of the stock market data containing only the observations for
which \texttt{train} is \texttt{FALSE}--that is, the observations with dates in 2005. The
output above indicates that there are 252 such observations.
We now fit a logistic regression model using only the subset of the observations that correspond to dates before 2005, using the \texttt{subset} argument. We then obtain predicted probabilities of the stock market going up for each of the days in our test set--that is, for the days in 2005.
```{r}
glm.fits <- glm(Direction~Lag1+Lag2+Lag3+Lag4+Lag5+Volume,data=Smarket,family=binomial,subset=train)
glm.probs <- predict(glm.fits,Smarket_2005,type="response")
```
Notice that we have trained and tested our model on two completely separate data sets: training was performed using only the dates before 2005, and testing was performed using only the dates in 2005. Finally, we compute the predictions for 2005 and compare them to the actual movements
of the market over that time period.
```{r}
glm.pred <- rep("Down",252) # a container for saving the prediction/classifications
glm.pred[glm.probs>.5] <- "Up"
table(glm.pred,Direction_2005)
mean(glm.pred == Direction_2005) # fraction of correct predictions
mean(glm.pred != Direction_2005) # fraction of false predictions
```
The \texttt{!=} notation means \emph{not equal to}, and so the last command computes the test set error rate. The results are rather disappointing: the test error rate is 52%, which is worse than random guessing! Of course this result is not all that surprising, given that one would not generally expect to be able to use previous days’ returns to predict future market performance. (After all, if it were possible to do so, then the authors of this book would be out striking it rich rather than writing a statistics textbook.)
We recall that the logistic regression model had very underwhelming p-values associated with all of the predictors, and that the smallest p-value, though not very small, corresponded to \texttt{Lag1}. Perhaps by removing the variables that appear not to be helpful in predicting \texttt{Direction}, we can
obtain a more effective model. After all, using predictors that have no
relationship with the response tends to cause a deterioration in the test
error rate (since such predictors cause an increase in variance without a
corresponding decrease in bias), and so removing such predictors may in
turn yield an improvement. Below we have refit the logistic regression using
just \texttt{Lag1} and \texttt{Lag2}, which seemed to have the highest predictive power in
the original logistic regression model.
```{r}
glm.fits <- glm(Direction~Lag1+Lag2,data=Smarket,family=binomial,subset=train)
glm.probs <- predict(glm.fits,Smarket_2005,type="response")
glm.pred <- rep("Down",252)
glm.pred[glm.probs>.5] <- "Up"
table(glm.pred,Direction_2005) # confusion matrix
mean(glm.pred == Direction_2005) # fraction of correct predictions
106/(106+76) # fraction of correct 'up'-predictions
```
Now the results appear to be a little better: 56% of the daily movements have been correctly predicted. It is worth noting that in this case, a much simpler strategy of predicting that the market will increase every day will also be correct 56%($=(35+106)/(35+106+35+76)$) of the time! Hence, in terms of overall error rate, the logistic regression method is no better than the naive approach. However, the confusion matrix shows that on days when logistic regression predicts an increase in the market, it has a 58% accuracy rate. This suggests a possible trading strategy of buying on days when the model predicts an increasing market, and avoiding trades on days when a decrease is predicted. Of course one would need to investigate more carefully whether this small improvement was real or just due to random chance.
Suppose that we want to predict the returns associated with particular
values of \texttt{Lag1} and \texttt{Lag2}. In particular, we want to predict \texttt{Direction} on a
day when \texttt{Lag1} and \texttt{Lag2} equal \texttt{1.2} and \texttt{1.1}, respectively, and on a day when they equal $1.5$ and $-0.8$. We do this using the \texttt{predict()} function.
```{r}
predict(glm.fits, newdata=data.frame(Lag1=c(1.2, 1.5),
Lag2=c(1.1, -0.8)),
type="response")
```
## Linear Discriminant Analysis
Now we will perform LDA on the \texttt{Smarket} data. In \texttt{R}, we fit an LDA model using the \texttt{lda()} function, which is part of the \texttt{MASS} library. Notice that the syntax for the \texttt{lda()} function is identical to that of \texttt{lm()}, and to that of \texttt{glm()} except for the absence of the family option. We fit the model using only the observations before 2005.
```{r}
library("MASS")
lda.fit <- lda(Direction~Lag1+Lag2,data=Smarket,subset=train)
lda.fit
plot(lda.fit)
plot(lda.fit)
```
The LDA output indicates that $\hat{\pi}_1 = 0.492$ and $\hat{\pi}_2 = 0.508$; in other words, 49.2% of the training observations correspond to days during which the market went down. It also provides the group means; these are the average of each predictor within each class, and are used by LDA as estimates of $\mu_k$. These suggest that there is a tendency for the previous 2 days’ returns to be negative on days when the market increases, and a tendency for the previous days' returns to be positive on days when the market declines. The \emph{coefficients of linear discriminants} output provides the linear combination of \texttt{Lag1} and \texttt{Lag2} that are used to form the LDA decision rule. In other words, these are the multipliers of the elements of $X =x$ in Equation (4.19) of our textbook. If $-0.642\times \texttt{Lag1} -0.514\times\texttt{Lag2}$ is large, then the LDA classifier will predict a market increase, and if it is small, then the LDA classifier will predict a market decline. The \texttt{plot()} function produces plots of the linear discriminants, obtained by computing $-0.642\times\texttt{Lag1}-0.514\times\texttt{Lag2}$ for each of the training observations.
The `predict()` function returns a list with three elements. The first element, \texttt{class}, contains LDA's predictions about the movement of the market. The second element, \texttt{posterior}, is a matrix whose $k$th column contains the posterior probability that the corresponding observation belongs to the $k$th class, computed from Equation (4.10) of our textbook. Finally, \texttt{x} contains the linear discriminants, described earlier.
```{r}
lda.pred=predict(lda.fit, Smarket_2005)
names(lda.pred)
## Plotting the density-estimates of the discriminants
## for group 'up' and 'down'
plot(density(lda.pred$x[Smarket_2005$Direction=="Up"]),
main="", xlab="",
col="darkblue", lwd=2)
lines(density(lda.pred$x[Smarket_2005$Direction=="Down"]),
col="darkred", lwd=2)
```
As we already observed in Section 4.5 of our textbook, the LDA and logistic regression predictions are almost identical.
```{r}
lda.class=lda.pred$class
table(lda.class,Direction_2005)
mean(lda.class==Direction_2005)
```
Applying a 50% threshold to the posterior probabilities allows us to recreate the predictions contained in \texttt{lda.pred\$class}.
```{r}
sum(lda.pred$posterior[,1]>=.5)
length(lda.pred$class[lda.pred$class == "Down"])
sum(lda.pred$posterior[,1]<.5)
length(lda.pred$class[lda.pred$class == "Up"])
```
Notice that the posterior probability output by the model corresponds to the probability that the market will \emph{decrease}:
```{r}
lda.pred$posterior[1:20,1]
lda.class[1:20]
```
If we wanted to use a posterior probability threshold other than 50% in order to make predictions, then we could easily do so. For instance, suppose that we wish to predict a market decrease only if we are very certain that the market will indeed decrease on that day--say, if the posterior probability is at least 90%.
```{r}
sum(lda.pred$posterior[,1]>.9)
```
No days in 2005 meet that threshold! In fact, the greatest posterior probability of decrease in all of 2005 was 52.02%.
```{r}
max(lda.pred$posterior[,1])
which.max(lda.pred$posterior[,1])
```
## Quadratic Discriminant Analysis
We will now fit a QDA model to the \texttt{Smarket} data. QDA is implemented in \texttt{R} using the \texttt{qda()} function, which is also part of the \texttt{MASS} library. The syntax is identical to that of \texttt{lda()}.
```{r}
qda.fit=qda(Direction~Lag1+Lag2,data=Smarket,subset=train)
qda.fit
```
The output contains the group means. But it does not contain the coefficients of the linear discriminants, because the QDA classifier involves a quadratic, rather than a linear, function of the predictors. The \texttt{predict()} function works in exactly the same fashion as for LDA.
```{r}
qda.class=predict(qda.fit,Smarket_2005)$class
table(qda.class,Direction_2005)
mean(qda.class==Direction_2005)
```
Interestingly, the QDA predictions are accurate almost 60% of the time, even though the 2005 data was not used to fit the model. This level of accuracy is quite impressive for stock market data, which is known to be quite hard to model accurately. This suggests that the quadratic form assumed by QDA may capture the true relationship more accurately than the linear forms assumed by LDA and logistic regression. However, we recommend evaluating this method's performance on a larger test set before betting that this approach will consistently beat the market!
## K-Nearest Neighbors
We will now perform KNN using the \texttt{knn()} function, which is part of the \texttt{class} library. This function works rather differently from the other modelfitting functions that we have encountered thus far. Rather than a two-step approach in which we first fit the model and then we use the model to make predictions, \texttt{knn()} forms predictions using a single command. The function requires four inputs.
* A matrix containing the predictors associated with the training data, labeled \texttt{train.X} below.
* A matrix containing the predictors associated with the data for which we wish to make predictions, labeled \texttt{test.X} below.
* A vector containing the class labels for the training observations, labeled \texttt{train.Direction} below.
* A value for $K$, the number of nearest neighbors to be used by the classifier. \end{enumerate} We use the \texttt{cbind()} function, short for column bind, to bind the \texttt{Lag1} and \texttt{Lag2} variables together into two matrices, one for the training set and the other for the test set.
```{r}
library(class)
train.X=cbind(Lag1,Lag2)[train,]
test.X=cbind(Lag1,Lag2)[!train,]
train.Direction=Direction[train]
```
Now the \texttt{knn()} function can be used to predict the market's movement for the dates in 2005. We set a random seed before we apply \texttt{knn()} because if several observations are tied as nearest neighbors, then `R` will randomly break the tie. Therefore, a seed must be set in order to ensure reproducibility of results.
```{r}
set.seed(1)
knn.pred=knn(train.X,test.X,train.Direction,k=1)
table(knn.pred,Direction_2005)
(83+43)/252
```
The results using $K=1$ are not very good, since only 50% of the observations are correctly predicted. Of course, it may be that $K=1$ results in an overly flexible fit to the data. Below, we repeat the analysis using $K=3$.
```{r}
knn.pred=knn(train.X,test.X,train.Direction,k=3)
table(knn.pred,Direction_2005)
mean(knn.pred==Direction_2005)
```
The results have improved slightly. But increasing K further turns out to provide no further improvements. It appears that for this data, QDA provides the best results of the methods that we have examined so far.
## An Application to Caravan Insurance Data
Finally, we will apply the KNN approach to the \texttt{Caravan} data set, which is part of the \texttt{ISLR} library. This data set includes 85 predictors that measure demographic characteristics for 5,822 individuals. The response variable is \texttt{Purchase}, which indicates whether or not a given individual purchases a caravan insurance policy. In this data set, only 6% of people purchased caravan insurance.
```{r}
dim(Caravan)
attach(Caravan)
summary(Purchase)
348/5822
```
Because the KNN classifier predicts the class of a given test observation by identifying the observations that are nearest to it, the scale of the variables matters. Any variables that are on a large scale will have a much larger effect on the distance between the observations, and hence on the KNN classifier, than variables that are on a small scale. For instance, imagine a data set that contains two variables, \texttt{salary} and \texttt{age} (measured in dollars and years, respectively). As far as KNN is concerned, a difference of $1,000 in salary is enormous compared to a difference of 50 years in age. Consequently, \texttt{salary} will drive the KNN classification results, and \texttt{age} will have almost no effect. This is contrary to our intuition that a salary difference of $1,000 is quite small compared to an age difference of 50 years. Furthermore, the importance of scale to the KNN classifier leads to another issue: if we measured salary in Japanese yen, or if we measured \texttt{age} in minutes, then we’d get quite different classification results from what we get if these two variables are measured in dollars and years.
A good way to handle this problem is to \emph{standardize} the data so that all variables are given a mean of zero and a standard deviation of one. Then all variables will be on a comparable scale. The \texttt{scale()} function does just this. In standardizing the data, we exclude column 86, because that is the qualitative \texttt{Purchase} variable.
```{r}
standardized.X=scale(Caravan[,-86])
var(Caravan[,1])
var(Caravan[,2])
var(standardized.X[,1])
var(standardized.X[,2])
```
Now every column of \texttt{standardized.X} has a standard deviation of one and a mean of zero.
We now split the observations into a test set, containing the first 1,000 observations, and a training set, containing the remaining observations. We fit a KNN model on the training data using $K=1$, and evaluate its performance on the test data.
```{r}
test=1:1000
train.X=standardized.X[-test,]
test.X=standardized.X[test,]
train.Y=Purchase[-test]
test.Y=Purchase[test]
set.seed(1)
knn.pred=knn(train.X,test.X,train.Y,k=1)
mean(test.Y!=knn.pred)
mean(test.Y!="No")
```
The vector \texttt{test} is numeric, with values from 1 through 1,000. Typing \texttt{standardized.X[test,]} yields the submatrix of the data containing the observations whose indices range from 1 to 1, 000, whereas typing \texttt{standardized.X[-test,]} yields the submatrix containing the observations whose indices do not range from 1 to 1, 000. The KNN error rate on the 1,000 test observations is just under 12%. At first glance, this may appear to be fairly good. However, since only 6% of customers purchased insurance, we could get the error rate down to 6% by always predicting No regardless of the values of the predictors!
Suppose that there is some non-trivial cost to trying to sell insurance to a given individual. For instance, perhaps a salesperson must visit each potential customer. If the company tries to sell insurance to a random selection of customers, then the success rate will be only 6%, which may be far too low given the costs involved. Instead, the company would like to try to sell insurance only to customers who are likely to buy it. So the overall error rate is not of interest. Instead, the fraction of individuals that are correctly predicted to buy insurance is of interest.
It turns out that KNN with $K = 1$ does far better than random guessing among the customers that are predicted to buy insurance. Among 77 such customers, 9, or 11.7%, actually do purchase insurance. This is double the rate that one would obtain from random guessing.
```{r}
table(knn.pred,test.Y)
9/(68+9)
```
Using $K=3$, the success rate increases to 19%, and with $K=5$ the rate is 26.7%. This is over four times the rate that results from random guessing. It appears that KNN is finding some real patterns in a difficult data set!
```{r}
knn.pred=knn(train.X,test.X,train.Y,k=3)
table(knn.pred,test.Y)
5/26
knn.pred=knn(train.X,test.X,train.Y,k=5)
table(knn.pred,test.Y)
4/15
```
As a comparison, we can also fit a logistic regression model to the data. If we use $0.5$ as the predicted probability cut-off for the classifier, then we have a problem: only seven of the test observations are predicted to purchase insurance. Even worse, we are wrong about all of these! However, we are not required to use a cut-off of $0.5$. If we instead predict a purchase any time the predicted probability of purchase exceeds $0.25$, we get much better results: we predict that 33 people will purchase insurance, and we are correct for about 33% of these people. This is over five times better than random guessing!
```{r}
glm.fits=glm(Purchase~.,data=Caravan,family=binomial,subset=-test)
glm.probs=predict(glm.fits,Caravan[test,],type="response")
glm.pred=rep("No",1000)
glm.pred[glm.probs>.5]="Yes"
table(glm.pred,test.Y)
glm.pred=rep("No",1000)
glm.pred[glm.probs>.25]="Yes"
table(glm.pred,test.Y)
11/(22+11)
```