Explore Financial Data in R
Now that we have the data file in our working directory, we can load it in our R session and start exploring it.
Use the following command to load the data into R.
1> loandata = read.csv("loan_data_2017.csv",stringsAsFactors=FALSE)
2
The âstringsAsFactors = FALSEâ parameter turns off the automatic conversion of character strings to factors.
Let's set the seed to 0 so that we can replicate the results.
set.seed(0)
Before we proceed with our analysis, we need to understand whatâs contained in the dataset. We may also possibly want to remove unwanted data and transform some data to make it usable for analysis.
Data Summary
Letâs first analyse the structure of the data using str()
 or summary()
 function.
1> str(loandata)
2'data.frame': 133889 obs. of 145 variables:
3 $ id : chr "" "" "" "" ...
4 $ member_id : logi NA NA NA NA NA NA ...
5 $ loan_amnt : int 10000 35000 20000 17475 8000 14400 18000 5800 12500 3000 ...
6... (List of all variables)
7
As you can see we have 133889 observations and 145 variables.
Loan Statuses
One of the variables is loan_status
. Letâs look at what unique values we have for loan_status
.
1> unique(loandata$loan_status)
2[1] "Fully Paid" "Current" "Late (31-120 days)" "Charged Off"
3[5] "Late (16-30 days)" "In Grace Period" "Default" ""
4>
5
As we can see, we have 7 loan statuses: Charged Off,Current,Default,Fully Paid,In Grace Period,Late (16-30 days),Late (31-120 days). We consider Late (31-120 days), Default, Charged Off as a default loan and Fully Paid as a desirable loan and ignore everything else.
So, letâs remove the records for loan statuses âCurrentâ, âIn Grace Periodâ, and âLate (16-30 daysâ. This can be done in many ways, we will use the subset()
 function.
1> loandata <- subset(loandata, loan_status!='In Grace Period')
2> loandata <- subset(loandata, loan_status!='Late (16-30 days)')
3> loandata <- subset(loandata, loan_status!='Current')
4>
5
We will also remove records where loan status is empty.
> loandata <- subset(loandata, loan_status!='')
We are now left with only 4 loan statuses, namely, Fully Paid, Late (31-120 days), Charged Off, and Default.
Let us look at the number of each loan status. For this purpose, we will use a package called dplyr
. We can use its group_by
 function of dplyr
 to look at how many loans are there for each status.
1> install.packages("dyplr")
2> library(dplyr)
3
1> loandata %>% group_by(loan_status) %>% summarise(count = n())
2# A tibble: 4 x 2
3 loan_status count
4 <chr> <int>
51 Charged Off 15698
62 Default 9
73 Fully Paid 41833
84 Late (31-120 days) 2329
9>
10
Loan Statuses: Default and Fully Paid
Since, we want to look a only two scenarios, default an no default, we will combine Charged Off, Default, and Late (31-120 days) to a single category:Â Default
.
Here we will use another R package called stringr
 for string manipulations.
1install.packages("stringr")
2library(stringr)
3
1> loandata$loan_status = ifelse(str_detect(loandata$loan_status,"Paid"),loandata$loan_status,"Default")
2
In this command, we make use of the str_detect
 function of stringr
 package to detect the present of the string 'Paid'. Then use ifelse condition to keep those records as is, but change the status of all other records to Default.
We can again look at the number of each loan status to make sure that our data is correct.
1> loandata %>% group_by(loan_status) %>% summarise(count = n())
2# A tibble: 2 x 2
3 loan_status count
4 <chr> <int>
51 Default 18036
62 Fully Paid 41833
7>
8
Plotting Loan Status
We can use the ggplot
 package to plot the graph for loan status.
1> install.packages("ggplot2")
2> library(ggplot2)
3
1> g <- ggplot(loandata, aes(x = loan_status, fill=loan_status))
2> g + geom_bar()
3
In the next lesson, we will start working on our problem of predicting loan default.