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:
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
## ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── 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(ggplot2)
credit_data = read.csv(("/Users/tbs/Downloads/Data Sets 'R'/credit_data.csv"))
In this dataset, the most valuable continuous variable is the
Loan.Amount. This represents the amount of money a customer
is seeking for a loan, which is a key factor for both lenders and
borrowers.
We’ll examine the Occupation variable as a potential
factor influencing the Loan Amount.
# Loan amount by occupation
loan_amount_by_occupation <- credit_data %>%
group_by(Occupation) %>%
summarize(mean_loan_amount = mean(Loan.Amount))
loan_amount_by_occupation
## # A tibble: 15 × 2
## Occupation mean_loan_amount
## <chr> <dbl>
## 1 Banker 106894.
## 2 Business Owner 106319.
## 3 Civil Servant 107912.
## 4 Contractor 105349.
## 5 Doctor 107943.
## 6 Farmer 105210.
## 7 Graphic Designer 100514.
## 8 Independent Consultant 103538.
## 9 Photographer 103402.
## 10 Shopkeeper 105828.
## 11 Software Engineer 107446.
## 12 Student 101385.
## 13 Teacher 107712.
## 14 Writer 103650.
## 15 <NA> 101299.
Null Hypothesis (H0): There is no significant difference in the mean loan amounts among different occupations. In other words, occupation does not have a significant impact on loan amounts.
Alternative Hypothesis (H1): At least one occupation group has a different mean loan amount compared to the others. Occupation significantly influences loan amounts.
alpha <- 0.05
anova_result <- aov(Loan.Amount ~ Occupation, data = credit_data)
summary(anova_result)
## Df Sum Sq Mean Sq F value Pr(>F)
## Occupation 13 1.051e+12 8.082e+10 49.78 <2e-16 ***
## Residuals 261570 4.247e+14 1.624e+09
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 18272 observations deleted due to missingness
As p-value is very less I am rejecting null hypothesis and accepting alternative hypothesis H1.
The ANOVA test results indicate a significant difference in loan amounts across different occupations. This suggests that occupation is a relevant factor in determining loan amounts.
library(ggplot2)
# Linear regression model with income
lm_model <- lm(Loan.Amount ~ Income, data = credit_data)
summary(lm_model)
##
## Call:
## lm(formula = Loan.Amount ~ Income, data = credit_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -103840 -31705 5042 32569 61218
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 7.781e+04 1.442e+02 539.6 <2e-16 ***
## Income 3.659e-01 1.644e-03 222.5 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 37290 on 279854 degrees of freedom
## Multiple R-squared: 0.1503, Adjusted R-squared: 0.1503
## F-statistic: 4.952e+04 on 1 and 279854 DF, p-value: < 2.2e-16
# Extract model data
model_data <- data.frame(
fitted.values = lm_model$fitted.values,
residuals = lm_model$residuals
)
# Residuals vs Fitted Values
ggplot(model_data, aes(x = fitted.values, y = residuals)) +
geom_point(color = "blue") +
labs(title = "Residuals vs Fitted Values", x = "Fitted Values", y = "Residuals")
# Normal Q-Q
qqnorm(residuals(lm_model), col = "purple")
qqline(residuals(lm_model), col = "red")
# Scale-Location
ggplot(model_data, aes(x = fitted.values, y = sqrt(abs(residuals)))) +
geom_point(color = "green") +
labs(title = "Scale-Location", x = "Fitted Values", y = "Sqrt(abs(Residuals))")
# Cook's Distance
ggplot(model_data, aes(x = 1:nrow(model_data), y = cooks.distance(lm_model))) +
geom_point(color = "orange") +
labs(title = "Cook's Distance", x = "Observation Number", y = "Cook's Distance")
The linear regression model confirms a significant positive relationship between income and loan amount. The model explains 15.03% of the variation in loan amount, and both the intercept and the “Income” coefficient are highly significant.
# Multiple regression model with income and occupation:-
lm_model_multiple <- lm(Loan.Amount ~ Income + Occupation, data = credit_data)
summary(lm_model_multiple)
##
## Call:
## lm(formula = Loan.Amount ~ Income + Occupation, data = credit_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -103420 -31597 5207 32336 62663
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 7.733e+04 2.630e+02 293.996 < 2e-16 ***
## Income 3.612e-01 1.698e-03 212.688 < 2e-16 ***
## OccupationBusiness Owner 1.475e+03 3.408e+02 4.328 1.51e-05 ***
## OccupationCivil Servant 1.059e+03 3.174e+02 3.337 0.000848 ***
## OccupationContractor 3.603e+02 3.400e+02 1.060 0.289210
## OccupationDoctor 1.082e+03 3.193e+02 3.389 0.000701 ***
## OccupationFarmer 6.953e+01 3.405e+02 0.204 0.838215
## OccupationGraphic Designer -1.191e+03 5.407e+02 -2.202 0.027652 *
## OccupationIndependent Consultant 1.664e+03 5.444e+02 3.057 0.002235 **
## OccupationPhotographer 1.155e+03 5.412e+02 2.135 0.032788 *
## OccupationShopkeeper 9.406e+02 3.386e+02 2.778 0.005464 **
## OccupationSoftware Engineer 4.048e+02 3.176e+02 1.275 0.202400
## OccupationStudent 2.032e+03 3.548e+02 5.727 1.03e-08 ***
## OccupationTeacher 7.601e+02 3.170e+02 2.398 0.016481 *
## OccupationWriter 1.512e+03 5.466e+02 2.766 0.005675 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 37210 on 261569 degrees of freedom
## (18272 observations deleted due to missingness)
## Multiple R-squared: 0.1495, Adjusted R-squared: 0.1495
## F-statistic: 3285 on 14 and 261569 DF, p-value: < 2.2e-16
The multiple regression model shows that both income and occupation have significant effects on loan amount. The model suggests that both factors should be considered when evaluating loan applications.
# Multiple regression model with income and occupation:-
lm_model_multiple <- lm(Loan.Amount ~ Income * Occupation, data = credit_data)
summary(lm_model_multiple)
##
## Call:
## lm(formula = Loan.Amount ~ Income * Occupation, data = credit_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -117725 -31506 5275 32345 64203
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 7.951e+04 4.661e+02 170.595 < 2e-16
## Income 3.346e-01 4.999e-03 66.922 < 2e-16
## OccupationBusiness Owner -1.000e+03 7.039e+02 -1.421 0.155420
## OccupationCivil Servant -6.116e+02 6.639e+02 -0.921 0.356943
## OccupationContractor -3.083e+03 7.020e+02 -4.392 1.12e-05
## OccupationDoctor 1.484e+03 6.680e+02 2.222 0.026298
## OccupationFarmer -2.478e+03 7.012e+02 -3.534 0.000409
## OccupationGraphic Designer -5.580e+03 1.090e+03 -5.119 3.07e-07
## OccupationIndependent Consultant -6.376e+03 1.102e+03 -5.787 7.19e-09
## OccupationPhotographer -5.245e+03 1.116e+03 -4.698 2.63e-06
## OccupationShopkeeper -1.656e+03 6.957e+02 -2.380 0.017308
## OccupationSoftware Engineer 3.456e+01 6.651e+02 0.052 0.958553
## OccupationStudent -8.810e+03 7.313e+02 -12.046 < 2e-16
## OccupationTeacher 6.897e+02 6.609e+02 1.044 0.296681
## OccupationWriter -3.258e+03 1.109e+03 -2.937 0.003310
## Income:OccupationBusiness Owner 3.051e-02 7.846e-03 3.888 0.000101
## Income:OccupationCivil Servant 2.040e-02 7.132e-03 2.861 0.004228
## Income:OccupationContractor 4.314e-02 7.799e-03 5.531 3.19e-08
## Income:OccupationDoctor -4.948e-03 7.175e-03 -0.690 0.490467
## Income:OccupationFarmer 3.141e-02 7.757e-03 4.049 5.13e-05
## Income:OccupationGraphic Designer 5.937e-02 1.361e-02 4.363 1.28e-05
## Income:OccupationIndependent Consultant 1.129e-01 1.370e-02 8.239 < 2e-16
## Income:OccupationPhotographer 8.782e-02 1.380e-02 6.365 1.96e-10
## Income:OccupationShopkeeper 3.209e-02 7.728e-03 4.153 3.29e-05
## Income:OccupationSoftware Engineer 4.633e-03 7.123e-03 0.650 0.515436
## Income:OccupationStudent 1.687e-01 9.506e-03 17.745 < 2e-16
## Income:OccupationTeacher 9.107e-04 7.080e-03 0.129 0.897650
## Income:OccupationWriter 6.434e-02 1.368e-02 4.704 2.55e-06
##
## (Intercept) ***
## Income ***
## OccupationBusiness Owner
## OccupationCivil Servant
## OccupationContractor ***
## OccupationDoctor *
## OccupationFarmer ***
## OccupationGraphic Designer ***
## OccupationIndependent Consultant ***
## OccupationPhotographer ***
## OccupationShopkeeper *
## OccupationSoftware Engineer
## OccupationStudent ***
## OccupationTeacher
## OccupationWriter **
## Income:OccupationBusiness Owner ***
## Income:OccupationCivil Servant **
## Income:OccupationContractor ***
## Income:OccupationDoctor
## Income:OccupationFarmer ***
## Income:OccupationGraphic Designer ***
## Income:OccupationIndependent Consultant ***
## Income:OccupationPhotographer ***
## Income:OccupationShopkeeper ***
## Income:OccupationSoftware Engineer
## Income:OccupationStudent ***
## Income:OccupationTeacher
## Income:OccupationWriter ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 37170 on 261556 degrees of freedom
## (18272 observations deleted due to missingness)
## Multiple R-squared: 0.1512, Adjusted R-squared: 0.1511
## F-statistic: 1726 on 27 and 261556 DF, p-value: < 2.2e-16
Including an interaction term tests the hypothesis that the relationship between “Income” and “Loan Amount” is different for different “Occupation” groups.
This analysis highlights the importance of income and occupation in determining loan amounts. Lenders can use this information to make informed decisions about loan approvals and interest rates.