# R/bad_bin.R In mob: Monotonic Optimal Binning

```#' Monotonic binning by quantile with cases Y = 1
#'
#' The function \code{bad_bin} implements the quantile-based monotonic binning
#' by the iterative discretization based on cases with Y = 1.
#'
#' @param x A numeric vector
#' @param y A numeric vector with 0/1 binary values
#'
#' @return A list of binning outcomes, including a numeric vector with cut
#'         points and a dataframe with binning summary
#'
#' @examples
#' data(hmeq)

x_ <- x[!is.na(x)]
y_ <- y[!is.na(x)]
n_ <- 2:max(2, min(50, length(unique(x_[y_ == 1])) - 1))
p_ <- unique(lapply(n_, function(n) qcut(x_[y_ == 1], n)))

l1 <- lapply(p_, function(p) list(cut = p, out = manual_bin(x_, y_, p)))

l2 <- lapply(l1[order(Reduce(c, lapply(l1, function(l) -length(l\$cut))))],
function(l) list(cut  = l\$cut,
scor = round(cor(l\$out\$bin, l\$out\$bads / l\$out\$freq, method = "spearman"), 8)))

l3 <- l2[Reduce(c, lapply(l2, function(l) abs(l\$scor) == 1 & l\$minr > 0 & l\$maxr < 1))][[1]]

l4 <- l1[Reduce(c, lapply(l1, function(l) identical(l\$cut, l3\$cut)))][[1]]\$out

return(list(cut = l3\$cut, tbl = gen_woe(add_miss(l4, x, y), l3\$cut)))
}
```

## Try the mob package in your browser

Any scripts or data that you put into this service are public.

mob documentation built on July 31, 2021, 9:06 a.m.