在固定臨界值下比較連結函式
在這最後一題中,你會用三種連結函式(logit、probit 和 cloglog)各自訓練一個模型,對測試集做預測,根據指定的臨界值將預測分類到正確的群組(違約與非違約),建立混淆矩陣,並計算各模型在該臨界值下的準確率與靈敏度!太棒了,你已經學到很多了。最後,你也會嘗試找出在此臨界值下,哪個模型的準確率表現最佳!
請注意,各模型之間的差異通常會很小,而且結果仍會依賴你所選擇的臨界值。實際觀察到的結果(違約或非違約)已經儲存在主控台中的 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 <-