ADP (R)

[R] 로지스틱 회귀 연습 (with mtcars)

멋쟁이천재사자 2022. 10. 26. 20:54

#이항 로지스틱 회귀 분석 : 예측 결과가 0,1 이 아님


# 1-1. 종속변수 am 와 나머지 독립변수에 로지스틱 회귀 분석
rm(list=ls())

# 상관성 낮아 보임 carb vs qsec hp 
library(PerformanceAnalytics)
chart.Correlation(mtcars, histogram=TRUE, pch="+")
sum(is.na(mtcars))
str(mtcars)

# Warning messages:
#   1: glm.fit: 알고리즘이 수렴하지 않았습니다 
mtcars.glm <- glm(formula=am ~ .,data=mtcars,family = binomial)
#mtcars.glm <- glm(formula=am ~ .,data=mtcars,family = "binomial")

# mpg cyl disp hp wt vs gear carb 10 이상임 
library(car)
vif(mtcars.glm)

#모델의 유의성 : chisquare 통계량을 이용해 분석 ~ 유의함
pchisq(mtcars.glm$null.deviance - mtcars.glm$deviance,
       mtcars.glm$df.null -mtcars.glm$df.residual,
       lower.tail = F)

# 회귀계수 유의성 : 유의한 계수가 없음
summary(mtcars.glm)

#과산포 확인. 2 미만이므로 과산포 위험 없음
deviance(mtcars.glm) / mtcars.glm$df.residual

mtcars.glm.step <- step(mtcars.glm,direction = "both")
summary(mtcars.glm)
summary(mtcars.glm.step)

pred <- predict(mtcars.glm.step,mtcars,type="response")

# Error: `data` and `reference` should be factors with the same levels.
confusionMatrix(pred,mtcars$am)

pred.f <- factor(ifelse(pred>0.5,1,0))
confusionMatrix(pred.f,as.factor(mtcars$am))