| Title: | Tools for Binning Data |
|---|---|
| Description: | Manually bin data using weight of evidence and information value. Includes other binning methods such as equal length, quantile and winsorized. Options for combining levels of categorical data are also available. Dummy variables can be generated based on the bins created using any of the available binning methods. References: Siddiqi, N. (2006) <doi:10.1002/9781119201731>. |
| Authors: | Aravind Hebbali [aut, cre] |
| Maintainer: | Aravind Hebbali <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.3.0 |
| Built: | 2026-11-19 13:38:48 UTC |
| Source: | https://github.com/rsquaredacademy/rbin |
The data is related with direct marketing campaigns of a Portuguese banking institution. The marketing campaigns were based on phone calls. Often, more than one contact to the same client was required, in order to access if the product (bank term deposit) would be ('yes') or not ('no') subscribed.
mbankmbank
A tibble with 4521 rows and 17 variables:
age of the client
type of job
marital status
education level of the client
has credit in default?
average yearly balance, in euros
has housing loan?
has personal loan?
contact communication type
last contact day of the month
last contact month of year
last contact duration, in seconds
number of contacts performed during this campaign and for this client
number of days that passed by after the client was last contacted from a previous campaign
number of contacts performed before this campaign and for this client
outcome of the previous marketing campaign
has the client subscribed a term deposit?
[Moro et al., 2014] S. Moro, P. Cortez and P. Rita. A Data-Driven Approach to Predict the Success of Bank Telemarketing. Decision Support Systems, Elsevier, 62:22-31, June 2014
Create dummy variables from bins.
rbin_create(data, predictor, bins)rbin_create(data, predictor, bins)
data |
A |
predictor |
Bare column name, character name, or variable holding a character
name in |
bins |
An object of class |
Missing predictor values produce missing indicators; the separate missing-value bin is not turned into a dummy column. Winsorized objects apply their fitted clamp before assigning intervals. Older saved winsorized objects without clamp metadata must be fitted again. Equal-frequency bins keep ties together and use their stored value intervals for scoring. Gaps go to the preceding group, with boundaries at the next group's minimum. Dummy names describe these scoring intervals, which extend beyond the training range.
data with one indicator column per bin, appended in bin
order. Every bin defined when the bins were created is represented, even
when the data passed here has no observation in that bin.
k <- rbin_manual(mbank, y, age, c(29, 39, 56)) rbin_create(mbank, age, k)k <- rbin_manual(mbank, y, age, c(29, 39, 56)) rbin_create(mbank, age, k)
Bin continuous data using the equal frequency binning method.
rbin_equal_freq(data = NULL, response = NULL, predictor = NULL, bins = 10) ## S3 method for class 'rbin_equal_freq' plot(x, print_plot = TRUE, ...)rbin_equal_freq(data = NULL, response = NULL, predictor = NULL, bins = 10) ## S3 method for class 'rbin_equal_freq' plot(x, print_plot = TRUE, ...)
data |
A |
response |
Bare column name, character name, or variable holding a character
name in |
predictor |
Bare column name, character name, or variable holding a character
name in |
bins |
Number of bins. |
x |
An object of class |
print_plot |
logical; if |
... |
further arguments passed to or from other methods. |
Groups have approximately equal sizes and never split tied values.
For each k = 1, ..., bins - 1, take the smallest observed value
whose cumulative count reaches k * n / bins, where n is
the number of non-missing predictors (the type-1 empirical quantile).
All observations equal to that value stay in the lower group. The next
distinct observed value is the scoring cut. Repeated cuts and cuts beyond
the maximum are omitted; a message reports the requested and actual
numbers of non-missing bins when fewer bins result. No bin is empty.
Without ties, group sizes differ by at most one observation.
Both the top-level and table lower_cut/upper_cut fields
contain the actual left-closed, right-open scoring intervals. The first
and last intervals extend to negative and positive infinity; gaps go to
the preceding group. rbin_create() reproduces the non-missing
training counts using these intervals.
Missing predictor values form a separate final group, and the equal
frequency groups are built from non-missing values only. As with other
numeric methods, missing predictors produce missing dummy indicators.
A list of class rbin_equal_freq with the following elements:
bins, a data.frame with one row per group (plus a final row
for missing values when present); method; vars;
lower_cut; and upper_cut.
bins <- rbin_equal_freq(mbank, y, age, 10) bins # plot plot(bins)bins <- rbin_equal_freq(mbank, y, age, 10) bins # plot plot(bins)
Bin continuous data using the equal length binning method.
rbin_equal_length( data = NULL, response = NULL, predictor = NULL, bins = 10, include_na = TRUE ) ## S3 method for class 'rbin_equal_length' plot(x, print_plot = TRUE, ...)rbin_equal_length( data = NULL, response = NULL, predictor = NULL, bins = 10, include_na = TRUE ) ## S3 method for class 'rbin_equal_length' plot(x, print_plot = TRUE, ...)
data |
A |
response |
Bare column name, character name, or variable holding a character
name in |
predictor |
Bare column name, character name, or variable holding a character
name in |
bins |
Number of bins. |
include_na |
logical; if |
x |
An object of class |
print_plot |
logical; if |
... |
further arguments passed to or from other methods. |
A list of class rbin_equal_length with the following elements:
bins, a data.frame with one row per interval (including
empty intervals) and a final row for missing values when present;
method; vars; lower_cut; and upper_cut.
bins <- rbin_equal_length(mbank, y, age, 10) bins # plot plot(bins)bins <- rbin_equal_length(mbank, y, age, 10) bins # plot plot(bins)
Weight of evidence and information value for categorical data.
rbin_factor(data = NULL, response = NULL, predictor = NULL, include_na = TRUE) ## S3 method for class 'rbin_factor' plot(x, print_plot = TRUE, ...)rbin_factor(data = NULL, response = NULL, predictor = NULL, include_na = TRUE) ## S3 method for class 'rbin_factor' plot(x, print_plot = TRUE, ...)
data |
A |
response |
Bare column name, character name, or variable holding a character
name in |
predictor |
Bare column name, character name, or variable holding a character
name in |
include_na |
logical; if |
x |
An object of class |
print_plot |
logical; if |
... |
further arguments passed to or from other methods. |
Both response and predictor accept bare column names,
character names, or variables holding character names. Groups and cumulative
counts follow first appearance, with missing values last. Factor levels and
the ordered class are preserved; an explicit NA level is a valid
category, distinct from a missing factor code. It precedes the final
missing-code group and is retained when include_na = FALSE.
Numeric NaN and NA share a missing-value group.
If a literal "NA" category or an explicit NA level is present,
the genuine missing group displays as "(missing)" in printed
summaries and plots. Stored
typed labels, explicit NA levels and ordered classes are preserved.
A list of class rbin_factor with the following elements:
bins, a data.frame with one row per observed level (plus a
final row for missing values when present); method; and vars.
bins <- rbin_factor(mbank, y, education) bins # plot plot(bins)bins <- rbin_factor(mbank, y, education) bins # plot plot(bins)
Manually combine levels of categorical data.
rbin_factor_combine(data, var, new_var, new_name)rbin_factor_combine(data, var, new_var, new_name)
data |
A |
var |
Bare or character name of a factor or character column in |
new_var |
A character vector containing the names of levels to combine.
An |
new_name |
Name of the combined level. |
Levels are matched exactly, not as regular expressions. Only the
selected levels are renamed; other levels are left untouched even when
their names contain the selected name as a substring.
Unselected explicit NA levels and missing codes are preserved, as is
the ordered class. A combined level takes the position of its first selected
level; a new level for missing codes alone is appended at the end.
A data.frame with the predictor's levels combined. The
predictor keeps its original position among the columns.
upper <- c("secondary", "tertiary") out <- rbin_factor_combine(mbank, education, upper, "upper") table(out$education) out <- rbin_factor_combine(mbank, education, c("secondary", "tertiary"), "upper") table(out$education)upper <- c("secondary", "tertiary") out <- rbin_factor_combine(mbank, education, upper, "upper") table(out$education) out <- rbin_factor_combine(mbank, education, c("secondary", "tertiary"), "upper") table(out$education)
Create dummy variables for categorical data.
rbin_factor_create(data, predictor)rbin_factor_create(data, predictor)
data |
A |
predictor |
Bare column name, character name, or variable holding a character
name in |
Indicator columns are derived from the factor codes, so a factor
with an explicit NA level gets a real indicator column (for example
g_NA) and not a column of missings. A missing factor code that is
not a level produces missing indicators. Generated names never overwrite
existing columns: a collision is made unique by appending a suffix.
A data.frame with the original columns preserved and one
indicator column per level, appended at the end.
upper <- c("secondary", "tertiary") out <- rbin_factor_combine(mbank, education, upper, "upper") rbin_factor_create(out, education)upper <- c("secondary", "tertiary") out <- rbin_factor_combine(mbank, education, upper, "upper") rbin_factor_create(out, education)
Bin continuous data manually.
rbin_manual( data = NULL, response = NULL, predictor = NULL, cut_points = NULL, include_na = TRUE ) ## S3 method for class 'rbin_manual' plot(x, print_plot = TRUE, ...)rbin_manual( data = NULL, response = NULL, predictor = NULL, cut_points = NULL, include_na = TRUE ) ## S3 method for class 'rbin_manual' plot(x, print_plot = TRUE, ...)
data |
A |
response |
Bare column name, character name, or variable holding a character
name in |
predictor |
Bare column name, character name, or variable holding a character
name in |
cut_points |
Cut points for binning. |
include_na |
logical; if |
x |
An object of class |
print_plot |
logical; if |
... |
further arguments passed to or from other methods. |
Cut points are open upper bounds: intervals are left closed and
right open, [lower, upper). Supply nine cut points for ten bins;
the last bin has no upper bound. For an integer predictor, a cut at 37
includes integers through 36. For continuous data, that cut includes every
value below 37, including 36.5; choose the continuous boundary directly.
A list of class rbin_manual with the following elements:
bins, a data.frame with one row per interval (including
empty intervals) and a final row for missing values when present;
method; vars; lower_cut; and upper_cut.
bins <- rbin_manual(mbank, y, age, c(29, 31, 34, 36, 39, 42, 46, 51, 56)) bins # plot plot(bins)bins <- rbin_manual(mbank, y, age, c(29, 31, 34, 36, 39, 42, 46, 51, 56)) bins # plot plot(bins)
Bin continuous data using quantiles.
rbin_quantiles( data = NULL, response = NULL, predictor = NULL, bins = 10, include_na = TRUE ) ## S3 method for class 'rbin_quantiles' plot(x, print_plot = TRUE, ...)rbin_quantiles( data = NULL, response = NULL, predictor = NULL, bins = 10, include_na = TRUE ) ## S3 method for class 'rbin_quantiles' plot(x, print_plot = TRUE, ...)
data |
A |
response |
Bare column name, character name, or variable holding a character
name in |
predictor |
Bare column name, character name, or variable holding a character
name in |
bins |
Number of bins. |
include_na |
logical; if |
x |
An object of class |
print_plot |
logical; if |
... |
further arguments passed to or from other methods. |
A list of class rbin_quantiles with the following elements:
bins, a data.frame with one row per interval (including
empty intervals) and a final row for missing values when present;
method; vars; lower_cut; and upper_cut.
bins <- rbin_quantiles(mbank, y, age, 10) bins # plot plot(bins)bins <- rbin_quantiles(mbank, y, age, 10) bins # plot plot(bins)
Bin continuous data using winsorized method.
rbin_winsorize( data = NULL, response = NULL, predictor = NULL, bins = 10, include_na = TRUE, winsor_rate = 0.05, min_val = NULL, max_val = NULL, type = 7, remove_na = TRUE ) ## S3 method for class 'rbin_winsorize' plot(x, print_plot = TRUE, ...)rbin_winsorize( data = NULL, response = NULL, predictor = NULL, bins = 10, include_na = TRUE, winsor_rate = 0.05, min_val = NULL, max_val = NULL, type = 7, remove_na = TRUE ) ## S3 method for class 'rbin_winsorize' plot(x, print_plot = TRUE, ...)
data |
A |
response |
Bare column name, character name, or variable holding a character
name in |
predictor |
Bare column name, character name, or variable holding a character
name in |
bins |
Number of bins. |
include_na |
logical; if |
winsor_rate |
A value from 0.0 to 0.5. |
min_val |
the low border, all values being lower than this will be replaced by this value. The default is set to the 5 percent quantile of predictor. |
max_val |
the high border, all values being larger than this will be replaced by this value. The default is set to the 95 percent quantile of predictor. |
type |
an integer between 1 and 9 selecting one of the nine quantile algorithms detailed in |
remove_na |
logical; if |
x |
An object of class |
print_plot |
logical; if |
... |
further arguments passed to or from other methods. |
A list of class rbin_winsorize with the following elements:
bins, a data.frame with one row per interval (including
empty intervals) and a final row for missing values when present;
method; vars; lower_cut; upper_cut; and
clamp, the fitted minimum and maximum used when scoring new data
(absent if all predictor values are missing).
bins <- rbin_winsorize(mbank, y, age, 10, winsor_rate = 0.05) bins # plot plot(bins)bins <- rbin_winsorize(mbank, y, age, 10, winsor_rate = 0.05) bins # plot plot(bins)
Manually bin continuous data using weight of evidence.
rbinAddin(data = NULL)rbinAddin(data = NULL)
data |
A |
## Not run: rbinAddin(data = mbank) ## End(Not run)## Not run: rbinAddin(data = mbank) ## End(Not run)
Manually combine categorical variables using weight of evidence.
rbinFactorAddin(data = NULL)rbinFactorAddin(data = NULL)
data |
A |
## Not run: rbinFactorAddin(data = mbank) ## End(Not run)## Not run: rbinFactorAddin(data = mbank) ## End(Not run)