library(ISLR2)
library(MASS)
##
## Attaching package: 'MASS'
## The following object is masked from 'package:ISLR2':
##
## Boston
attach(Smarket)
train <- (Year < 2005)
Smarket.2005 <- Smarket[!train, ]
Direction.2005 <- Direction[!train]
lda.fit <- lda(Direction ~ Lag1+ Lag2, data = Smarket, subset = train)
lda.fit
## Call:
## lda(Direction ~ Lag1 + Lag2, data = Smarket, subset = train)
##
## Prior probabilities of groups:
## Down Up
## 0.491984 0.508016
##
## Group means:
## Lag1 Lag2
## Down 0.04279022 0.03389409
## Up -0.03954635 -0.03132544
##
## Coefficients of linear discriminants:
## LD1
## Lag1 -0.6420190
## Lag2 -0.5135293
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 coeffcients of linear
discriminants output provides the linear combination of
Lag1
and 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.24). If
−0.642 × Lag1
−0.514 × Lag2
is large, then the
LDA classifer will predict a market increase, and if it is small, then
the LDA classifer will predict a market decline.
plot(lda.fit)
The
plot()
function produces plots of the linear
discriminants, obtained by computing −0.642 × Lag1
−0.514 ×
Lag2
for each of the training observations. The
Up
and Down
observations are displayed
separately
lda.pred <- predict(lda.fit, Smarket.2005)
names(lda.pred)
## [1] "class" "posterior" "x"
The predict()
function returns a list with three
elements. The frst element, class, contains LDA’s predictions about the
movement of the market. The second element, posterior
, is a
matrix whose kth column contains the posterior probability that the
corresponding observation belongs to the kth class, computed from
(4.15). Finally, x
contains the linear discriminants,
described earlier.
As we observed in Section 4.5, the LDA and logistic regression predictions are almost identical.
lda.class <- lda.pred$class
table(lda.class, Direction.2005)
## Direction.2005
## lda.class Down Up
## Down 35 35
## Up 76 106
mean(lda.class == Direction.2005)
## [1] 0.5595238
Applying a 50 % threshold to the posterior probabilities allows us to
recreate the predictions contained in lda.pred$class
.
sum(lda.pred$posterior[, 1] >= .5)
## [1] 70
sum(lda.pred$posterior[, 1] < .5)
## [1] 182
Notice that the posterior probability output by the model corresponds to the probability that the market will decrease:
lda.pred$posterior[1:20, 1]
## 999 1000 1001 1002 1003 1004 1005 1006
## 0.4901792 0.4792185 0.4668185 0.4740011 0.4927877 0.4938562 0.4951016 0.4872861
## 1007 1008 1009 1010 1011 1012 1013 1014
## 0.4907013 0.4844026 0.4906963 0.5119988 0.4895152 0.4706761 0.4744593 0.4799583
## 1015 1016 1017 1018
## 0.4935775 0.5030894 0.4978806 0.4886331
lda.class[1:20]
## [1] Up Up Up Up Up Up Up Up Up Up Up Down Up Up Up
## [16] Up Up Down Up Up
## Levels: Down Up
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 %.
sum(lda.pred$posterior[, 1] > .9)
## [1] 0
No days in 2005 meet that threshold! In fact, the greatest posterior probability of decrease in all of 2005 was 52.02 %
qda.fit <- qda(Direction ~ Lag1 + Lag2, data = Smarket, subset = train)
qda.fit
## Call:
## qda(Direction ~ Lag1 + Lag2, data = Smarket, subset = train)
##
## Prior probabilities of groups:
## Down Up
## 0.491984 0.508016
##
## Group means:
## Lag1 Lag2
## Down 0.04279022 0.03389409
## Up -0.03954635 -0.03132544
The output contains the group means. But it does not contain the
coeffcients of the linear discriminants, because the QDA classifer
involves a quadratic, rather than a linear, function of the predictors.
The predict()
function works in exactly the same fashion as
for LDA.
qda.class <- predict(qda.fit, Smarket.2005)$class
table(qda.class, Direction.2005)
## Direction.2005
## qda.class Down Up
## Down 30 20
## Up 81 121
mean(qda.class == Direction.2005)
## [1] 0.5992063
Interestingly, the QDA predictions are accurate almost 60 % of the time, even though the 2005 data was not used to ft 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!
By default, this implementation of the naive Bayes classifer models each quantitative feature using a Gaussian distribution. However, a kernel density method can also be used to estimate the distributions.
library(e1071)
nb.fit <- naiveBayes(Direction ~ Lag1 + Lag2, data = Smarket, subset = train)
nb.fit
##
## Naive Bayes Classifier for Discrete Predictors
##
## Call:
## naiveBayes.default(x = X, y = Y, laplace = laplace)
##
## A-priori probabilities:
## Y
## Down Up
## 0.491984 0.508016
##
## Conditional probabilities:
## Lag1
## Y [,1] [,2]
## Down 0.04279022 1.227446
## Up -0.03954635 1.231668
##
## Lag2
## Y [,1] [,2]
## Down 0.03389409 1.239191
## Up -0.03132544 1.220765
The output contains the estimated mean and standard deviation for
each variable in each class. For example, the mean for Lag1
is 0.0428 for Direction=Down
, and the standard deviation is
1.23. We can easily verify this:
mean(Lag1[train][Direction[train] == "Down"])
## [1] 0.04279022
sd(Lag1[train][Direction[train] == "Down"])
## [1] 1.227446
The predict() function is straightforward.
nb.class <- predict(nb.fit, Smarket.2005)
table(nb.class, Direction.2005)
## Direction.2005
## nb.class Down Up
## Down 28 20
## Up 83 121
mean(nb.class == Direction.2005)
## [1] 0.5912698
Naive Bayes performs very well on this data, with accurate predictions over 59% of the time. This is slightly worse than QDA, but much better than LDA.
The predict()
function can also generate estimates of
the probability that each observation belongs to a particular class.
nb.preds <- predict(nb.fit, Smarket.2005, type = "raw")
nb.preds[1:5, ]
## Down Up
## [1,] 0.4873164 0.5126836
## [2,] 0.4762492 0.5237508
## [3,] 0.4653377 0.5346623
## [4,] 0.4748652 0.5251348
## [5,] 0.4901890 0.5098110
The function knn()
requires four inputs.
1. A matrix containing the predictors associated with the training data,
labeled train.X
below
2. A matrix containing the predictors associated with the data for which
we wish to make predictions, labeled test.X
below
3. A vector containing the class labels for the training observations,
labeled train.Direction
below
4. A value for K, the number of nearest neighbors to be used by
the classifier
We use the cbind()
function, short for column
bind, to bind the Lag1
and Lag2
variables
together into two matrices, one for the training set and the other for
the test set
library(class)
train.X <- cbind(Lag1, Lag2)[train, ]
test.X <- cbind(Lag1, Lag2)[!train, ]
train.Direction <- Direction[train]
Now the knn()
function can be used to predict the
market’s movement for the dates in 2005. We set a random seed before we
apply 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.
set.seed(1)
knn.pred <- knn(train.X, test.X, train.Direction, k = 1)
table(knn.pred, Direction.2005)
## Direction.2005
## knn.pred Down Up
## Down 43 58
## Up 68 83
(83 + 43) / 252
## [1] 0.5
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 fexible ft to the data. Below, we repeat the analysis using K = 3.
knn.pred <- knn(train.X, test.X, train.Direction, k = 3)
table(knn.pred, Direction.2005)
## Direction.2005
## knn.pred Down Up
## Down 48 54
## Up 63 87
mean(knn.pred == Direction.2005)
## [1] 0.5357143
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.
KNN does not perform well on the Smarket
data but it does
often provide impressive results. As an example we will apply the KNN
approach to the Caravan
data set, which is part of the
ISLR2
library. This data set includes 85 predictors that
measure demographic characteristics for 5,822 individuals. The response
variable is 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.
dim(Caravan)
## [1] 5822 86
attach(Caravan)
summary(Purchase)
## No Yes
## 5474 348
348 / 5822
## [1] 0.05977327
Because the KNN classifer predicts the class of a given test
observation by identifying the observations that are nearest to it, the
scale of the variables matters. Variables that are on a large scale will
have a much larger efect on the distance between the
observations, and hence on the KNN classifer, than variables that are on
a small scale. For instance, imagine a data set that contains two
variables, salary
and age
(measured in dollars
and years, respectively). As far as KNN is concerned, a diference of
$1,000 in salary is enormous compared to a diference of 50 years in age.
Consequently, salary
will drive the KNN classifcation
results, and age
will have almost no efect. This is
contrary to our intuition that a salary diference of $1,000 is quite
small compared to an age diference of 50 years. Furthermore, the
importance of scale to the KNN classifer leads to another issue: if we
measured salary
in Japanese yen, or if we measured
age
in minutes, then we’d get quite diferent classifcation
results from what we get if these two variables are measured in dollars
and years.
A good way to handle this problem is to 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
scale()
function does just this. In standardizing the data,
we exclude column 86, because that is the qualitative
Purchase
variable.
standardized.X <- scale(Caravan[, -86])
var(Caravan[, 1])
## [1] 165.0378
var(Caravan[, 2])
## [1] 0.1647078
var(standardized.X[, 1])
## [1] 1
var(standardized.X[, 2])
## [1] 1
Now every column of standardized.X
has a standard
deviation of one and a mean of zero.
We now split the observations into a test set, containing the frst 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.
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)
## [1] 0.118
mean(test.Y != "No")
## [1] 0.059
The vector test is numeric, with values from 1 through 1, 000. Typing
standardized.X[test, ]
yields the submatrix of the data
containing the observations whose indices range from 1 to 1, 000,
whereas typing 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
frst 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.
table(knn.pred, test.Y)
## test.Y
## knn.pred No Yes
## No 873 50
## Yes 68 9
9 / (68 + 9)
## [1] 0.1168831
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 fnding some real patterns in a diffcult data set!
knn.pred <- knn(train.X, test.X, train.Y, k = 3)
table(knn.pred, test.Y)
## test.Y
## knn.pred No Yes
## No 920 54
## Yes 21 5
5 / 26
## [1] 0.1923077
knn.pred <- knn(train.X, test.X, train.Y, k = 5)
table(knn.pred, test.Y)
## test.Y
## knn.pred No Yes
## No 930 55
## Yes 11 4
4 / 15
## [1] 0.2666667
However, while this strategy is cost-efective, it is worth noting
that only 15 customers are predicted to purchase insurance using KNN
with K = 5. In practice, the insurance company may wish to expend
resources on convincing more than just 15 potential customers to buy
insurance.
As a comparison, we can also ft a logistic regression model to the data.
If we use 0.5 as the predicted probability cut-of for the classifer,
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-of 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!
glm.fits <- glm(Purchase ~ ., data = Caravan, family = binomial, subset = -test)
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
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)
## test.Y
## glm.pred No Yes
## No 934 59
## Yes 7 0
glm.pred <- rep("No", 1000)
glm.pred[glm.probs > .25] <- "Yes"
table(glm.pred, test.Y)
## test.Y
## glm.pred No Yes
## No 919 48
## Yes 22 11
11 / (22 + 11)
## [1] 0.3333333