Get full access to all Data Science, Machine Learning, and AI courses built for finance professionals.
One-time payment - Lifetime access
Or create a free account to start
A step-by-step guide covering Python, SQL, analytics, and finance applications.
Or create a free account to access more
Get full access to all Data Science, Machine Learning, and AI courses built for finance professionals.
One-time payment - Lifetime access
Or create a free account to start
A step-by-step guide covering Python, SQL, analytics, and finance applications.
Or create a free account to access more
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)
2The “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.
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 As you can see we have 133889 observations and 145 variables.
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)"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)')
3We 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)
31> loandata %>% group_by(loan_status) %>% summarise(count = n())
2# A tibble: 4 x 2
3 loan_status count
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)
31> loandata$loan_status = ifelse(str_detect(loandata$loan_status,"Paid"),loandata$loan_status,"Default")
2In 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
We can use the ggplot package to plot the graph for loan status.
1> install.packages("ggplot2")
2> library(ggplot2)
31> 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.