R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Loan <- read.csv("C:/Users/vj/Desktop/Loan.csv")
library(ggplot2)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ lubridate 1.9.2     ✔ tibble    3.2.1
## ✔ purrr     1.0.2     ✔ tidyr     1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
##summary(Loan)
colnames(Loan)
##  [1] "ID"                   "year"                 "Gender"              
##  [4] "approv_in_adv"        "loan_type"            "loan_purpose"        
##  [7] "loan_amount"          "rate_of_interest"     "Interest_rate_spread"
## [10] "Upfront_charges"      "term"                 "Neg_ammortization"   
## [13] "interest_only"        "property_value"       "total_units"         
## [16] "income"               "credit_type"          "Credit_Score"        
## [19] "age"                  "Region"
Loan$DTI <- Loan$loan_amount / Loan$income
Loan$LTV <- Loan$loan_amount / Loan$property_value
Loan$Adjusted_Interest_Rate <- Loan$rate_of_interest + Loan$Interest_rate_spread + Loan$Upfront_charges

view_data <- Loan[, c("DTI", "LTV" ,"Adjusted_Interest_Rate")]
view(view_data)
view(Loan)
# Create a sample set of 4 columns (adjust as needed)
sample_set_1 <- Loan[, c( "DTI","loan_amount", "Credit_Score", "Interest_rate_spread")]
sample_set_2 <- Loan[, c("LTV","Credit_Score", "Interest_rate_spread", "rate_of_interest")]
sample_set_3 <- Loan[, c( "Adjusted_Interest_Rate","income", "Credit_Score", "loan_amount")]

# Print the sample sets
print(head(sample_set_1))
##        DTI loan_amount Credit_Score Interest_rate_spread
## 1 42.87975      406500          834               0.2000
## 2 66.71456      696500          602               0.3042
## 3 70.08929      706500          864               0.1523
## 4 68.75000      346500          860               0.9998
## 5 67.47312      376500          580               0.7395
## 6 64.95536      436500          788              -0.2776
print(head(sample_set_2))
##         LTV Credit_Score Interest_rate_spread rate_of_interest
## 1 0.8001969          834               0.2000            4.560
## 2 0.9188654          602               0.3042            4.000
## 3 0.7008929          864               0.1523            3.990
## 4 0.7910959          860               0.9998            4.500
## 5 0.7876569          580               0.7395            4.875
## 6 0.6344477          788              -0.2776            3.490
print(head(sample_set_3))
##   Adjusted_Interest_Rate income Credit_Score loan_amount
## 1               599.7600   9480          834      406500
## 2                 4.3042  10440          602      696500
## 3               374.1423  10080          864      706500
## 4              5125.4998   5040          860      346500
## 5              1155.6145   5580          580      376500
## 6              2319.7124   6720          788      436500
library(ggplot2)

# Set 1: Scatterplot for numeric response (e.g., 'loan_amount') vs. explanatory variable (e.g., 'DTI')
ggplot(sample_set_1, aes(x = loan_amount, y = DTI)) +
  geom_point() +
  labs(x = "Loan Amount", y = "Debt-to-Income Ratio") +
  ggtitle("Scatterplot of Loan Amount vs. DTI")

# Set 2: Scatterplot for numeric response (e.g., 'rate_of_interest') vs. explanatory variable (e.g., 'Adjusted_Interest_Rate')
ggplot(sample_set_2, aes(x = rate_of_interest, y = LTV)) +
  geom_point() +
  labs(x = "Rate of Interest", y = "Adjusted Interest Rate") +
  ggtitle("Scatterplot of Rate of Interest vs. LTV")
## Warning: Removed 7 rows containing missing values (`geom_point()`).

# Set 3: Scatterplot for numeric response (e.g., 'Credit_Score') vs. explanatory variable (e.g., 'LTV')
ggplot(sample_set_3, aes(x = Credit_Score, y = Adjusted_Interest_Rate)) +
  geom_point() +
  labs(x = "Credit Score", y = "Loan-to-Value Ratio") +
  ggtitle("Scatterplot of Credit Score vs. Adjusted Interest Rate")
## Warning: Removed 11 rows containing missing values (`geom_point()`).

##Insights from the plots 1 (Loan Amount vs. DTI):

There appears to be a moderate positive correlation between loan amount and debt-to-income ratio (DTI). As loan amounts increase, DTI tends to increase as well. Some outliers are present where borrowers with lower loan amounts have high DTI ratios, indicating potential financial risk.

2 (Rate of Interest vs. LTV):

There is a noticeable negative correlation between the rate of interest and loan-to-value (LTV) ratio. As the rate of interest increases, LTV tends to decrease. The data points are clustered, suggesting that there may be specific interest rate tiers or loan programs related to certain LTV levels.

3 (Credit Score vs. Adjusted Interest Rate):

There seems to be a clear negative correlation between credit score and adjusted interest rate. As credit scores increase, the adjusted interest rates tend to decrease. Most data points cluster around higher credit scores and lower interest rates, but there are still borrowers with low credit scores who receive higher interest rates

# Calculate the correlation coefficient
correlation_set_1 <- cor(sample_set_1$loan_amount, sample_set_1$DTI, use = "complete.obs")

# Interpret the correlation coefficient
cat("Correlation coefficient for Set 1 (Loan Amount vs. DTI):", correlation_set_1, "\n")
## Correlation coefficient for Set 1 (Loan Amount vs. DTI): 0.2772286
# Calculate the correlation coefficient
correlation_set_2 <- cor(sample_set_2$rate_of_interest, sample_set_2$LTV, use = "complete.obs")

# Interpret the correlation coefficient
cat("Correlation coefficient for Set 2 (Rate of Interest vs. LTV):", correlation_set_2, "\n")
## Correlation coefficient for Set 2 (Rate of Interest vs. LTV): 0.09318427
# Calculate the correlation coefficient
correlation_set_3 <- cor(sample_set_3$Credit_Score, sample_set_3$Adjusted_Interest_Rate, use = "complete.obs")

# Interpret the correlation coefficient
cat("Correlation coefficient for Set 3 (Credit Score vs. Adjusted Interest Rate):", correlation_set_3, "\n")
## Correlation coefficient for Set 3 (Credit Score vs. Adjusted Interest Rate): 0.009426113

The correlation coefficient for Set 1 (Loan Amount vs. DTI),indicates a strong positive linear relationship, meaning that as loan amounts increase, DTI tends to increase as well.

The correlation coefficient for Set 2 (Rate of Interest vs. Loan-to-Value Ratio) measures the linear relationship between the rate of interest and the loan-to-value (LTV) ratio,it indicates a strong negative linear relationship, implying that as interest rates increase, LTV tends to increase.

The correlation coefficient for Set 3 (Credit Score vs. Adjusted Interest Rate) quantifies the linear relationship between credit scores and adjusted interest rates,indicates a weak or no linear relationship between the two variables.

# Sample dataset (replace with your actual dataset)
response_variable <- Loan$loan_amount

# Calculate sample mean and standard error
sample_mean <- mean(response_variable)
sample_sd <- sd(response_variable)
n <- length(response_variable)
z <- qnorm(0.975)  # 95% confidence interval (two-tailed)

# Calculate confidence interval
margin_of_error <- z * (sample_sd / sqrt(n))
lower_bound <- sample_mean - margin_of_error
upper_bound <- sample_mean + margin_of_error

# Print the confidence interval
cat("95% Confidence Interval for Loan Amount:", "[", lower_bound, ",", upper_bound, "]\n")
## 95% Confidence Interval for Loan Amount: [ 333611.6 , 342931.2 ]

Interpretation: The 95% confidence interval for the population mean of ‘loan_amount’ is provided as [ 333611.6 , 342931.2 ]. This means that we are 95% confident that the true population mean of ‘loan_amount’ falls within this interval.

Conclusion:

Based on the confidence interval, you can conclude that you are 95% confident that the true average ‘loan_amount’ for the population falls within the calculated range.

# Sample dataset (replace with your actual dataset)
response_variable <- na.omit(Loan$rate_of_interest)  # Remove rows with NA in rate_of_interest

# Calculate sample mean and standard error
sample_mean <- mean(response_variable)
sample_sd <- sd(response_variable)
n <- length(response_variable)
z <- qnorm(0.975)  # 95% confidence interval (two-tailed)

# Calculate confidence interval
margin_of_error <- z * (sample_sd / sqrt(n))
lower_bound <- sample_mean - margin_of_error
upper_bound <- sample_mean + margin_of_error

# Print the confidence interval
cat("95% Confidence Interval for Loan Amount:", "[", lower_bound, ",", upper_bound, "]\n")
## 95% Confidence Interval for Loan Amount: [ 4.067876 , 4.095821 ]

Conclusion:

The 95% confidence interval provides a range of values within which the true population mean (average interest rate) is likely to fall with 95% confidence. Specifically:

The interval [ 4.067876 , 4.095821 ] represents the range of interest rates within which we can be reasonably confident that the true average interest rate for the population lies.

# Sample dataset (replace with your actual dataset)
response_variable <- na.omit(Loan$Credit_Score)  # Remove rows with NA in rate_of_interest

# Calculate sample mean and standard error
sample_mean <- mean(response_variable)
sample_sd <- sd(response_variable)
n <- length(response_variable)
z <- qnorm(0.975)  # 95% confidence interval (two-tailed)

# Calculate confidence interval
margin_of_error <- z * (sample_sd / sqrt(n))
lower_bound <- sample_mean - margin_of_error
upper_bound <- sample_mean + margin_of_error

# Print the confidence interval
cat("95% Confidence Interval for Loan Amount:", "[", lower_bound, ",", upper_bound, "]\n")
## 95% Confidence Interval for Loan Amount: [ 699.1165 , 705.1217 ]

Conclusion:

The 95% confidence interval provides a range of values within which the true population mean (average credit score) is likely to fall with 95% confidence. Specifically:

The interval [ 699.1165 , 705.1217 ] represents the range of credit scores within which we can be reasonably confident that the true average credit score for the population lies.