CHEN, PO-YU / Student ID:606890100

This question should be answered using the Weekly data set, which is part of the ISLR package. This data is similar in nature to the Smarket data from this chapter’s lab, except that it contains 1, 089 weekly 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?

  1. Yes,the predictor Lag2 appears to be statistically significant.

## 
## Call:
## glm(formula = Direction ~ Lag1 + Lag2 + Lag3 + Lag4 + Lag5 + 
##     Volume, family = binomial, data = Weekly)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.6949  -1.2565   0.9913   1.0849   1.4579  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)   
## (Intercept)  0.26686    0.08593   3.106   0.0019 **
## Lag1        -0.04127    0.02641  -1.563   0.1181   
## Lag2         0.05844    0.02686   2.175   0.0296 * 
## Lag3        -0.01606    0.02666  -0.602   0.5469   
## Lag4        -0.02779    0.02646  -1.050   0.2937   
## Lag5        -0.01447    0.02638  -0.549   0.5833   
## Volume      -0.02274    0.03690  -0.616   0.5377   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 1496.2  on 1088  degrees of freedom
## Residual deviance: 1486.4  on 1082  degrees of freedom
## AIC: 1500.4
## 
## Number of Fisher Scoring iterations: 4

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

Flase Positive(FP) : 430

Flase Negative(FN) : 48

##      Up
## Down  0
## Up    1
##         Direction
## glm.pred Down  Up
##     Down   54  48
##     Up    430 557

(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).

train <- (Year <= 2008)
W.2008 <- Weekly[!train, ]
dim(W.2008)
## [1] 104   9
D.2008 <- Direction[!train]
glm.fit <- glm(Direction~Lag2, data = Weekly, family = binomial,subset = train)
glm.p <- predict(glm.fit, W.2008, type = "response")
glm.pred <- rep("Down", 104)
glm.pred[glm.p > .5] <- "Up"
table(glm.pred, D.2008)
##         D.2008
## glm.pred Down Up
##     Down    9  5
##     Up     34 56
(9 + 56) / 104 # =Accuracy
## [1] 0.625
mean(glm.pred == D.2008)# =Accuracy
## [1] 0.625

Code

library(ISLR)
names(Weekly)
options(show.signif.stars = T)
summary(Weekly)
pairs(Weekly)

cor(Weekly[ ,-9])
attach(Weekly)
## The following objects are masked from Weekly (pos = 3):
## 
##     Direction, Lag1, Lag2, Lag3, Lag4, Lag5, Today, Volume, Year
plot(Volume)

glm.fit <- glm(Direction~Lag1 + Lag2 + Lag3 + Lag4 + Lag5 + Volume, data = Weekly, family = binomial)
options(show.signif.stars = T)
summary(glm.fit)
coef(glm.fit)
summary(glm.fit)$coef
glm.p <- predict(glm.fit, type = "response")
glm.p[1 : 10]
contrasts(Direction)
glm.pred <- rep("Down", 1089)
glm.pred[glm.p > .5] <- "Up"
glm.pred[1:10]
table(glm.pred, Direction)
(54 + 557)  / 1089
mean(glm.pred == Direction)
train <- (Year <= 2008)
W.2008 <- Weekly[!train, ]
dim(W.2008)
D.2008 <- Direction[!train]
glm.fit <- glm(Direction~Lag2, data = Weekly, family = binomial,subset = train)
glm.p <- predict(glm.fit, W.2008, type = "response")
glm.pred <- rep("Down", 104)
glm.pred[glm.p > .5] <- "Up"
table(glm.pred, D.2008)
(9 + 56) / 104
mean(glm.pred == D.2008)