library(ISLR)
summary(Weekly)
## Year Lag1 Lag2 Lag3
## Min. :1990 Min. :-18.195 Min. :-18.195 Min. :-18.195
## 1st Qu.:1995 1st Qu.: -1.154 1st Qu.: -1.154 1st Qu.: -1.158
## Median :2000 Median : 0.241 Median : 0.241 Median : 0.241
## Mean :2000 Mean : 0.151 Mean : 0.151 Mean : 0.147
## 3rd Qu.:2005 3rd Qu.: 1.405 3rd Qu.: 1.409 3rd Qu.: 1.409
## Max. :2010 Max. : 12.026 Max. : 12.026 Max. : 12.026
## Lag4 Lag5 Volume Today
## Min. :-18.195 Min. :-18.195 Min. :0.087 Min. :-18.195
## 1st Qu.: -1.158 1st Qu.: -1.166 1st Qu.:0.332 1st Qu.: -1.154
## Median : 0.238 Median : 0.234 Median :1.003 Median : 0.241
## Mean : 0.146 Mean : 0.140 Mean :1.575 Mean : 0.150
## 3rd Qu.: 1.409 3rd Qu.: 1.405 3rd Qu.:2.054 3rd Qu.: 1.405
## Max. : 12.026 Max. : 12.026 Max. :9.328 Max. : 12.026
## Direction
## Down:484
## Up :605
##
##
##
##
set.seed(1)
attach(Weekly)
glm.fit = glm(Direction ~ Lag1 + Lag2, data = Weekly, family = binomial)
summary(glm.fit)
##
## Call:
## glm(formula = Direction ~ Lag1 + Lag2, family = binomial, data = Weekly)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.62 -1.26 1.00 1.08 1.51
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.2212 0.0615 3.60 0.00032 ***
## Lag1 -0.0387 0.0262 -1.48 0.13967
## Lag2 0.0602 0.0265 2.27 0.02323 *
## ---
## 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: 1488.2 on 1086 degrees of freedom
## AIC: 1494
##
## Number of Fisher Scoring iterations: 4
glm.fit = glm(Direction ~ Lag1 + Lag2, data = Weekly[-1, ], family = binomial)
summary(glm.fit)
##
## Call:
## glm(formula = Direction ~ Lag1 + Lag2, family = binomial, data = Weekly[-1,
## ])
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.63 -1.26 1.00 1.08 1.51
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.2232 0.0615 3.63 0.00028 ***
## Lag1 -0.0384 0.0262 -1.47 0.14268
## Lag2 0.0608 0.0266 2.29 0.02197 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 1494.6 on 1087 degrees of freedom
## Residual deviance: 1486.5 on 1085 degrees of freedom
## AIC: 1493
##
## Number of Fisher Scoring iterations: 4
predict.glm(glm.fit, Weekly[1, ], type = "response") > 0.5
## 1
## TRUE
Prediction was UP, true Direction was DOWN.
count = rep(0, dim(Weekly)[1])
for (i in 1:(dim(Weekly)[1])) {
glm.fit = glm(Direction ~ Lag1 + Lag2, data = Weekly[-i, ], family = binomial)
is_up = predict.glm(glm.fit, Weekly[i, ], type = "response") > 0.5
is_true_up = Weekly[i, ]$Direction == "Up"
if (is_up != is_true_up)
count[i] = 1
}
sum(count)
## [1] 490
490 errors.
mean(count)
## [1] 0.45
LOOCV estimates a test error rate of 45%.