주어진 컷오프에서 링크 함수 비교
이번 마지막 연습 문제에서는 세 가지 링크 함수(logit, probit, cloglog)를 각각 사용해 모형을 적합하고, 테스트 세트에 대한 예측을 만든 뒤, 주어진 컷오프에서 예측을 적절한 그룹(부도 vs 비부도)으로 분류하고, 혼동 행렬을 만든 다음, 각 모형에 대해 정확도와 민감도를 계산해 보겠습니다! 지금까지 정말 많은 내용을 배우셨어요. 마지막으로, 해당 컷오프 값에서 정확도 기준으로 가장 성능이 좋은 모형을 찾아보세요!
일반적으로 모형 간 차이는 매우 작을 수 있으며, 결과는 선택한 컷오프 값에 따라 달라진다는 점이 중요합니다. 관측된 실제 결과(부도 vs 비부도)는 콘솔의 true_val에 저장되어 있습니다.
이 연습은 강의의 일부입니다
R로 배우는 신용 위험 모델링
연습 안내
- 링크를 각각
logit,probit,cloglog로 지정해 로지스틱 회귀 모형 세 개를 적합하세요. 일부 코드는 제공됩니다. 예측 변수로는age,emp_cat,ir_cat,loan_amnt를 사용하세요. - 모든 모형에 대해
test_set을 사용해 예측값을 만드세요. - 각 모형의 성능을 비교할 수 있도록 컷오프 값을 14%로 사용해 분류 예측을 수행하세요.
- 세 모형에 대한 혼동 행렬을 만드세요.
- 마지막으로, 세 모형 모두에 대해 분류 정확도를 계산하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Fit the logit, probit and cloglog-link logistic regression models
log_model_logit <- glm(loan_status ~ age + emp_cat + ir_cat + loan_amnt,
family = binomial(link = logit), data = training_set)
log_model_probit <-
log_model_cloglog <-
# Make predictions for all models using the test set
predictions_logit <- predict(log_model_logit, newdata = test_set, type = "response")
predictions_probit <-
predictions_cloglog <-
# Use a cut-off of 14% to make binary predictions-vectors
cutoff <- 0.14
class_pred_logit <- ifelse(predictions_logit > cutoff, 1, 0)
class_pred_probit <-
class_pred_cloglog <-
# Make a confusion matrix for the three models
tab_class_logit <- table(true_val,class_pred_logit)
tab_class_probit <-
tab_class_cloglog <-
# Compute the classification accuracy for all three models
acc_logit <- sum(diag(tab_class_logit)) / nrow(test_set)
acc_probit <-
acc_cloglog <-