Lab Exercise #1

# Distinct homeownship values
unique_homeownership <- unique(loans$homeownership)
most_common_homeownership <- names(sort(table(loans$homeownership), decreasing = TRUE))[1]
list(distinct_values = length(unique_homeownership), most_common = most_common_homeownership)
## $distinct_values
## [1] 3
## 
## $most_common
## [1] "MORTGAGE"
# Checking distinct values for interest rate
unique_rates <- length(unique(loans$interest_rate))
most_common_rate <- names(sort(table(loans$interest_rate), decreasing = TRUE))[1]
list(distinct_values = unique_rates, most_common = most_common_rate)
## $distinct_values
## [1] 58
## 
## $most_common
## [1] "9.93"
# Applying table function to annual_income
income_table <- table(loans$annual_income)
income_table[1:80]
## 
##     0     1  3000  3120  3300  4000  4800  5000  5208  5235  5500  7200  7500 
##    23     1     2     1     1     1     1     2     1     1     1     1     1 
##  7800  8000  8500  9000  9600  9840  9972  9996 10000 10320 10500 10548 10596 
##     1     1     1     4     2     1     1     1    11     1     2     1     1 
## 10800 11000 11150 11352 11772 12000 12036 12250 12276 12300 12480 12696 12816 
##     4     3     1     1     1     9     1     1     1     1     1     1     1 
## 13000 13050 13136 13140 13164 13200 13390 13500 13692 13728 13800 13920 13930 
##     6     1     1     1     1     2     1     1     1     1     1     1     1 
## 14000 14200 14300 14364 14400 14500 14508 14560 14568 14616 14928 15000 15200 
##     5     1     1     1     4     1     1     1     1     1     1    27     1 
## 15250 15380 15500 15599 15600 15864 15972 15984 15996 16000 16200 16356 16500 
##     1     1     1     1     2     1     1     1     1     7     2     1     1 
## 16620 16700 
##     1     1

The results are not useful at all, since the data is continuous.

Lab Exercise: Histogram of Loan Amount

ggplot(loans, aes(x = loan_amount)) +
  geom_histogram(bins = 10, color='black', fill = 'turquoise') +
  theme_fivethirtyeight() +
  labs(title = "Histogram of Loan Amount", x = "Loan Amount", y = "Frequency")

Lab Exercise: Histogram of Annual Income

ggplot(loans, aes(x = annual_income)) +
  geom_histogram(binwidth = 10000, fill = "turquoise", color = "black") +
  theme_fivethirtyeight() +
  labs(title = "Histogram of Annual Income", x = "Annual Income", y = "Frequency")

Issue: The histogram is heavily right-skewed due to outliers - billionaires.

Lab Exercise: Histogram of Debt-to-Income Ratio

ggplot(loans, aes(x = debt_to_income)) +
  geom_histogram(binwidth = 2, fill = "turquoise", color = "black") +
  xlim(0, 100) +
  geom_density(aes(y = ..count.. * 2), color = "blue", size = 1) +
  theme_fivethirtyeight() +
  labs(title = "Histogram and Density Plot of Debt-to-Income Ratio", x = "Debt-to-Income Ratio", y = "Count")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: The dot-dot notation (`..count..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(count)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Removed 57 rows containing non-finite outside the scale range
## (`stat_bin()`).
## Warning: Removed 57 rows containing non-finite outside the scale range
## (`stat_density()`).
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_bar()`).

The distribution appears right-skewed, with most values clustered in the lower range, meaning most borrowers have low debt-to-income ratios.

Lab Exercise: Scatter Plot of Interest Rate vs Debt-to-Income

ggplot(loans, aes(x = debt_to_income, y = interest_rate, color = grade)) +
  geom_point(alpha = 0.6) +
  theme_fivethirtyeight() +
  labs(title = "Scatter Plot of Interest Rate vs Debt-to-Income", x = "Debt-to-Income Ratio", y = "Interest Rate")
## Warning: Removed 24 rows containing missing values or values outside the scale range
## (`geom_point()`).

Higher debt-to-income ratios tend to correlate with higher interest rates, and lower grades appear to be associated with higher interest rates, which makes sense.