R/credit.scorecard-package.R

Defines functions dt_prepare create_scorecard dt_score

Documented in create_scorecard dt_prepare dt_score

#' credit.scorecard.
#'
#' @name credit.scorecard
#' @docType package

dt_prepare = function(dataset, y, positive_y = "bad|1")
{
  # filter variable via missing rate, iv, identical value rate
  nm = names(dataset)
  if(!(y %in% nm)) stop("y as target variable must be in the data.")
  nm = ifelse(nm != y, nm, 'y__')
  names(dataset) = nm
  dt_sel = var_filter(dataset, 'y__', positive = positive_y)

  # woe binning ------
  bins = woebin(dt_sel, 'y__')
  dt_woe = woebin_ply(dt_sel, bins)
  list(dt_woe = dt_woe, bins = bins)
}

create_scorecard = function(dt, points0 = 600, Odds = 1/19, pdo = 50, use_intercept = TRUE)
{
  dt_woe = dt$dt_woe
  bins = dt$bins

  # glm ------
  m = glm(y__ ~ ., family = binomial(), data = dt_woe)

  # Select a formula-based model by AIC
  m_step = step(m, direction = "both", trace = FALSE)
  m = eval(m_step$call)
  # summary(m)

  # predicted proability
  dt_pred = predict(m, type = 'response', dt_woe)

  # performace
  # ks & roc plot
  perf_eva(dt_woe$y__, dt_pred)

  # scorecard
  # Example I # creat a scorecard
  card = scorecard(bins = bins, model = m, points0 = points0, odds0 = Odds, pdo = pdo, basepoints_eq0 = !use_intercept)

  # credit score
  # Example I # only total score
  # score1 = scorecard_ply(dt, card)
  return(card)
}

dt_score = function(score)
{
  x = NULL
  for(i in 2:length(score))
  {
    x = rbind(x, score[[i]])
  }
  return(x)
}
aephidayatuloh/credit.scorecard documentation built on May 27, 2019, 11:54 a.m.