开始使用免费开始使用

指定阈值

我们已经向您展示了,如何通过设定阈值来获得更理想的混淆矩阵。现在,您将学习如何把预测向量转换为由二元值组成的向量,用来指示贷款状态。R 中的 ifelse() 函数可以帮助您完成这个转换。

在阈值的语境下使用 ifelse(),您可以这样写:

ifelse(predictions > 0.3, 1, 0)

在第一个参数中,您在判断预测向量中的某个值是否大于 0.3。若为 TRUE,R 返回"1"(由第二个参数指定);若为 FALSE,R 返回"0"(由第三个参数指定)。它们分别表示"违约"和"未违约"。

本练习是课程的一部分

R 中的信用风险建模

查看课程

练习说明

  • 控制台中给出了完整逻辑回归模型及其预测向量的代码。
  • 使用 0.15 作为阈值,基于 ifelse() 函数和 predictions_all_full 创建向量 pred_cutoff_15
  • 使用 table() 查看混淆矩阵(将真实值 test_set$loan_status 放在第一个参数中)。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# The code for the logistic regression model and the predictions is given below
log_model_full <- glm(loan_status ~ ., family = "binomial", data = training_set)
predictions_all_full <- predict(log_model_full, newdata = test_set, type = "response")

# Make a binary predictions-vector using a cut-off of 15%


# Construct a confusion matrix
编辑并运行代码