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(ggthemes)
library(ggrepel)
library(pwr)
library(pwrss)
## 
## Attaching package: 'pwrss'
## 
## The following object is masked from 'package:stats':
## 
##     power.t.test
library(broom)
library(lindia)
library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## 
## The following object is masked from 'package:dplyr':
## 
##     recode
## 
## The following object is masked from 'package:purrr':
## 
##     some

###Creating new column as binary_approv_in_adv by converting approv_in_adv categorical column to binary column.

Loan$binary_approv_in_adv <- ifelse(Loan$approv_in_adv == "pre", 1, 0)
View(Loan)
# A logistic regression model with two explanatory variable
Mymodel <- glm(binary_approv_in_adv ~ Credit_Score + income, data = Loan, family = binomial)


coef(Mymodel)
##   (Intercept)  Credit_Score        income 
## -1.641815e+00  7.005385e-05 -5.986545e-07
summary(Mymodel)
## 
## Call:
## glm(formula = binary_approv_in_adv ~ Credit_Score + income, family = binomial, 
##     data = Loan)
## 
## Coefficients:
##                Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  -1.642e+00  2.261e-01  -7.260 3.87e-13 ***
## Credit_Score  7.005e-05  3.106e-04   0.226    0.822    
## income       -5.987e-07  6.048e-06  -0.099    0.921    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 5093.1  on 5616  degrees of freedom
## Residual deviance: 5093.1  on 5614  degrees of freedom
## AIC: 5099.1
## 
## Number of Fisher Scoring iterations: 3

Interpretation:

Intercept (-1.642e+00): This is the estimated intercept of the logistic regression model. In the context of logistic regression, it represents the estimated log-odds of the response variable being 1 (approval in advance) when all other explanatory variables are set to zero. In my case, it suggests the log-odds of approval in advance when Credit_Score and income are both zero. It seems like it may not helpful for my data.

Credit_Score (7.005e-05): This is the estimated coefficient for the “Credit_Score” variable. It represents the change in the log-odds of approval in advance for a one-unit change in Credit_Score while holding all other variables constant. In my case, it’s a very small positive value, which suggests that as Credit_Score increases by one unit, the log-odds of approval in advance also increase slightly. However, the p-value for this coefficient is 0.822, which is quite high, indicating that Credit_Score is not statistically significant in predicting the binary outcome.

income (-5.987e-07): This is the estimated coefficient for the “income” variable. It represents the change in the log-odds of approval in advance for a one-unit change in income while holding all other variables constant. In my case, it’s a very small negative value, suggesting that as income increases by one unit, the log-odds of approval in advance decrease slightly. Like Credit_Score, the p-value for this coefficient is high (0.921), indicating that income is not statistically significant in predicting the binary outcome.

In summary, based on the provided results, it seems that neither Credit_Score nor income is statistically significant in predicting loan approval in advance. This is because the p-values for both variables are much higher than the commonly used significance levels (e.g., 0.05). Therefore, the coefficients for these variables are not meaningful in predicting the outcome variable, and I may need to consider a different set of explanatory variables or further evaluate the model to improve its predictive power.

#Finding CI using standard_error
coefficient_estimate <- coef(Mymodel)["Credit_Score"]
standard_error <- summary(Mymodel)$coef["Credit_Score", "Std. Error"]

# Considering my desired confidence level 
confidence_level <- 0.95

# Calculating the margin of error
z <- qnorm(1 - (1 - confidence_level) / 2)  # Z-score for the confidence level
margin_of_error <- z * standard_error

# Calculating the confidence interval
lower_limit <- coefficient_estimate - margin_of_error
upper_limit <- coefficient_estimate + margin_of_error

# Print the confidence interval
cat("95% Confidence Interval for Credit_Score Coefficient: (", lower_limit, ", ", upper_limit, ")\n")
## 95% Confidence Interval for Credit_Score Coefficient: ( -0.0005387632 ,  0.0006788709 )

Interpretation:

It tells that we can be 95% confident that the true effect of the “Credit_Score” variable on the log-odds of approval in advance falls within the calculated interval.

###

Creating scatter plot for Loanamount and Income attributes from my dataset Loan, and taking the records with a condition of binary_approv_in_adv == 1.

 Mymodel <- lm(loan_amount ~ income,
            filter(Loan, binary_approv_in_adv == 1))

rsquared <- summary(Mymodel)$r.squared
print(rsquared)
## [1] 0.240075
Loan |> 
  filter(binary_approv_in_adv == 1) |>
  ggplot(mapping = aes(x = loan_amount, 
                       y = income)) +
  geom_point() +
  geom_smooth(method = 'lm', color = 'gray', linetype = 'dashed', 
              se = FALSE) +
  geom_smooth(se = FALSE) +
  labs(title = "loan amount vs. income",
       subtitle = paste("Linear Fit R-Squared =", round(rsquared, 3))) +
  theme_classic()
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Interpretation:

A linear relationship between “loan_amount” and “income” means that changes in income have a constant effect on the loan amount, and the relationship can be adequately captured by a linear model, such as simple linear regression.Based on the linear relationship observed in the scatter plot and the absence of heteroscedasticity, we can conclude that further transformations of these variables may not be necessary for modeling the relationship between “loan_amount” and “income.” A linear regression model can be used to understand how changes in income affect loan amounts, and the model’s coefficients will provide insights into the relationship.