R Package 'smbinning' V0.3: Optimal Binning for Scoring Modeling

R Package 'smbinning' V0.3: Optimal Binning for Scoring Modeling

标签(空格分隔): RPackage


Reference

Scoring Modeling - Data, Analysis, and Scoring Modeling


Description

The package smbinning categorizes a numeric variable into bins or bands mapped to a binary target variable for its ulterior usage in scoring modeling. Its purpose is to automate the time consuming process of selecting the right cut points, quickly calculate metrics such as Weight of Evidence and Information Value (IV); and also document SQL codes, tables, and plots used throughout the development stage.

In its new Version 0.3, the package allows the user in one step (smbinning.eda) to see missing values and outliers for each characteristic in the dataset, along with basic statistics to better understand their distribution, and also in one step obtain the Information Value for each characteristic (smbinning.sumiv).

The desired outputs are basically the tables showed in the examples below, whose theory can be found in the following books:

  • "Credit Scoring, Response Modeling, and Insurance Rating" (Finlay, 2010). See it [Here]
  • "The Credit Scoring Toolkit" (Anderson, 2007). See it [Here]
  • "Credit Risk Scorecards" (Siddiqi, 2006). See it [Here]

More details on:

  • CRAN Repository [Here]
  • Documentation (pdf) [Here]
  • Video (v0.1) [Here]

What's New on Version 0.3?

  • New function that calculates IV for all variables in one step.
  • New function that plots and ranks IVs for each variable.
  • New function for exploratory data analysis.
  • New function that produces SQL code after binning factors.
  • New function that generate variables after binning factors.
  • Variables generated after binning now are factors with labels, instead of character.
  • Modified dataset that allows a better understanding of the new functionalities.
# ----------------------------------------------------
# Package : Optimal Binning for Scoring Modeling V0.3
# Author  : Herman Jopia
# Website : http://www.scoringmodeling.com
# Twitter : @hjopia
# ----------------------------------------------------

# Load package and its data
library(smbinning) 
data(chileancredit)
str(chileancredit) # Quick description of the data
table(chileancredit$FlagGB) # Tabulate target variable
table(chileancredit$FlagSample) # 2 random samples (1:75%, 0:25%) 

# Training and testing samples
chileancredit.train=subset(chileancredit,FlagSample==1)
chileancredit.test=subset(chileancredit,FlagSample==0)

# Optimal Binning ----------------------------------------------------------
result=
  smbinning(df=chileancredit.train,y="FlagGB",x="TOB",p=0.05) # Run and save
result$ivtable # Tabulation and Information Value
result$iv # Information value
result$bands # Bins or bands
result$ctree # Decision tree from partykit

# Relevant plots (2x2 Page)
par(mfrow=c(2,2))
boxplot(chileancredit.train$TOB~chileancredit.train$FlagGB,
        horizontal=T, frame=F, col="lightgray",main="Distribution")
mtext("Time on Books (Months)",3)
smbinning.plot(result,option="dist",sub="Time on Books (Months)")
smbinning.plot(result,option="badrate",sub="Time on Books (Months)")
smbinning.plot(result,option="WoE",sub="Time on Books (Months)")

# SQL Code after binning a numeric variable ---------------------------------
smbinning.sql(result)

# Generate variable after binning -------------------------------------------
chileancredit=smbinning.gen(chileancredit, result, chrname = "gTOB")

# Customized Binning --------------------------------------------------------
# Remove exclusions from chileancredit dataset
TOB.train=
  subset(chileancredit,(FlagSample==1 & (FlagGB==1 | FlagGB==0)), select=TOB)
# Percentiles of 20%
TOB.Pct20=quantile(TOB.train, probs=seq(0,1,0.2), na.rm=T)
TOB.Pct20.Breaks=as.vector(quantile(TOB.train, probs=seq(0,1,0.2), na.rm=T))
Cuts.TOB.Pct20=TOB.Pct20.Breaks[2:(length(TOB.Pct20.Breaks)-1)]
# Package application and results
result=
  smbinning.custom(df=chileancredit.train,
                   y="FlagGB",x="TOB",cuts=Cuts.TOB.Pct20) # Run and save
result$ivtable # Tabulation and Information Value

# Factor Variable Application -----------------------------------------------
result=
  smbinning.factor(df=chileancredit.train,y="FlagGB",x="IncomeLevel")
result$ivtable

# SQL Code after binning a factor variable ----------------------------------
smbinning.sql(result)

# Generate variable after binning factor ------------------------------------
chileancredit=smbinning.factor.gen(chileancredit, result, chrname = "gInc")

# Exploratory Data Analysis -------------------------------------------------
smbinning.eda(df=chileancredit.train)$eda # Table with basic statistics
smbinning.eda(df=chileancredit.train)$edapct # Table with basic percentages

# Information Value for all variables in one step ---------------------------
smbinning.sumiv(df=chileancredit.train,y="FlagGB") # IV for eache variable

# Plot IV for all variables -------------------------------------------------
sumivt=smbinning.sumiv(chileancredit.train,y="FlagGB")
sumivt # Display table with IV by characteristic
par(mfrow=c(1,1))
smbinning.sumiv.plot(sumivt,cex=1) # Plot IV summary table
Table 1
Table 1

Table 1. Time on Books and Credit Performance via Optimal Binning. Plots from this output are shown in Figure 1 (Below).

Table 2
Table 2

Table 2. Time on Books and Credit Performance utilizing customized cutpoints.

Table 3
Table 3

Table 3. Income Levels and Credit Performance. The package allows users to take advantage of its functionalities to analyze factor variables.

Figure 1
Figure 1

Figure 1. Time on Books and Credit Performance plots after Optimal Binning (Table 1).

Table 4
Table 4

Table 4. Basic statistics to better understand each variable.

Table 5
Table 5

Table 5. Basic percentages that gives a different perspective of the numbers.

Table 6
Table 6

Table 6. IV for each characteristic of the dataset.

Figure 2
Figure 2

Figure 2. Plot the IV for each characteristic of the dataset.

Package History

  • 2016-06-21: Version 0.3 available on CRAN (Happy!)
  • 2015-06-15: Version 0.2 available on CRAN (Awesome!)
  • 2015-03-24: Package featured on RevolutionAnalytics [Here]
  • 2015-02-22: Package featured on Data Science Central [Here]
  • 2015-02-16: Version 0.1 available as Binary Package, meaning, install.packages("smbinning") can be used to install it.
  • 2015-02-15: Version 0.1 available on CRAN as a Source Package (Great!)
  • 2014-10-19: Inception while writing about binning using recursive partitioning.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,383评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,522评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,852评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,621评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,741评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,929评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,076评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,803评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,265评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,582评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,716评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,395评论 4 333
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,039评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,798评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,027评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,488评论 2 361
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,612评论 2 350

推荐阅读更多精彩内容

  • 今天夜班,女儿班任微信我补交书费,并问了孩子的情况,问能不能上学。我把这消息告诉女儿。她正和爸爸在散步,爸...
    持续一致的小东风阅读 133评论 2 5
  • 文/何求美人折 一山复向一山行,竹林幽闭石罄声。兰溪落泥滚童子,茅草清酒一老翁。 云瑾手里拿着一筒竹简,竹简是在死...
    何求美人折阅读 556评论 2 3
  • 今天爸爸给我买了平衡车,可好玩了! 我问店里的叔叔,这个怎么玩?叔叔告诉我,先站在车后边,左脚轻...
    小种子吴子豪阅读 312评论 0 1
  • 元稹 秋丛绕舍似陶家,遍绕篱边日渐斜。不是花中偏爱菊,此花开尽更无花。
    Lin_Lian阅读 295评论 0 0