data(mtcars)
library(dplyr)
## 
## 다음의 패키지를 부착합니다: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
glimpse(mtcars)
## Rows: 32
## Columns: 11
## $ mpg  <dbl> 21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2, 17.8,…
## $ cyl  <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 8,…
## $ disp <dbl> 160.0, 160.0, 108.0, 258.0, 360.0, 225.0, 360.0, 146.7, 140.8, 16…
## $ hp   <dbl> 110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, 180, 180…
## $ drat <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.92, 3.92,…
## $ wt   <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3.150, 3.…
## $ qsec <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 22.90, 18…
## $ vs   <dbl> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,…
## $ am   <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0,…
## $ gear <dbl> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3, 3,…
## $ carb <dbl> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, 1, 1, 2,…
mtcars$vs<-as.factor(mtcars$vs)
mtcars$am<-as.factor(mtcars$am)
mtcars %>% count(vs)
##   vs  n
## 1  0 18
## 2  1 14
glm(vs~mpg+am,data=mtcars,family=binomial)
## 
## Call:  glm(formula = vs ~ mpg + am, family = binomial, data = mtcars)
## 
## Coefficients:
## (Intercept)          mpg          am1  
##    -12.7051       0.6809      -3.0073  
## 
## Degrees of Freedom: 31 Total (i.e. Null);  29 Residual
## Null Deviance:       43.86 
## Residual Deviance: 20.65     AIC: 26.65
exp(0.6809)
## [1] 1.975655
exp(-3.0073)
## [1] 0.04942495
# 로짓계수 해석과 오즈비
# mpg 해석: am이 주어질때 mpg가 한 단위 증가함에 따라 
# vs가 1일 오즈가 exp(0.6809)=1.98(98%증가)
# am 해석: mpg이 주어질때 vs가 1일 오즈가 exp(-3.0073)=0.05(95% 감소)
# am 트랜스미션 0:수동 1:자동 
# vs:0 flat engine,1:straight engine
# mpg 연비