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.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

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