다중 로지스틱 회귀

R 중급 회귀

Richie Cotton

Data Evangelist at DataCamp

은행 이탈(churn) 데이터셋

has_churned time_since_first_purchase time_since_last_purchase
0 0.3993247 -0.5158691
1 -0.4297957 0.6780654
0 3.7383122 0.4082544
0 0.6032289 -0.6990435
... ... ...
이탈 여부 관계 지속 기간 최근 활동 시점
1 https://www.rdocumentation.org/packages/bayesQR/topics/Churn
R 중급 회귀

glm()

glm(response ~ explanatory, data = dataset, family = binomial)
glm(response ~ explanatory1 + explanatory2, data = dataset, family = binomial)
glm(response ~ explanatory1 * explanatory2, data = dataset, family = binomial)
R 중급 회귀

예측 흐름

explanatory_data <- expand_grid(
  explanatory1 = some_values,
  explanatory2 = some_values
)
prediction_data <- explanatory_data %>% 
  mutate(
    has_churned = predict(mdl, explanatory_data, type = "response")
  )
R 중급 회귀

네 가지 결과

실제: 거짓 실제: 참
예측: 거짓 정확 거짓 음성
예측: 참 거짓 양성 정확
1 https://campus.datacamp.com/courses/introduction-to-regression-in-r/simple-logistic-regression?ex=10
R 중급 회귀

혼동 행렬

actual_response <- dataset$response
predicted_response <- round(fitted(mdl))
outcomes <- table(predicted_response, actual_response)
confusion <- conf_mat(outcomes)
autoplot(confusion)
summary(confusion, event_level = "second")
R 중급 회귀

시각화

  • 범주형 변수에는 패싯을 사용합니다.
  • 수치형 설명 변수가 2개면, 반응을 색으로 표시합니다.
  • 0.5 미만은 한 색, 0.5 초과는 다른 색으로 구분합니다.
scale_color_gradient2(midpoint = 0.5)
R 중급 회귀

연습해 봅시다!

R 중급 회귀

Preparing Video For Download...