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)
)
?
總結
以上是生活随笔為你收集整理的R语言 scorecard包 评分卡的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: R-基本数据类型
- 下一篇: Building credit scor