-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch4lab.R
45 lines (42 loc) · 1.35 KB
/
ch4lab.R
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
library(ISLR)
names(Smarket)
summary(Smarket)
pairs(Smarket)
cor(Smarket)
cor(Smarket[,1:8])
attach(Smarket)
plot(Volume)
plot(Year, Volume)
glm.fits = glm(Direction~Lag1+Lag2+Lag3+Lag4+Lag5+Volume,
data=Smarket, family = binomial)
summary(glm.fits)
coef(glm.fits)
summary(glm.fits)$coef
summary(glm.fits)$coef[,4]
glm.probs = predict(glm.fits, type = "response")
glm.probs[1:10]
contrasts(Direction)
glm.pred = rep("Down", 1250)
glm.pred[glm.probs > .5] = "Up"
table(glm.pred, Direction)
# Up to here we used the same data to train and validate
train = (Year < 2005)
Smarket.2005 = Smarket[!train,]
dim(Smarket.2005)
Direction.2005 = Direction[!train]
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")
glm.pred = rep("Down", 252)
glm.pred[glm.probs > .5] = "Up"
table(glm.pred, Direction.2005)
mean(glm.pred == Direction.2005)
# Remove indicators with low p value
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)
mean(glm.pred == Direction.2005)
predict(glm.fits, data.frame(Lag1 = c(1.2,1.5), Lag2 = c(1.1,-0.8)), type="response")