# Explore Loan Data in R - Loan Grade and Interest Rate

There is no set path to how one would go about analyzing a data set. Typically, a data scientist would spend quite some time exploring and observing the data to understand it well.

Let’s look at some of the attributes in our dataset and see their relationship with the default rate. For each loan, we have two basic attributes, namely, grade and the interest rate. In LendingClub, loan grade, or credit grade, is the letter (A-G or AA-HR) that is assigned to a borrower and corresponds with the interest rate that is charged for the loan.

These rates are set based on the originator’s underwriting assessment for the individual borrower. The higher the expected risk of default, the higher the interest rate is set in order to offset this risk. The assessment of the credit grade decision includes FICO, loan term (a shorter term is considered better), proprietary models, and the loan amount requested by the borrower.

We also have the interest rate being charged for each loan.

### Default Rate for Each Loan Grade

Let’s create a plot where we will plot the default rate for each grade.

The following command will give us the number of defaults for each grade.

```
> g1 = loandata %>% filter(loan_status == "Default") %>% group_by(grade) %>% summarise(default_count = n())
> g1
# A tibble: 7 x 2
grade default_count
<chr> <int>
1 A 975
2 B 3487
3 C 5534
4 D 3697
5 E 2704
6 F 1260
7 G 379
>
```

We can now calculate the default rate in each grade as follows:

```
> g2 = loandata %>% group_by(grade) %>% summarise(count = n())
> g3 <- g2 %>% left_join(g1) %>% mutate(default_rate = 100*default_count/count) %>% select(grade,count,default_count,default_rate)
Joining, by = "grade"
> g3
# A tibble: 7 x 4
grade count default_count default_rate
<chr> <int> <int> <dbl>
1 A 9956 975 9.79
2 B 16649 3487 20.9
3 C 16815 5534 32.9
4 D 8480 3697 43.6
5 E 5261 2704 51.4
6 F 2109 1260 59.7
7 G 599 379 63.3
>
```

We can now plot this using ggplot2.

```
> ggplot(g3, aes(x=grade, y=default_rate, fill=grade)) + geom_bar(stat="identity")
```

As we would expect, riskier loans have higher default rates.

### Loan Grade Vs Interest Rate

We can also plot the relationship between Loan Grade and Interest Rate.

To do so, we will first convert the interest rate attribute to numeric.

```
> loandata$int_rate = (as.numeric(gsub(pattern = "%",replacement = "",x = loandata$int_rate)))
```

We can now group the data by grade and their mean interest rates:

```
> x1 = loandata %>% filter(loan_status == "Default") %>% group_by(grade) %>% summarise(int_rate=mean(int_rate))
```

```
> ggplot(x1, aes(x=grade, y=int_rate, fill=grade)) + geom_bar(stat="identity",position="dodge")
```

As we would expect, riskier loans (higher grades) have higher interest rates.

#### Course Downloads

- Credit Risk Modelling - Case Studies
- Classification vs. Regression Models
- Case Study - German Credit - Steps to Build a Predictive Model
- Import Credit Data Set in R
- German Credit Data : Data Preprocessing and Feature Selection in R
- Credit Modelling: Training and Test Data Sets
- Build the Predictive Model
- Logistic Regression Model in R
- Measure Model Performance in R Using ROCR Package
- Create a Confusion Matrix in R
- Credit Risk Modelling - Case Study- Lending Club Data
- Explore Loan Data in R - Loan Grade and Interest Rate
- Credit Risk Modelling - Required R Packages
- Loan Data - Training and Test Data Sets
- Data Cleaning in R - Part 1
- Data Cleaning in R - Part 2
- Data Cleaning in R - Part 3
- Data Cleaning in R - Part 5
- Remove Dimensions By Fitting Logistic Regression
- Create a Function and Prepare Test Data in R
- Building Credit Risk Model
- Credit Risk - Logistic Regression Model in R
- Support Vector Machine (SVM) Model in R
- Random Forest Model in R
- Extreme Gradient Boosting in R
- Predictive Modelling: Averaging Results from Multiple Models
- Predictive Modelling: Comparing Model Results
- How Insurance Companies Calculate Risk

# R Programming Bundle: 25% OFF

**R Programming - Data Science for Finance Bundle**for just $29 $39.