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
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(tidyverse)
library(ggthemes)
library(ggrepel)
library(tidyverse)
library(ggthemes)
library(ggrepel)
library(pwr)
library(pwrss)
##
## Attaching package: 'pwrss'
##
## The following object is masked from 'package:stats':
##
## power.t.test
Setting Loan amount as response variable here,and loan amount as explanatory variable from my Loan dataset . Performing Anova test by considering the below null hypothesis, Null Hypothesis (H0): There is no significant difference in the mean loan amounts across different loan purposes. Alternative Hypothesis (Ha): There is a significant difference in the mean loan amounts across different loan purposes.
Considering Alpha value as 0.05,
library(dplyr)
# ANOVA model
loan_anova <- aov(loan_amount ~ loan_purpose, data = Loan)
summary(loan_anova)
## Df Sum Sq Mean Sq F value Pr(>F)
## loan_purpose 3 9.869e+12 3.290e+12 109.6 <2e-16 ***
## Residuals 5613 1.684e+14 3.001e+10
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##Interpretation: Here i have considered significance value as 0.05, and p value is less than 0.05, so there is enough evidence to reject null hypothesis,and to accept alternate hypothesis. Now, based on this result, we can conclude that there is a highly significant difference in mean loan amounts across the different loan purposes. In other words, the type of loan purpose has a significant impact on the loan amounts granted.
#Adding another continuous variable credit score
# Fit a linear regression model
linear_model <- lm(loan_amount ~ Credit_Score, data = Loan)
summary(linear_model)
##
## Call:
## lm(formula = loan_amount ~ Credit_Score, data = Loan)
##
## Residuals:
## Min 1Q Median 3Q Max
## -312618 -131696 -30837 108043 2667706
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 342737.794 14734.471 23.261 <2e-16 ***
## Credit_Score -6.361 20.711 -0.307 0.759
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 178200 on 5615 degrees of freedom
## Multiple R-squared: 1.68e-05, Adjusted R-squared: -0.0001613
## F-statistic: 0.09434 on 1 and 5615 DF, p-value: 0.7587
#Summary: For the linear regression model, the “Credit Score” does not appear to be a statistically significant predictor of “Loan Amount,” as indicated by the high p-value. The model, as a whole, does not explain much variance in “Loan Amount,” as indicated by the low R-squared values and F-statistic. Therefore, “Credit Score” may not be a strong predictor of “Loan Amount” in this dataset.
# Diagnostic plots
par(mfrow = c(2, 2)) # Setting up a 2x2 grid for plots
plot(linear_model)
# Hypothesis test for "Credit Score" coefficient
coef_summary <- coef(summary(linear_model))
p_value_credit_score <- coef_summary["Credit_Score", "Pr(>|t|)"]
# Output results
print(paste("p-value for Credit Score coefficient:", p_value_credit_score))
## [1] "p-value for Credit Score coefficient: 0.758740270789169"
we have plotted Residuals vs. Fitted: To check for linearity and homoscedasticity. Normal Q-Q: To assess whether residuals are normally distributed. Scale-Location (or Spread-Location): To check for constant variance of residuals. Residuals vs. Leverage: To identify potential outliers or influential data points
The p-value for the “Credit Score” coefficient is 0.7587, which is substantially higher than the conventional significance level of 0.05. This suggests that the “Credit Score” variable is not a statistically significant predictor of “Loan Amount” in the context of my data. In other words, there is not enough evidence to conclude that “Credit Score” has a meaningful effect on the loan amounts granted.
Here’s an interpretation of the coefficients and their relevance to the context of the data:
Intercept (342,737.794): The intercept represents the estimated average loan amount when the “Credit Score” is zero. However, this value is not interpretable in this context because it does not make sense for credit scores to be zero.
Credit Score Coefficient (-6.361): This coefficient suggests that, on average, for each one-unit increase in “Credit Score,” the loan amount decreases by 6.361 units. However, the high p-value indicates that this relationship is not statistically significant, meaning that changes in credit scores do not provide meaningful information about changes in loan amounts.
#Adding anothe variable Loan purpose into regression model This will allow us to assess how the combination of “Credit Score” and “Loan Purpose” affects “Loan Amount.”
# Fit a linear regression model with both 'Credit_Score' and 'Loan_Purpose'
updated_linear_model <- lm(loan_amount ~ Credit_Score + loan_purpose, data = Loan)
summary(updated_linear_model)
##
## Call:
## lm(formula = loan_amount ~ Credit_Score + loan_purpose, data = Loan)
##
## Residuals:
## Min 1Q Median 3Q Max
## -328131 -129702 -29660 101521 2649549
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 356275.69 14843.43 24.002 < 2e-16 ***
## Credit_Score 1.09 20.14 0.054 0.957
## loan_purposep2 -130269.99 16877.13 -7.719 1.38e-14 ***
## loan_purposep3 -60687.36 5875.84 -10.328 < 2e-16 ***
## loan_purposep4 27807.55 6186.07 4.495 7.09e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 173200 on 5612 degrees of freedom
## Multiple R-squared: 0.05535, Adjusted R-squared: 0.05467
## F-statistic: 82.2 on 4 and 5612 DF, p-value: < 2.2e-16
##Evaluation:
Credit Score Alone: The coefficient for Credit Score is not statistically significant in this model. This implies that, in the presence of Loan Purpose, Credit Score alone may not be a strong predictor of Loan Amount.
Loan Purpose: “Loan Purpose” variables (p2, p3, and p4) are all statistically significant predictors of Loan Amount. This suggests that the reason for taking a loan has a significant impact on the loan amount granted.
Model Fit: The adjusted R-squared is slightly higher in the updated model, indicating a slightly better fit. However, the overall R-squared value is still relatively low, suggesting that the model does not explain a large portion of the variance in Loan Amount.
##Adding intercation term: It’s relevant to include an interaction term because we might hypothesize that the relationship between “Credit Score” and “Loan Amount” is different for various “Loan Purpose” categories.
# Fit a linear regression model with an interaction term
interaction_model <- lm(loan_amount ~ Credit_Score * loan_purpose, data = Loan)
# Summarize the interaction model
summary(interaction_model)
##
## Call:
## lm(formula = loan_amount ~ Credit_Score * loan_purpose, data = Loan)
##
## Residuals:
## Min 1Q Median 3Q Max
## -322951 -127334 -29325 102217 2649624
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 355617.71 28504.29 12.476 <2e-16 ***
## Credit_Score 2.03 40.18 0.051 0.9597
## loan_purposep2 -189989.02 96979.62 -1.959 0.0502 .
## loan_purposep3 -32719.99 36426.95 -0.898 0.3691
## loan_purposep4 -448.62 38019.16 -0.012 0.9906
## Credit_Score:loan_purposep2 85.92 137.30 0.626 0.5315
## Credit_Score:loan_purposep3 -39.63 51.20 -0.774 0.4389
## Credit_Score:loan_purposep4 40.40 53.61 0.754 0.4511
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 173200 on 5609 degrees of freedom
## Multiple R-squared: 0.0559, Adjusted R-squared: 0.05472
## F-statistic: 47.44 on 7 and 5609 DF, p-value: < 2.2e-16
The inclusion of an interaction term is motivated by the need to capture the nuanced, context-specific relationships between “Credit Score,” “Loan Purpose,” and “Loan Amount.” It allows us to account for and potentially leverage the heterogeneity in these relationships to make more precise recommendations for lending practices, catering to borrowers’ individual needs and objectives.