로지스틱 회귀분석(logistic regression)은 독립변수의 선형 결합을 이용하여 사건의 발생 가능성을 예측하는데 사용되는 통계기법으로 설명변수(독립변수, X)와 범주형 목표변수(종속변수, Y) 간의 관계를 모형화하여 목표변수를 분석하거나 분류하는 통계적 방법론이다. 로지스틱 회귀분석을 활용한 분류(classification) 문제에서는 목표변수를 직접 예측(prediction)하는 것이 아닌 2개의 클래스(e.g., ‘성공’ or ‘실패’) 중 하나의 클래스로 예측할 때 사용된다(i.e., Binary Classification).
학생들의 대학 합격 여부를 예측하는 로지스틱 회귀분석을 가정해보자. 여기서 종속 변수는 합격 여부(합격=1, 불합격=0)이고, 독립 변수로는 시험 점수(GRE), 학점(GPA), 그리고 추천서의 수(Recommendations)를 사용한다. (아래 코드에서 생성한 가상데이터가 통계적으로 유의미한 결과를 가져오지 않을 수 있음.)
# 데이터 준비data <-data.frame(admit =c(0, 1, 1, 0, 1, 1), # 합격 여부gre =c(600, 700, 700, 500, 660, 680), # GRE 점수gpa =c(3.3, 3.7, 3.9, 2.5, 3.3, 3.8), # GPArecommendations =c(1, 2, 2, 1, 1, 2) # 추천서의 수)# 로지스틱 회귀 모델 적합model <-glm(admit ~ gre + gpa + recommendations, family =binomial(link ="logit"), data = data)
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
# 결과 해석summary(model)
Call:
glm(formula = admit ~ gre + gpa + recommendations, family = binomial(link = "logit"),
data = data)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -2.086e+02 5.506e+05 0 1
gre 7.937e-01 2.117e+03 0 1
gpa -9.818e+01 3.886e+05 0 1
recommendations 3.257e+01 1.612e+05 0 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 7.6382e+00 on 5 degrees of freedom
Residual deviance: 3.9359e-10 on 2 degrees of freedom
AIC: 8
Number of Fisher Scoring iterations: 23
은행에서 대출 승인 여부 예측
은행에서 고객의 신용 데이터를 기반으로 개인 대출 승인 여부를 예측하는 경우를 들 수 있다. 이 예제에서는 고객의 신용 점수(credit score), 연 소득(annual income), 대출 요청 금액(loan amount)을 독립 변수로 사용하여 대출 승인 여부(loan approval)를 종속 변수로 예측하는 모델을 구축한다. (아래 코드에서 생성한 가상데이터가 통계적으로 유의미한 결과를 가져오지 않을 수 있음.)
# 가상 데이터 생성set.seed(123) # 결과 재현성을 위한 시드 설정loan_approval <-sample(0:1, 100, replace =TRUE)credit_score <-rnorm(100, mean =650, sd =100)annual_income <-rnorm(100, mean =50000, sd =20000)loan_amount <-rnorm(100, mean =15000, sd =5000)data <-data.frame(loan_approval, credit_score, annual_income, loan_amount)# 로지스틱 회귀 모델 적합model <-glm(loan_approval ~ credit_score + annual_income + loan_amount, family =binomial(link ="logit"), data = data)# 모델 요약 결과 출력summary(model)
Call:
glm(formula = loan_approval ~ credit_score + annual_income +
loan_amount, family = binomial(link = "logit"), data = data)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 1.879e-02 1.657e+00 0.011 0.991
credit_score -1.656e-04 2.119e-03 -0.078 0.938
annual_income -1.180e-05 1.129e-05 -1.046 0.296
loan_amount 2.527e-05 4.156e-05 0.608 0.543
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 136.66 on 99 degrees of freedom
Residual deviance: 135.24 on 96 degrees of freedom
AIC: 143.24
Number of Fisher Scoring iterations: 4
# 새로운 데이터에 대한 예측new_data <-data.frame(credit_score =c(700), annual_income =c(60000), loan_amount =c(10000))predict(model, newdata = new_data, type ="response")