R/scorecard.r

#' Application ScoreCard for Credit Risk Model
#'
#' This Function will create a application scorecard from the predicted probability of the logistic regression model.
#' Suppose we have to Build an application scorecard with the good to bad odds of 10 to 1 at a score of 400 doubling every 20 points.
#' So here Odd will be 10, pdo will be 20 and score will be 400.
#' @param Applicant this the unique applicant id for which we are require to create a score card
#' @param PD this is the predicted propability which is generated by using predict() function on logistic model.
#' @param True_Label this will the actual label for which prediction is generated either from train or test data sets.
#' @param score this is the maximum score that is given to the applicant who is very active in paying the debts owe to him.
#' @param pdo this is the point to double the odds
#' @param Odd this the good to bad odd
#' @return the function will return a data frame with the scores along with all the above parameters
#' @export


df.score <-
  function(Applicant = NULL,
           PD = NULL,
           True_Label = NULL,
           score = NULL,
           pdo = NULL,
           Odd = NULL) {
    if (is.null(Applicant)) {
      stop("Error: No Applicant ID provided")
    }
    if (is.null(PD)) {
      stop("ERROR: No predicted probability Provided")
    }
    if (is.null(True_Label)) {
      stop("ERROR: No True_Label Provided")
    }
    if (is.null(score)) {
      stop("ERROR: No max score range Provided")
    }
    if (is.null(pdo)) {
      stop("ERROR: No point to double Provided")
    }
    if (is.null(Odd)) {
      stop("ERROR: No good to bad odds Provided")
    }
    if (is.double(PD) == FALSE) {
      stop("ERROR: predicted probability must be in percentage of decimal")
    }
    if (is.factor(True_Label) == FALSE) {
      stop("ERROR: True_Label must be of factor class")
    }
    if (is.numeric(score) == FALSE |
        is.numeric(pdo) == FALSE | is.numeric(Odd) == FALSE) {
      stop("ERROR: score, pdo, Odd must be of integer type")
    }
    good <- 1 - PD
    oddgood <- good / PD
    odd <- log(oddgood)
    fac <- (pdo / log(2))
    offset <- score - (fac * Odd)
    y <- offset + (fac * odd)
    x <-
      as.data.frame(cbind.data.frame(Applicant, True_Label, PD, good, oddgood, odd, y))
    colnames(x) <-
      c("Applicant",
        "True_Label",
        "PD",
        "P_good",
        "Odds_good",
        "ln_Odds",
        "Scores")
    z <- x[order(-x[, 5]), ]
    z[, 7] <- ifelse(z[, 7] > score, score, z[, 7])
    return(z)
  }
prk327/AtConP documentation built on May 26, 2019, 8:33 a.m.