Logistic and Discriminant

#########################################
######################################### Logistic Regression 
#########################################   by South African Hearth Disease Data
#install.packages("ElemStatLearn")
library(ElemStatLearn)
data(SAheart)
# 전처리 과정
heart <-SAheart[,c(1:3,5,7:10)]
# 로지스틱 분석
heartfit<-glm( chd~.,data=heart, family = binomial)
summary(heartfit)
## 
## Call:
## glm(formula = chd ~ ., family = binomial, data = heart)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.7517  -0.8378  -0.4552   0.9292   2.4434  
## 
## Coefficients:
##                  Estimate Std. Error z value Pr(>|z|)    
## (Intercept)    -4.1295997  0.9641558  -4.283 1.84e-05 ***
## sbp             0.0057607  0.0056326   1.023  0.30643    
## tobacco         0.0795256  0.0262150   3.034  0.00242 ** 
## ldl             0.1847793  0.0574115   3.219  0.00129 ** 
## famhistPresent  0.9391855  0.2248691   4.177 2.96e-05 ***
## obesity        -0.0345434  0.0291053  -1.187  0.23529    
## alcohol         0.0006065  0.0044550   0.136  0.89171    
## age             0.0425412  0.0101749   4.181 2.90e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 596.11  on 461  degrees of freedom
## Residual deviance: 483.17  on 454  degrees of freedom
## AIC: 499.17
## 
## Number of Fisher Scoring iterations: 4
#########################################
######################################### Discriminant Analysis
#########################################   by South African Hearth Disease Data
# 판별분석 모형식
require(MASS)
## Loading required package: MASS
disc.fit <- lda(factor(chd) ~., data = heart)
# 분류표
pred_chd <- predict(disc.fit, newdata = heart[,1:7])
result <- table(heart$chd, pred_chd$class)
# 정분류율
accuracy <- (result[1, 1] + result[2, 2]) / sum(result) * 100