#' Extract a matrix with IV, WOE, structure of bads and structure of goods for each band.
#'
#' @param yVar A logical vector of dependent variable.
#' @param xVar A factor or logical vector of independent variable.
#' @return A matrix with information value, WOE, structure of bads and structure of goods for each band.
#' @examples
#' data(lendclub)
#' getIV(yVar = lendclub$loan_status, lendclub$home_ownership)
#' #or check IV for variable
#' x <- getIV(yVar = lendclub$loan_status, lendclub$home_ownership)
#' sum(x[,"iv"])
#' @export
getIV <- function (yVar, xVar)
{
# checking ------------------------------------------------------------------
if(any(length(c(xVar,yVar)) == 0))
stop("NULL xVar o yVar")
if(!is.logical(yVar))
stop("yVar must be logical")
if(!(is.factor(xVar) | is.logical(xVar)))
stop("xVar must be factor or logical")
if(length(unique(yVar)) != 2)
stop("yVar is constant")
# amount for each band ------------------------------------------------------
tBad <- tapply(yVar, xVar, sum) #?li
tGood <- tapply(!yVar, xVar, sum) #dobrzy
#erase when number levels of factor are higher than unique values in xVar
tGood <- tGood[!is.na(tGood)]
tBad <- tBad[!is.na(tBad)]
# structure for each band ---------------------------------------------------
sBad <- tBad / sum(tBad) #struktura z?ych
sGood <- tGood / sum(tGood) #struktura dobrych
# Weight of Evidence --------------------------------------------------------
woe <- ifelse(!is.finite(sBad) | sBad == 0, 0, log(sGood/sBad))
# Information Value ---------------------------------------------------------
iv <- (sGood - sBad) * woe
#return
cbind(iv, woe, sGood, sBad)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.