【R包】scorecard:评分卡包
笔者邀请您,先思考:
1 如何设计和开发评分卡?
2 评分卡如何应用?
本文介绍评分卡包scorecard的使用。
scorecard包简介
scorecard包通过提供信息价值,变量过滤器,最佳woe分箱,记分卡缩放和模型性能评估等功能,以实现轻松高效地设计和开发信用风险记分卡。

scorecard包的函数
scorecard包提供如下函数,使得高效开发信用风险记分卡。
-
信息价值(iv)
-
变量过滤器(var_filter)
-
最佳woe分享(woebin,woebin_ply,woebin_plot,woebin_adj)
-
记分卡缩放(scorecard,scorecard_ply)
-
性能评估(perf_eva,perf_psi)
scorecard包应用举例
这是一个基本示例,向您展示如何开发通用的信用风险记分卡:
# 加载R包
if(!require(scorecard))
{
install.packages("scorecard")
require(scorecard)
}
# 数据准备
data("germancredit")
print(dim(germancredit))
print(names(germancredit))
print(head(germancredit[,20:21]))
# 变量选择
dt_s <- var_filter(germancredit, y="creditability")
print(dim(dt_s))
# 数据集划分为训练集和测试集
dt_list <- split_df(dt_s)
train <- dt_list$train
test <- dt_list$test
# woe 分箱 自动分箱
bins <- woebin(dt_s, y="creditability")
# 绘制分箱后的坏账率可视化
woebin_plot(bins)
# 手动分箱的实例
# 变量credit.amount为例
woebin_adj(dt_s, "creditability", bins$credit.amount)
# 变量other.debtors.or.guarantors为例
woebin_adj(dt_s, "creditability", bins$other.debtors.or.guarantors)
# 反复观察和调整,然后确定最佳分箱阈值
print(unique(germancredit$other.debtors.or.guarantors))
# 分箱调整
breaks_adj <- list(
age.in.years=c(26, 35, 40),
other.debtors.or.guarantors=c("none", "co-applicant%,%guarantor"))
bins_adj <- woebin(dt_s, y="creditability", breaks_list=breaks_adj)
woebin_adj(dt_s, "creditability", bins$other.debtors.or.guarantors)
woebin_adj(dt_s, "creditability", bins_adj$other.debtors.or.guarantors)
# 训练集和测试数据集转换为woe值
train_woe <- woebin_ply(train, bins_adj)
test_woe <- woebin_ply(test, bins_adj)
print(dim(train_woe))
print(dim(test_woe))
# glm模型
m1 <- glm( creditability ~ ., family = binomial(), data = train_woe)
summary(m1)
# 逐步回归选择变量
m_step <- step(m1, direction="both", trace = FALSE)
m2 <- eval(m_step$call)
summary(m2)
# 模型性能验证 ks和roc
# 预测的概率
train_pred <- predict(m2, train_woe, type = 'response')
test_pred <- predict(m2, test_woe, type = 'response')
# 性能
train_perf <- perf_eva(train$creditability, train_pred, title = 'train')
test_perf <- perf_eva(test$creditability, test_pred, title = 'test')
# 生成评分卡
card <- scorecard(bins_adj, m2)
card
# 信用评分
# 实际评分
train_score <- scorecard_ply(train, card, print_step = 0)
# 验证集评分
test_score <- scorecard_ply(test, card, print_step = 0)
print(train_score)
print(test_score)
# 模型的稳定性度量
# psi
psi_result <- perf_psi(
score = list(train = train_score, test = test_score),
label = list(train = train$creditability, test = test$creditability)
)
结论:
1 模型性能评价
1.1 训练数据集

1.2 测试数据集

2 模型的稳定性分析

完整代码也可以参见GitHub。
参考资料:
1https://www.rdocumentation.org/packages/scorecard/versions/0.1.8
2 Siddiqi, N. Intelligent Credit Scoring Building and Implementing Better Credit Risk Scorecards
您在阅读中,有什么见解,请留言。
版权声明:作者保留权利,严禁修改,转载请注明原文链接。
数据人网是数据人学习、交流和分享的平台http://shujuren.org 。专注于从数据中学习到有用知识。
平台的理念:人人投稿,知识共享;人人分析,洞见驱动;智慧聚合,普惠人人。
您在数据人网平台,可以1)学习数据知识;2)创建数据博客;3)认识数据朋友;4)寻找数据工作;5)找到其它与数据相关的干货。
我们努力坚持做原创,聚合和分享优质的省时的数据知识!
我们都是数据人,数据是有价值的,坚定不移地实现从数据到商业价值的转换!
加入数据人圈子或者商务合作,请添加笔者微信。

点击阅读原文,进入数据人网,获取数据知识。
公众号推荐:
链达君,专注于分享区块链内容。

脚印英语,专注于分享英语口语内容。

数据工作
我们公司大数据风控中心部门诚招:
1 风险分析和建模工程师(信用评分模型,反欺诈模型,额度模型|熟练常用ML算法|熟练使用R或者Python)
2 数据开发工程师(大数据平台开发,数据采集,数据预处理)
3 爬虫工程师(掌握爬虫技术,掌握反爬策略)
4 数据分析或者建模实习生(研究生学历|统计学,数学,计算机专业|熟悉R或者Python|有金融数据项目可以加分)
工作地点:深圳大冲商务中心
薪资待遇:面议
感兴趣者,请加小编微信。
请关注“恒诺新知”微信公众号,感谢“R语言“,”数据那些事儿“,”老俊俊的生信笔记“,”冷🈚️思“,“珞珈R”,“生信星球”的支持!