WOE_customFac {Rprofet} | R Documentation |
Custom Binning Factor Variables
Description
Function that bins a factor variable based on user inputted factor levels, plots the information on the new bins, and returns a list contains a dataframe of the newly binned values and id column and more items.
Usage
WOE_customFac(
data,
var,
id,
target,
new_levels,
color = "#0066CC",
plot = FALSE
)
Arguments
data |
Dataframe containing the target variable and desired factor variables to be binned. |
var |
A specific factor attribute to be binned. |
id |
The unique id variable in the dataframe. Must be specified. |
target |
A binary target variable. Must be specified. |
new_levels |
A vector the same length as the number of levels for the categorical variable containing the new factor levels. Must be specified. |
color |
A hexadecimal value representing a specific color. |
plot |
Logical. The default is FALSE which does not generate the plots. |
Value
A list with the following components.
NewBin |
Dataframe with the binned variable. |
BinWOE |
Dataframe with target, binned variable, and WOE values for the bins. |
IV |
Information value of the newly binned variable. |
vars |
Dataframe with binned variable, WOE values for the bins, Target Rate for each bin, and observation count for each bin. |
Examples
mydata <- ISLR::Default
mydata$ID = seq(1:nrow(mydata)) ## make the ID variable
mydata$default <- ifelse(mydata$default=="Yes", 1, 0) ## target coded with 1, 0
## WOE_customFactor
custom1 <- WOE_customFac(data=mydata, var="student", id ="ID", target="default",
new_levels=c("Student : No","Student : Yes"))
head(custom1$NewBin)
head(custom1$BinWOE)
custom1$IV
custom1$vars
## --------------------------
mydata$balance_cat <- cut(mydata$balance, breaks = c(-1,400,800,1200,1600,2000,2400,2800),
labels = c("Very-Low","Low","Med-Low","Med",
"Med-High","High","Very-High"))
custom2 <- WOE_customFac(data=mydata, var="balance_cat", id ="ID", target="default",
new_levels=c(1,1,2,2,2,3,3))
head(custom2$NewBin)
head(custom2$BinWOE)
custom2$IV
custom2$vars