Lab Exercise 1

Answer the following questions by using unique or table function:

  1. How many distinct values are there for homeownership variable? Which value is the most common one?
sort(table(loans$homeownership))
## 
##               ANY      OWN     RENT MORTGAGE 
##        0        0     1353     3858     4789

By using the table function, we see that homeownership variable actually has 4 distinct values (ANY, OWN, RENT, and MORTGAGE), despite ANY having a count of 0. The most common value is MORTGAGE.


  1. How many distinct interest rates are there? Which value is the most common one?
interest_rate_table <- sort(table(loans$interest_rate))
distinct_count <- length(interest_rate_table)
distinct_count
## [1] 58
most_common <- interest_rate_table[length(interest_rate_table)]
most_common
## 9.93 
##  390

By making a frequency table using the table() function, we see that there are 58 distinct values for interest rates, and the most common one is 9.93% appearing 390 times.


  1. Apply table function to the annual_income variable. Do you think the result is helpful or not?
tail(table(loans$annual_income),20)
## 
##  450000  485000  498000   5e+05  520000  550000   6e+05  650000   7e+05  740000 
##       2       1       1       5       1       2       2       1       1       1 
##  750000  780000  793000  885000  910000 1020000 1050000 1200000 1600001 2300000 
##       1       2       1       1       1       1       1       1       1       1

By observing several values in the table, I do not think the result is helpful as most values have only single digit frequency, which doesn’t tell much when there are 10000 distinct values.


Lab Exercise 2

  1. Create a histogram of loan_amount. Customize your plot to give a graph that looks most reasonable to you.
ggplot(data = loans) +
    geom_histogram(mapping = aes(x = loan_amount), binwidth = 2000) +
    xlim(0,41000)


  1. Create a histogram of annual_income. What is the issue with your graph?
ggplot(data = loans) +
    geom_histogram(mapping = aes(x = annual_income), binwidth = 20000)

The issue with this graph is that most of the data are clustered together despite having extreme spread on both the x(annual_income) and y(count) axis. It is hard to draw meaningful conclusion on the data set if solely based on the graph.


Lab Exercise 3

Create a histogram of variable debt_to_income in loans with the following requirements:

Question: Can you explain the distribution of debt_to_income?

ggplot(loans, aes(x = debt_to_income)) +
    geom_histogram(aes(y = after_stat(density)), binwidth = 2, color = 'black', fill = 'white') +
    xlim(0,100) +
    geom_density(adjust = 1, linewidth = 1.1)

The distribution of dept_to_income by density is very right skewed, with almost all sample being below 50%. This could (potentially) be justified by the following:

  1. The majority of sampled DTI is found near 20%, very likely due to mortgage payment.
  2. According to some finance website, 43% is considered the highest DTI ratio that a borrower can have to still apply for mortgage, any higher and they are refused, this could potentially explain why the curve approaches 0 once past 50%.


Lab Exercise 4

For loans data, create a scatter plot of interest_rate vs debt_to_income with mapping color to grade. What can you learn from the graph?

ggplot(data = loans)+
  geom_point(mapping = aes(x = interest_rate, y = debt_to_income, color = grade))

Conclusions from observation on the graph:

Interest rate and the letter grades are (roughly) linearly related as the grade moves up a letter for every 3%~5% increase in the interest rate. This is understandable as higher grade means higher risk of loss for the lender, therefore higher interest rate.


HW Exercise

Create a scatter plot of loan_amount vs interest_rate with a color grouping using term variable (please use factor(term) to convert it into a categorical variable). Save your plot to your local folder.

ggplot(data = loans)+
  geom_point(mapping = aes(x = interest_rate, y = loan_amount, colour = factor(term)))