Hands-On Introduction to creditR: An Amazing R Package to Enhance Credit Risk Scoring and Validation (2024)

Introduction

Machine learning is disrupting multiple and diverse industries right now. One of the biggest industries to be impacted – finance.

Functions like fraud detection, customer segmentation, employee or client retention are primary machine learning targets. The one we are going to focus on in this article is called credit risk scoring.

Credit scoring is a statistical analysis performed by lenders and financial institutions to access a person’screditworthiness. Lenders use credit scoring, among other things, to decide on whether to extend or deny credit. – Investopedia

Machine learning algorithms are often developed as challenger models because this is a field where regulatory requirements need to be met. This got me thinking – how can I make things easier for professionals working in the field?

Hands-On Introduction to creditR: An Amazing R Package to Enhance Credit Risk Scoring and Validation (1)

Out of that came the creditR package! It allows you to easily create your base models for credit risk scoring before machine learning applications. Additionally, the package also contains some functions that can be used to validate these processes.

The package aims to facilitate the applications of the methods of variable analysis, variable selection, model development, model calibration, rating scale development and model validation. Through the functions defined, these methodologies can be applied quickly on all modeling data or a specific variable.

In this article, we will first understand the nuts and bolts of the creditR package. We’ll then get our hands dirty in R by deep diving into a comprehensive example using creditR.

The package was issued for the use of credit risk professionals. Basic level knowledge about credit risk scoring methodologies is required for use of the package.

Table of Contents

  1. Why should you use creditR?
  2. Getting Started with creditR
  3. A List of Functions inside creditR
  4. An Application of the creditR Package

Why should you use creditR?

Perceptions of credit risk modeling are rapidly transforming as the demand for machine learning models in the field increases. However, many regulators are still very cautious about transitioning into machine learning techniques. Therefore, a possible speculation might be that during this transformation phase, machine learning algorithms will proceed along with the traditional methods.

Trust may be achieved on the part of the regulators once it is established that machine learning algorithms, while challenging the conventions of the field, are also producing more robust results than the traditional methods. Moreover, the new methods of interpreting machine learning algorithms may help to create a more transparent process.

The creditR package offers both possibilities for automating the use of traditional methods and also for the validation of traditional and machine learning models.

Getting Started with creditR

In order to install the creditR package, you should have the devtools package installed. The devtools package can be installed by running the following code:

install.packages("devtools", dependencies = TRUE)

The creditR package can be installed using the “install_github” function found in the devtools package:

library(devtools)devtools::install_github("ayhandis/creditR")library(creditR)

A List of Functions inside creditR

The functions available under the package are listed below.

ls("package:creditR")

Output:

Hands-On Introduction to creditR: An Amazing R Package to Enhance Credit Risk Scoring and Validation (2)

An Application of the creditR Package

We’ve aprsed through the theory aspect. Now let’s get our hands dirty in R!

An example application of creditR is shared below in a study of how some common steps in credit risk scoring are carried out using the functions provided in the package.

Real-world practices were taken into consideration in the preparation of this example.

The general application is structured under two main headings as modeling and model validation, and the details as to what the corresponding code does can be seen in the comment lines.

Only important outputs have been shared in this article.

This R script is designed to make the creditR package easier to understand. Obtaining a high accuracy model is not within the scope of this study.

# Attaching the librarylibrary(creditR)#Model data and data structuredata("germancredit")str(germancredit)#Preparing a sample data setsample_data <- germancredit[,c("duration.in.month","credit.amount","installment.rate.in.percentage.of.disposable.income", "age.in.years","creditability")]#Converting the ‘Creditability’ (default flag) variable into numeric typesample_data$creditability <- ifelse(sample_data$creditability == "bad",1,0)#Calculating the missing ratiosmissing_ratio(sample_data)

Output:

Hands-On Introduction to creditR: An Amazing R Package to Enhance Credit Risk Scoring and Validation (3)

#Splitting the data into train and test setstraintest <- train_test_split(sample_data,123,0.70)train <- traintest$traintest <- traintest$test

WOE transformation is a method which transforms the variable into a categorical variable through its relationship with the target variable. The following “woerules” object contains the WOE rules.

With the help of the woe.binning.deploy function, the rules were run on the data set. The variables we needed are assigned to the “train_woe” object with the help of the “woe.get.clear.data” function.

#Applying WOE transformation on the variableswoerules <- woe.binning(df = train,target.var = "creditability",pred.var = train,event.class = 1)train_woe <- woe.binning.deploy(train, woerules, add.woe.or.dum.var='woe')#Creating a dataset with the transformed variables and default flagtrain_woe <- woe.get.clear.data(train_woe,default_flag = "creditability",prefix = "woe")#Applying the WOE rules used on the train data to the test datatest_woe <- woe.binning.deploy(test, woerules, add.woe.or.dum.var='woe')test_woe <- woe.get.clear.data(test_woe,default_flag = "creditability",prefix = "woe")

Information value and univariate gini can be used as variable selection methods. Generally, a threshold value of 0.30 is used for IV and 0.10 is used for univariate Gini.

#Performing the IV and Gini calculations for the whole data setIV.calc.data(train_woe,"creditability")

Output:

Hands-On Introduction to creditR: An Amazing R Package to Enhance Credit Risk Scoring and Validation (4)

Gini.univariate.data(train_woe,"creditability")

Output:

Hands-On Introduction to creditR: An Amazing R Package to Enhance Credit Risk Scoring and Validation (5)

#Creating a new dataset by Gini elimination. IV elimination is also possibleeliminated_data <- Gini_elimination(train_woe,"creditability",0.10)str(eliminated_data)

Output:

Hands-On Introduction to creditR: An Amazing R Package to Enhance Credit Risk Scoring and Validation (6)

There are too many variables in real life to manage with correlation matrices. Hence, clustering is performed to determine variables with similar characteristics. This particular example of clustering does not make sense because of the small number of variables, but the method in general can be very useful in data sets with a large amounts of variables.

#A demonstration of the functions useful in performing Clusteringclustering_data <- variable.clustering(eliminated_data,"creditability", 2)clustering_data

Output:

Hands-On Introduction to creditR: An Amazing R Package to Enhance Credit Risk Scoring and Validation (7)

# Returns the data for variables that have the maximum gini value in the datasetselected_data <- variable.clustering.gini(eliminated_data,"creditability", 2)

In some cases, average correlations of clusters are important because the number of clusters may not be set correctly. Therefore, if the cluster has a high average correlation, it should be examined in detail. The correlation value, which is only one variable in cluster 1, is NaN.

correlation.cluster(eliminated_data,clustering_data,variables = "variable",clusters = "Group")

Output:

Hands-On Introduction to creditR: An Amazing R Package to Enhance Credit Risk Scoring and Validation (8)

A model was formed with the variables included in the data set. When the variables are examined by the model summary, it seems that the variables are meaningful. Then, with the help of the “woe.glm.feature.importance” function, the weights of the variables are calculated. In fact, weights are calculated on the basis of the effect of a single unit change on the probability.

#Creating a logistic regression model of the datamodel= glm(formula = creditability ~ ., family = binomial(link = "logit"), data = eliminated_data)summary(model)

Output:

Hands-On Introduction to creditR: An Amazing R Package to Enhance Credit Risk Scoring and Validation (9)

#Calculating variable weightswoe.glm.feature.importance(eliminated_data,model,"creditability")

Output:

Hands-On Introduction to creditR: An Amazing R Package to Enhance Credit Risk Scoring and Validation (10)

#Generating the PD values for the train and test datams_train_data <- cbind(eliminated_data,model$fitted.values)ms_test_data <- cbind(test_woe[,colnames(eliminated_data)], predict(model,type = "response", newdata = test_woe))colnames(ms_train_data) <- c("woe.duration.in.month.binned","woe.age.in.years.binned","woe.installment.rate.in.percentage.of.disposable.income.binned","creditability","PD")colnames(ms_test_data) <- c("woe.duration.in.month.binned","woe.age.in.years.binned","woe.installment.rate.in.percentage.of.disposable.income.binned","creditability","PD")

In real life, institutions use rating scales instead of continuous PD values. Due to some regulatory issues or to adapt to changing market/portfolio conditions, the models are calibrated to different central tendencies.

Regression and Bayesian calibration methods are included in the package. The numerical function that can perform calibration by embedding in the enterprise system can be obtained as output with the help of the “calibration object$calibration_formula” code.

#An example application of the Regression calibration method. The model is calibrated to the test_woe dataregression_calibration <- regression.calibration(model,test_woe,"creditability")regression_calibration$calibration_dataregression_calibration$calibration_modelregression_calibration$calibration_formula

Output:

Hands-On Introduction to creditR: An Amazing R Package to Enhance Credit Risk Scoring and Validation (11)

The Bayesian calibration method is applied over the rating scale. We can easily create the rating scale with the help of the “master.scale” function. However, in real life, rating scales can only be created after many trials.

The summary is added to the output. Details can be seen by running the R script. In addition, the example is only aimed to introduce the function within the scope of this study, hence, the PD values do not increase monotonically.

#Creating a master scalemaster_scale <- master.scale(ms_train_data,"creditability","PD")master_scale

Output:

Hands-On Introduction to creditR: An Amazing R Package to Enhance Credit Risk Scoring and Validation (12)

In order to apply Bayesian calibration, the score variable is created in the data set. Then the rating scale is calibrated to 5% central tendency.

#Calibrating the master scale and the modeling data to the default rate of 5% using the bayesian calibration methodms_train_data$Score = log(ms_train_data$PD/(1-ms_train_data$PD)) ms_test_data$Score = log(ms_test_data$PD/(1-ms_test_data$PD)) bayesian_method <- bayesian.calibration(data = master_scale,average_score ="Score",total_observations = "Total.Observations",PD = "PD",central_tendency = 0.05,calibration_data = ms_train_data,calibration_data_score ="Score")#After calibration, the information and data related to the calibration process can be obtained as followsbayesian_method$Calibration.modelbayesian_method$Calibration.formula

Output:

Hands-On Introduction to creditR: An Amazing R Package to Enhance Credit Risk Scoring and Validation (13)

In real life applications, it is difficult to understand the concept of probability for employees who are not familiar with risk management. Therefore, there is a need to create a scaled score. This can be done simply by using the “scaled.score” function.

#The Scaled score can be created using the following functionscaled.score(bayesian_method$calibration_data, "calibrated_pd", 3000, 15)

After the modeling phase, the model validation is performed to validate different expectations such as the accuracy and stability of the model. In real life, a qualitative validation process is also applied.

Note: Model calibration is performed for illustration only. Model validation tests proceed through the original master scale as follows.

In the models created by logistic regression, the problem of multicollinearity should be taken into consideration. Although different threshold values are used, vif values greater than 5 indicate this problem.

#Calculating the Vif values of the variables.vif.calc(model)

Output:

Hands-On Introduction to creditR: An Amazing R Package to Enhance Credit Risk Scoring and Validation (14)

Generally, acceptable lower limit is 0.40 for Gini coefficient. However, this may vary according to model types.

#Calculating the Gini for the modelGini(model$fitted.values,ms_train_data$creditability)

Output:

0.3577422

#Performing the 5 Fold cross validationk.fold.cross.validation.glm(ms_train_data,"creditability",5,1)

Output:

Hands-On Introduction to creditR: An Amazing R Package to Enhance Credit Risk Scoring and Validation (15)

#The KS test is performed on the distributions of the estimates for good and bad observationsKolmogorov.Smirnov(ms_train_data,"creditability","PD")Kolmogorov.Smirnov(ms_test_data,"creditability","PD")

The scorecards generally are revised in a long-term basis because the process creates an important operational cost. Therefore, the stability of the models reduces the need to revise. In addition, institutions want models that are stable since these models are used as input of many calculations like impairment, capital, risk weighted asset etc.

System Stability Index is a test used to measure the model and variable stability. SSI values above 0.25 indicate that variable stability is impaired.

#Variable stabilities are measuredSSI.calc.data(train_woe,test_woe,"creditability")

Output:

Hands-On Introduction to creditR: An Amazing R Package to Enhance Credit Risk Scoring and Validation (16)

The HHI test measures the concentration of the master scale since the main purpose of the master scale is to differantiate the risk. Over 0.30 HHI values indicate high concentration. This may be due to modeling phase or the incorrect creation of the master scale.

#The HHI test is performed to measure the concentration of the master scaleHerfindahl.Hirschman.Index(master_scale,"Total.Observations")

Output:

0.1463665

With the help of the “anchor.point” function, it is tested whether the default rate is compatible with average PD at the expected levels.

#Performing the Anchor point testAnchor.point(master_scale,"PD","Total.Observations",0.30)

Output:

Hands-On Introduction to creditR: An Amazing R Package to Enhance Credit Risk Scoring and Validation (17)

Chi square test can also be used as a calibration test. The “chisquare.test” function can be used to perform the test in the specified confidence level.

#The Chi-square test is applied on the master scalechisquare.test(master_scale,"PD","Bad.Count","Total.Observations",0.90)

Output:

Hands-On Introduction to creditR: An Amazing R Package to Enhance Credit Risk Scoring and Validation (18)

Binomial test can also be applied as a calibration test. The one-tail binomial test is usually used for IRB models, while the two-tail test is used for IFRS 9 models. But the two-tail test will be more convenient for general use, except for IRB.

#The Binomial test is applied on the master scalemaster_scale$DR <- master_scale$Bad.Count/master_scale$Total.ObservationsBinomial.test(master_scale,"Total.Observations","PD","DR",0.90,"one")

Output:

Hands-On Introduction to creditR: An Amazing R Package to Enhance Credit Risk Scoring and Validation (19)

Modeling and model validation need to be managed to ensure continuity. When the R environment is managed correctly, this manageable modeling and validation environment can be provided easily by institutions.

Institutions are designing much more efficient business processes using open source environments such as R or Python with big data technologies. From this perspective, creditR offers organizational convenience to the application of modeling and validation methods.

End Notes

The creditR package provides users with a number of methods to perform traditional credit risk scoring, as well as some of those for testing model validity, which can also be applied to ML algorithms. Moreover, as the package provides automation in the application of the traditional methods, the operational costs for these processes can be reduced.

Furthermore, these models can be compared with the machine learning models in order to demonstrate that the ML models also meet the regulatory requirements, the meeting of which is the precondition for the application of ML models.

Bug Fixes and About the Author

Please inform the author about the errors you have encountered while using the package via the e-mail address that is shared below.

Ayhan Dis – Senior Risk Consultant

Hands-On Introduction to creditR: An Amazing R Package to Enhance Credit Risk Scoring and Validation (20)

Ayhan Dis is a Senior Risk Consultant. He works on consulting projects like IFRS 9/IRB model development and validation, as well as advanced analytics solutions including ML/DL in areas such as fraud analytics, customer analytics and risk analytics, using Python, R, Base SAS and SQL fluently.

Over the course of his work experience, he has worked with various types of data such as twitter, weather, credit risk, electric hourly price, stock price and customer data to offer solutions to his clients from sectors such as banking, energy, insurance, financing and pharmaceutical industry.

As a data science enthusiast, he thinks that the real thrill of data science is not found in establishing one’s technical abilities; instead, it is found in blending data science together with big data to reveal insights which can be integrated with the bussiness processes, through artificial intelligence.

Credit Riskdata sciencemachine learningR

g

guest_blog06 May 2019

AdvancedData ScienceLibrariesMachine LearningProgramming

Hands-On Introduction to creditR: An Amazing R Package to Enhance Credit Risk Scoring and Validation (2024)

FAQs

What is the R package for credit risk? ›

The package aims to facilitate the applications of the methods of variable analysis, variable selection, model development, model calibration, rating scale development and model validation. Through the functions defined, these methodologies can be applied quickly on all modeling data or a specific variable.

How to improve credit risk analysis? ›

3 Ways to Take Your Credit Risk Analysis Process to the Next Level
  1. Refine Credit Scoring Techniques.
  2. Incorporate Trend Analysis into your Credit Risk Assessment Process.
  3. Embrace New Technology and Tools to Improve Credit Risk Analysis.

How to validate credit risk models? ›

Some of the most common strategies include:
  1. Testing the model's accuracy using historical data.
  2. Calibrating the model using a set of representative data.
  3. Generating forecasts using a set of representative data.
  4. Evaluating the model's accuracy using a set of performance metrics.
Apr 16, 2024

What is the introduction of credit risk management? ›

Credit risk management is the practice of mitigating losses by assessing borrowers' credit risk – including payment behavior and affordability.

What is an R rating credit score? ›

An "R" rating is also included in your credit score. This rating is assigned by lenders based on your past history of borrowing and paying off debts, and it can range from 1 through 9. An R1 rating is the best, meaning you pay your debts on time, within 30 days, and an R9 is the worst.

What are the R principles of credit? ›

3 R's of credit: Returns, Repayment Capacity and Risk bearing ability. This is an important measure in the credit analysis. The banker needs to have an idea about the extent of returns likely to be obtained from the proposed investment.

What are the 5 Cs of credit risk analysis? ›

The five Cs of credit are character, capacity, capital, collateral, and conditions.

What is the credit risk scoring model? ›

A credit scoring model is a mathematical model used to estimate the probability of default, which is the probability that customers may trigger a credit event (e.g., bankruptcy, obligation default, failure to pay, and cross-default events).

How to be a great credit analyst? ›

Some of the essential credit analyst skills include financial and quantitative skills, due diligence, proficiency in statistical software, and the ability to work under pressure. Credit analysts can acquire the skills by undergoing formal training or by learning on-the-job while working in credit analysis.

What is the core responsibility of a credit risk manager? ›

Key responsibilities include:

ensuring all credit risk exposures at clients, product, and portfolio level remain appropriate and within acceptable parameters. monitoring and communicating the level of credit risk taken to senior management. undertaking key tasks in credit risk management on a day to day basis.

What are the basics of credit risk? ›

Credit risk is the risk of loss resulting from the borrower failing to make full and timely payments of interest and/or principal. The key components of credit risk are risk of default and loss severity in the event of default. The product of the two is expected loss.

What is an R on a credit report? ›

"R” refers to revolving credit like a credit card, where you have a credit line that you borrow against each month and then repay only the amount that you use. The R rating reflects your repayment history for revolving credit accounts. There is also the “I” rating for installment loans, like a student or mortgage loan.

What is in an R package? ›

What is R packages? An R package is an extension of R containing data sets and specific functions to solve specific questions. R comes with standard (or base) packages, which contain the basic functions and data sets as well as standard statistical and graphical functions that allow R to work.

What is R used for in finance? ›

R is also a common symbol representing "return" in many financial formulas. There are many different types of returns and they are usually denoted with the upper or lower case letter "R," though there is no formal designation. If there are multiple returns used in a calculation, they are often given subscript letters.

What are the financial analysis packages in R? ›

R is a language widely used in statistical computing and graphics. It is open-source, and it offers a wide variety of packages for financial analysis. Among these packages are PerformanceAnalytics, Quantmod, and Tidyquant, which are useful for data importing, data visualization, and performance measurement.

Top Articles
Latest Posts
Article information

Author: Kelle Weber

Last Updated:

Views: 5686

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Kelle Weber

Birthday: 2000-08-05

Address: 6796 Juan Square, Markfort, MN 58988

Phone: +8215934114615

Job: Hospitality Director

Hobby: tabletop games, Foreign language learning, Leather crafting, Horseback riding, Swimming, Knapping, Handball

Introduction: My name is Kelle Weber, I am a magnificent, enchanting, fair, joyous, light, determined, joyous person who loves writing and wants to share my knowledge and understanding with you.