R语言 scorecard包 评分卡

我会把文章及时的更新到公共号上,欢迎大家的关注。

 

library(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")

# 绘制分箱后的坏账率可视化,对应14张图,这里只展示最后一张
woebin_plot(bins)

train_woe <-  woebin_ply(train, bins)
test_woe <-  woebin_ply(test, bins)
print(dim(train_woe))
print(dim(test_woe))


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, 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
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI周红伟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值