<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/visualizations-for-credit-modeling-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://datascienceplus.com/visualizations-for-credit-modeling-in-r/"> R Programming – DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<ul></ul>
<p><strong>
<p>Categories</p>
<p></p></strong>
<ol>
<li><a rel="nofollow" href="https://datascienceplus.com/category/visualizing-data/" title="Permanent Link toVisualizing Data" target="_blank">Visualizing Data</a></li>
</ol>
<p><strong>
<p>Tags</p>
<p></p></strong>
<ol>
<li><a rel="nofollow" href="https://datascienceplus.com/tag/data-visualisation/" title="Permanent Link toData Visualisation" target="_blank">Data Visualisation</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/tag/ggplot2/" title="Permanent Link toggplot2" target="_blank">ggplot2</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/tag/rstats/" title="Permanent Link toR Programming" target="_blank">R Programming</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/tag/tidyverse/" title="Permanent Link totidyverse" target="_blank">tidyverse</a></li>
</ol>
<p>Visualization is a great way to get an overview of credit modeling. Typically you will start by making data management and data cleaning and after this, your credit modeling analysis will start with visualizations. This article is, therefore, the first part of a credit machine learning analysis with visualizations. The second part of the analysis will typically use logistic regression and ROC curves. </p>
<h3>Library of R packages</h3>
<p>In the following section we will use R for visualization of credit modelling. First we read the packages into the R library:</p>
<pre>
# Data management packages
library(readr) 
library(lubridate)
library(magrittr)
library(plyr)
library(dplyr) 
library(gridExtra) 
# Visualization packages
library(ggplot2) 
library(plotly)
library(ggthemes) 
</pre>
<h3>Load dataset and data management</h3>
<p>Next it is time to read the dataset and do some data management. We use the lending club loan <a rel="nofollow" href="https://www.kaggle.com/wendykan/lending-club-loan-data" target="_blank">dataset</a>:</p>
<pre>
# Read the dataset into R library
loan &lt;- read.csv(&quot;/loan.csv&quot;)
# Data management of the dataset
loan$member_id &lt;- as.factor(loan$member_id)
loan$grade &lt;- as.factor(loan$grade)
loan$sub_grade &lt;- as.factor(loan$sub_grade)
loan$home_ownership &lt;- as.factor(loan$home_ownership)
loan$verification_status &lt;- as.factor(loan$verification_status)
loan$loan_status &lt;- as.factor(loan$loan_status)
loan$purpose &lt;- as.factor(loan$purpose)
</pre>
<p>After the above data management it is time for data selection and data cleaning:</p>
<pre>
# Selection of variables for the analysis
loan &lt;- loan[,c(&quot;grade&quot;,&quot;sub_grade&quot;,&quot;term&quot;,&quot;loan_amnt&quot;,&quot;issue_d&quot;,&quot;loan_status&quot;,&quot;emp_length&quot;,
                          &quot;home_ownership&quot;, &quot;annual_inc&quot;,&quot;verification_status&quot;,&quot;purpose&quot;,&quot;dti&quot;,
                          &quot;delinq_2yrs&quot;,&quot;addr_state&quot;,&quot;int_rate&quot;, &quot;inq_last_6mths&quot;,&quot;mths_since_last_delinq&quot;,
                          &quot;mths_since_last_record&quot;,&quot;open_acc&quot;,&quot;pub_rec&quot;,&quot;revol_bal&quot;,&quot;revol_util&quot;,&quot;total_acc&quot;)]
# Data cleaningt for missing observations
loan$mths_since_last_delinq[is.na(loan$mths_since_last_delinq)] &lt;- 0
loan$mths_since_last_record[is.na(loan$mths_since_last_record)] &lt;- 0
var.has.na &lt;- lapply(loan, function(x){any(is.na(x))})
num_na &lt;- which( var.has.na == TRUE )	
loan &lt;- loan[complete.cases(loan),]
skim(loan)
<em>Skim summary statistics
n obs: 886877 n variables: 23 -- Variable type:factor --------------------------------------------------------
variable missing complete n n_unique top_counts ordered
addr_state 0 886877 886877 51 CA: 129456, NY: 74033, TX: 71100, FL: 60901 FALSE
emp_length 0 886877 886877 12 10+: 291417, 2 y: 78831, &lt; 1: 70538, 3 y: 69991 FALSE
grade 0 886877 886877 7 B: 254445, C: 245721, A: 148162, D: 139414 FALSE
home_ownership 0 886877 886877 6 MOR: 443319, REN: 355921, OWN: 87408, OTH: 180 FALSE
issue_d 0 886877 886877 103 Oct: 48619, Jul: 45938, Dec: 44323, Oct: 38760 FALSE
loan_status 0 886877 886877 8 Cur: 601533, Ful: 209525, Cha: 45956, Lat: 11582 FALSE
purpose 0 886877 886877 14 deb: 524009, cre: 206136, hom: 51760, oth: 42798 FALSE
sub_grade 0 886877 886877 35 B3: 56301, B4: 55599, C1: 53365, C2: 52206 FALSE
term 0 886877 886877 2 36: 620739, 60: 266138, NA: 0 FALSE
verification_status 0 886877 886877 3 Sou: 329393, Ver: 290896, Not: 266588, NA: 0 FALSE
-- Variable type:numeric -------------------------------------------------------
variable missing complete n mean sd p0 p25 p50 p75 p100 hist
annual_inc 0 886877 886877 75019.4 64687.38 0 45000 65000 90000 9500000 ????????
delinq_2yrs 0 886877 886877 0.31 0.86 0 0 0 0 39 ????????
dti 0 886877 886877 18.16 17.19 0 11.91 17.66 23.95 9999 ????????
inq_last_6mths 0 886877 886877 0.69 1 0 0 0 1 33 ????????
int_rate 0 886877 886877 13.25 4.38 5.32 9.99 12.99 16.2 28.99 ????????
loan_amnt 0 886877 886877 14756.97 8434.43 500 8000 13000 20000 35000 ????????
mths_since_last_delinq 0 886877 886877 16.62 22.89 0 0 0 30 188 ????????
mths_since_last_record 0 886877 886877 10.83 27.65 0 0 0 0 129 ????????
open_acc 0 886877 886877 11.55 5.32 1 8 11 14 90 ????????
pub_rec 0 886877 886877 0.2 0.58 0 0 0 0 86 ????????
revol_bal 0 886877 886877 16924.56 22414.33 0 6450 11879 20833 2904836 ????????
revol_util 0 886877 886877 55.07 23.83 0 37.7 56 73.6 892.3 ????????
total_acc 0 886877 886877 25.27 11.84 1 17 24 32 169 ????????
</em></pre>
<h2>Visualizations for credit modeling</h2>
<p>After loading the dataset and data management it is time to make the credit modelling visualizations in R:</p>
<pre>
# Chart on customers
ggplot(data = loan,aes(x = grade)) + geom_bar(color = &quot;blue&quot;,fill =&quot;green&quot;) +geom_text(stat='count', aes(label=..count..))+ theme_solarized()
ggplotly(p = ggplot2::last_plot())
</pre>
<p>The above coding gives us the following graph:<br>
<a rel="nofollow" href="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2018/10/Visualization-I-3.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2018/10/Visualization-I-3-490x421.png?w=450&ssl=1" alt="" class="wp-image-17740" data-recalc-dims="1"></a></p>
<p>Let’s look at which grading group are house owners:</p>
<pre>
# Chart on customers living
ggplot(data = loan,aes(x = home_ownership)) + geom_bar(color = &quot;blue&quot;,fill =&quot;green&quot;) +geom_text(stat='count', aes(label=..count..))+ theme_solarized()
ggplotly(p = ggplot2::last_plot())
</pre>
<p>This gives us the following bar plot:<br>
<a rel="nofollow" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2018/10/Visualization-2-1.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2018/10/Visualization-2-1-490x421.png?w=450&ssl=1" alt="" class="wp-image-17741" data-recalc-dims="1"></a></p>
<p>Now for the next visualizations, we need to make some data management:</p>
<pre>
# Data management for loan status
revalue(loan$loan_status, c(&quot;Does not meet the credit policy. Status:Charged Off&quot; = &quot;Charged Off&quot;)) -> loan$loan_status
revalue(loan$loan_status, c(&quot;Does not meet the credit policy. Status:Fully Paid&quot; = &quot;Fully Paid&quot;)) -> loan$loan_status
loan %>% group_by(loan$loan_status) %>% dplyr::summarize(total = n()) -> loan_status_data
loan %>% group_by(loan$loan_status) %>% dplyr::summarize(total = n()) -> loan_status_data
# Chart with customer living and loan status
ggplot(data=loan, aes(x=home_ownership, fill=loan_status)) + geom_bar()
ggplotly(p = ggplot2::last_plot())
</pre>
<p>The above coding gives us the following visualization:<br>
<a rel="nofollow" href="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2018/10/Visualization-3.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2018/10/Visualization-3-490x389.png?w=450&ssl=1" alt="" class="wp-image-17700" data-recalc-dims="1"></a></p>
<p>Now lets look at customers on loan verification:</p>
<pre>
# Customer and loan verification
ggplot(data=loan, aes(x=verification_status, fill=loan_status))+ geom_bar()
ggplotly(p = ggplot2::last_plot())
</pre>
<p>This gives us the following plot:<br>
<a rel="nofollow" href="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2018/10/V5.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2018/10/V5-490x388.png?w=450&ssl=1" alt="" class="wp-image-17701" data-recalc-dims="1"></a></p>
<p>Lets look at the loan verification as loan amount and interest rate graph:</p>
<pre>
# Loan amount
ggplot(data = loan,aes(x = loan_amnt)) + geom_bar(color = 'green')
ggplotly(p = ggplot2::last_plot())
# Interest rate
ggplot(data = loan,aes(x = int_rate))+ geom_bar(color = 'green')
ggplotly(p = ggplot2::last_plot())
</pre>
<p>This gives the following two graphs:<br>
<a rel="nofollow" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2018/10/V6.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2018/10/V6-490x388.png?w=450&ssl=1" alt="" class="wp-image-17704" data-recalc-dims="1"></a></p>
<p><a rel="nofollow" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2018/10/V7.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2018/10/V7-490x388.png?w=450&ssl=1" alt="" class="wp-image-17705" data-recalc-dims="1"></a></p>
<p>Now lets look at histogram based upon loan amount and interest rate:</p>
<pre>
#Histogram on loan amount
ggplot(data = loan,aes (x = loan_amnt,fill= grade))+ geom_histogram()
ggplotly(p = ggplot2::last_plot())
#Histogram  on interest rate
ggplot(data = loan,aes (x = int_rate,fill= grade))+ geom_histogram()
ggplotly(p = ggplot2::last_plot())
</pre>
<p>This gives us the following two histograms:<br>
<a rel="nofollow" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2018/10/V8.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2018/10/V8-490x388.png?w=450&ssl=1" alt="" class="wp-image-17706" data-recalc-dims="1"></a><br>
<a rel="nofollow" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2018/10/V9.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2018/10/V9-490x388.png?w=450&ssl=1" alt="" class="wp-image-17707" data-recalc-dims="1"></a></p>
<p>Now let’s look at density plot based on interest rate and loan amount:</p>
<pre>
# Density on interest rate
ggplot(data = loan,aes(x = int_rate)) + geom_density(fill = 'green',color = 'blue')
ggplotly(p = ggplot2::last_plot())
# Density on loan amount
ggplot(data = loan,aes(x = loan_amnt)) + geom_density(fill = 'green',color = 'blue')
ggplotly(p = ggplot2::last_plot())
</pre>
<p>This gives us the following density plots:<br>
<a rel="nofollow" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2018/10/V10-1.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2018/10/V10-1-490x421.png?w=450&ssl=1" alt="" class="wp-image-17748" data-recalc-dims="1"></a><br>
<a rel="nofollow" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2018/10/V11-1.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2018/10/V11-1-490x421.png?w=450&ssl=1" alt="" class="wp-image-17749" data-recalc-dims="1"></a></p>
<p>Next, it is time to look at the density plot on loan- and interest rate based grade type</p>
<pre>
#density on loan based on grade type
ggplot(data = loan,aes(x = loan_amnt,fill = grade)) + geom_density()
ggplotly(p = ggplot2::last_plot())
#density on interest rate based on grade type
ggplot(data = loan,aes(x = int_rate,fill = grade)) + geom_density()
ggplotly(p = ggplot2::last_plot())
</pre>
<p>This gives us the following plots:<br>
<a rel="nofollow" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2018/10/V12.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2018/10/V12-490x388.png?w=450&ssl=1" alt="" class="wp-image-17710" data-recalc-dims="1"></a><br>
<a rel="nofollow" href="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2018/10/V13.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2018/10/V13-490x388.png?w=450&ssl=1" alt="" class="wp-image-17711" data-recalc-dims="1"></a></p>
<p>Lastly let us look at box plots for interest rate based on purpose and grade:</p>
<pre>
# Box plot interest rate &amp; purpose
boxplot(int_rate ~ purpose, col=&quot;darkgreen&quot;, data=loan)
# Boxplot interest rate &amp; grade 
boxplot(int_rate ~ grade, col=&quot;darkgreen&quot;, data=loan)
</pre>
<p>The above coding gives us the following two histograms:</p>
<p><a rel="nofollow" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2018/10/V14-1.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2018/10/V14-1-490x421.png?w=450&ssl=1" alt="" class="wp-image-17743" data-recalc-dims="1"></a><br>
<a rel="nofollow" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2018/10/V15-1.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2018/10/V15-1-490x421.png?w=450&ssl=1" alt="" class="wp-image-17744" data-recalc-dims="1"></a></p>
<h3>References</h3>
<ol>
<li><a rel="nofollow" href="https://cran.r-project.org/web/packages/readr/readr.pdf" target="_blank">Using readr in R – CRAN.R-project.org</a></li>
<li><a rel="nofollow" href="https://cran.r-project.org/web/packages/lubridate/lubridate.pdf" target="_blank">Using lubridate in R – CRAN.R-project.org</a></li>
<li><a rel="nofollow" href="https://cran.r-project.org/web/packages/magrittr/vignettes/magrittr.html" target="_blank">Using magrittr in R – CRAN.R-project.org</a></li>
<li><a rel="nofollow" href="https://cran.r-project.org/web/packages/plyr/plyr.pdf" target="_blank">Using plyr in R – CRAN.R-project.org</a></li>
<li><a rel="nofollow" href="https://cran.r-project.org/web/packages/dplyr/dplyr.pdf" target="_blank">Using dplyr in R – CRAN.R-project.org</a></li>
<li><a rel="nofollow" href="https://cran.r-project.org/web/packages/gridExtra/gridExtra.pdf" target="_blank">Using gridExtra in R – CRAN.R-project.org</a></li>
<li><a rel="nofollow" href="https://cran.r-project.org/web/packages/ggplot2/ggplot2.pdf" target="_blank">Using ggplot2 in R – CRAN.R-project.org</a></li>
<li><a rel="nofollow" href="https://cran.r-project.org/web/packages/plotly/plotly.pdf" target="_blank">Using plotly in R – CRAN.R-project.org</a></li>
<li><a rel="nofollow" href="https://cran.r-project.org/web/packages/ggthemes/ggthemes.pdf" target="_blank">Using ggthemes in R – CRAN.R-project.org</a></li>
</ol>
<p><strong>
<p>Related Post</p>
<p></p></strong>
<ol>
<li><a rel="nofollow" href="https://datascienceplus.com/decision-trees-and-random-forests-in-r/" title="Permanent Link toDecision Trees and Random Forests in R" target="_blank">Decision Trees and Random Forests in R</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/add-value-to-your-visualizations-in-r/" title="Permanent Link toAdd value to your visualizations in R" target="_blank">Add value to your visualizations in R</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/visualize-your-portfolios-performance-and-generate-a-nice-report-with-r/" title="Permanent Link toVisualize your Portfolio’s Performance and Generate a Nice Report with R" target="_blank">Visualize your Portfolio’s Performance and Generate a Nice Report with R</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/exploring-san-francisco-bay-areas-bike-share-system/" title="Permanent Link toExploring San Francisco Bay Area’s Bike Share System" target="_blank">Exploring San Francisco Bay Area’s Bike Share System</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/blue-bikes-sharing-in-boston/" title="Permanent Link toAnalysis and Visualization of Blue Bikes Sharing in Boston" target="_blank">Analysis and Visualization of Blue Bikes Sharing in Boston</a></li>
</ol>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://datascienceplus.com/visualizations-for-credit-modeling-in-r/"> R Programming – DataScience+</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/0iUNUUaToDQ" height="1"></a></div>