Answer the following questions by using unique or table function:
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.
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.
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.
ggplot(data = loans) +
geom_histogram(mapping = aes(x = loan_amount), binwidth = 2000) +
xlim(0,41000)
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.
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:
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.
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)))