[关闭]
@lumincinta 2017-02-07T12:31:13.000000Z 字数 5523 阅读 283

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

R.Package


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:

More details on:

What's New on Version 0.3?

  1. # ----------------------------------------------------
  2. # Package : Optimal Binning for Scoring Modeling V0.3
  3. # Author : Herman Jopia
  4. # Website : http://www.scoringmodeling.com
  5. # Twitter : @hjopia
  6. # ----------------------------------------------------
  7. # Load package and its data
  8. library(smbinning)
  9. data(chileancredit)
  10. str(chileancredit) # Quick description of the data
  11. table(chileancredit$FlagGB) # Tabulate target variable
  12. table(chileancredit$FlagSample) # 2 random samples (1:75%, 0:25%)
  13. # Training and testing samples
  14. chileancredit.train=subset(chileancredit,FlagSample==1)
  15. chileancredit.test=subset(chileancredit,FlagSample==0)
  16. # Optimal Binning ----------------------------------------------------------
  17. result=
  18. smbinning(df=chileancredit.train,y="FlagGB",x="TOB",p=0.05) # Run and save
  19. result$ivtable # Tabulation and Information Value
  20. result$iv # Information value
  21. result$bands # Bins or bands
  22. result$ctree # Decision tree from partykit
  23. # Relevant plots (2x2 Page)
  24. par(mfrow=c(2,2))
  25. boxplot(chileancredit.train$TOB~chileancredit.train$FlagGB,
  26. horizontal=T, frame=F, col="lightgray",main="Distribution")
  27. mtext("Time on Books (Months)",3)
  28. smbinning.plot(result,option="dist",sub="Time on Books (Months)")
  29. smbinning.plot(result,option="badrate",sub="Time on Books (Months)")
  30. smbinning.plot(result,option="WoE",sub="Time on Books (Months)")
  31. # SQL Code after binning a numeric variable ---------------------------------
  32. smbinning.sql(result)
  33. # Generate variable after binning -------------------------------------------
  34. chileancredit=smbinning.gen(chileancredit, result, chrname = "gTOB")
  35. # Customized Binning --------------------------------------------------------
  36. # Remove exclusions from chileancredit dataset
  37. TOB.train=
  38. subset(chileancredit,(FlagSample==1 & (FlagGB==1 | FlagGB==0)), select=TOB)
  39. # Percentiles of 20%
  40. TOB.Pct20=quantile(TOB.train, probs=seq(0,1,0.2), na.rm=T)
  41. TOB.Pct20.Breaks=as.vector(quantile(TOB.train, probs=seq(0,1,0.2), na.rm=T))
  42. Cuts.TOB.Pct20=TOB.Pct20.Breaks[2:(length(TOB.Pct20.Breaks)-1)]
  43. # Package application and results
  44. result=
  45. smbinning.custom(df=chileancredit.train,
  46. y="FlagGB",x="TOB",cuts=Cuts.TOB.Pct20) # Run and save
  47. result$ivtable # Tabulation and Information Value
  48. # Factor Variable Application -----------------------------------------------
  49. result=
  50. smbinning.factor(df=chileancredit.train,y="FlagGB",x="IncomeLevel")
  51. result$ivtable
  52. # SQL Code after binning a factor variable ----------------------------------
  53. smbinning.sql(result)
  54. # Generate variable after binning factor ------------------------------------
  55. chileancredit=smbinning.factor.gen(chileancredit, result, chrname = "gInc")
  56. # Exploratory Data Analysis -------------------------------------------------
  57. smbinning.eda(df=chileancredit.train)$eda # Table with basic statistics
  58. smbinning.eda(df=chileancredit.train)$edapct # Table with basic percentages
  59. # Information Value for all variables in one step ---------------------------
  60. smbinning.sumiv(df=chileancredit.train,y="FlagGB") # IV for eache variable
  61. # Plot IV for all variables -------------------------------------------------
  62. sumivt=smbinning.sumiv(chileancredit.train,y="FlagGB")
  63. sumivt # Display table with IV by characteristic
  64. par(mfrow=c(1,1))
  65. smbinning.sumiv.plot(sumivt,cex=1) # Plot IV summary table

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. Time on Books and Credit Performance utilizing customized cutpoints.

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. Time on Books and Credit Performance plots after Optimal Binning (Table 1).

Table 4

Table 4. Basic statistics to better understand each variable.

Table 5

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

Table 6

Table 6. IV for each characteristic of the dataset.

Figure 2

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

Package History

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注