library(ISLR)
## Warning: package 'ISLR' was built under R version 3.4.4
library(ggplot2)
dim(Smarket)
## [1] 1250    9
names(Smarket)
## [1] "Year"      "Lag1"      "Lag2"      "Lag3"      "Lag4"      "Lag5"     
## [7] "Volume"    "Today"     "Direction"
ggplot(Smarket, aes (x=Year)) + geom_bar(aes(fill=Direction))+ggtitle("Stock Price Direction By Year")

attach(Smarket)
train = Year < 2005

glmFit = glm(Direction ~Lag1 + Lag2 +Lag3+Lag4+Lag5+Volume, data = Smarket, 
             subset = train, family = binomial)
#summary(glmFit)
glmprob = predict (glmFit, newdata = Smarket[!train,], type = "response")
glm_pred = ifelse(glmprob > 0.5, "Up", "Down")
Direction_2005 = Smarket$Direction[!train]

table(glm_pred, Direction_2005)
##         Direction_2005
## glm_pred Down Up
##     Down   77 97
##     Up     34 44
mean(glm_pred == Direction_2005)
## [1] 0.4801587
sm_logit = glm(Direction ~Lag1 + Lag2 +Lag3+Lag4+Lag5+Volume, data = Smarket, 
             subset = train, family = binomial (link = "logit"))

glmprob2 = predict(sm_logit, newdata = Smarket[!train,], type = "response")
glm_pred2 = ifelse(glmprob2 > 0.5, "up", "Down")
summary(glmprob2)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.4424  0.4823  0.4930  0.4917  0.5017  0.5282
table(glm_pred2, Direction_2005)
##          Direction_2005
## glm_pred2 Down Up
##      Down   77 97
##      up     34 44
mean (glm_pred2 == Direction_2005)
## [1] 0.3055556

Logit model average marginal effects

LogitScalar <- mean(dlogis(predict(sm_logit, type = "link")))
LogitScalar * coef(sm_logit)
##  (Intercept)         Lag1         Lag2         Lag3         Lag4 
##  0.047687585 -0.013511827 -0.011423648  0.001795678  0.001606326 
##         Lag5       Volume 
## -0.001053116 -0.028993973