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:

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(ggthemes)
library(ggrepel)
library(pwrss)
## 
## Attaching package: 'pwrss'
## 
## The following object is masked from 'package:stats':
## 
##     power.t.test
library(pwr)
library(glm2)
library(ggplot2)
credit_data = read.csv(("/Users/tbs/Downloads/Data Sets 'R'/credit_data.csv"))

Build a linear model to predict Credit Score

# Build a linear model to predict Credit Score using Income and Age
linear_model <- lm(`Credit.Score` ~ Income + Loan.Amount + Age, data = credit_data)

coef(linear_model)
##   (Intercept)        Income   Loan.Amount           Age 
##  5.236285e+02  8.760288e-04 -3.237853e-06 -1.669711e-01
# Summary of the linear model
summary(linear_model)
## 
## Call:
## lm(formula = Credit.Score ~ Income + Loan.Amount + Age, data = credit_data)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -304.2 -136.5    1.2  139.1  310.2 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  5.236e+02  1.096e+00 477.832  < 2e-16 ***
## Income       8.760e-04  9.376e-06  93.435  < 2e-16 ***
## Loan.Amount -3.238e-06  8.068e-06  -0.401    0.688    
## Age         -1.670e-01  2.512e-02  -6.647 2.99e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 159.1 on 279852 degrees of freedom
## Multiple R-squared:  0.04866,    Adjusted R-squared:  0.04865 
## F-statistic:  4771 on 3 and 279852 DF,  p-value: < 2.2e-16

Intercept (5.236285e+02): The intercept represents the estimated Credit Score when all other predictor variables are set to zero. In this context, it’s the estimated Credit Score for someone with zero income, zero loan amount, and zero age. Keep in mind that the intercept may not have a meaningful interpretation in this context, as it doesn’t make sense to have zero values for all these variables.

Income (8.760288e-04): For every one-unit increase in income, the model predicts an increase of approximately 0.000876 in the Credit Score. This suggests a positive relationship between income and credit score, meaning that higher income is associated with a higher credit score.

Loan Amount (-3.237853e-06): For every one-unit increase in loan amount, the model predicts a decrease of approximately 0.00000324 in the Credit Score. This suggests a negative relationship between loan amount and credit score, indicating that higher loan amounts are associated with lower credit scores.

Age (-1.669711e-01): For every one-year increase in age, the model predicts a decrease of approximately 0.166971 in the Credit Score. This suggests a negative relationship between age and credit score, meaning that older individuals tend to have slightly lower credit scores.

Assuming “linear_model” is a linear regression model, and “credit_data” is your dataset

# Residuals vs Fitted Values plot with color
plot(linear_model, which = 1, col = "blue")

# Q-Q plot with color
plot(linear_model, which = 2, col = "red")

# Cook's distance plot with color
plot(linear_model, which = 4, col = "green")

# Check for multicollinearity
cor_matrix <- cor(credit_data[, c("Income", "Loan.Amount")])
cor_matrix
##                Income Loan.Amount
## Income      1.0000000   0.3877397
## Loan.Amount 0.3877397   1.0000000
# Visualize the correlation matrix with colors
#ggplot2::heatmap(cor_matrix, col = colorRampPalette(c("blue", "white", "red"))(100))
rsquared <- summary(linear_model)$r.squared
credit_data_filtered <- credit_data %>%
  filter(State=='Karnataka')

ggplot(data = credit_data_filtered, aes(x = `Credit.Score`, y = `Loan.Amount`)) +
  geom_point(color = "orange") +
  geom_smooth(method = 'lm', color = 'blue', linetype = 'dashed', se = FALSE) +
  geom_smooth(color = 'red', se = FALSE) +
  labs(
    title = "Loan Amount vs. Profile Score",
    subtitle = paste("Linear Fit R-Squared =", round(rsquared, 3))
  ) +
  theme_classic()
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'

credit_data_filtered <- credit_data %>%
  filter(State=='Karnataka')

ggplot(data = credit_data_filtered, aes(x = `Credit.Score`, y = `Income`)) +
  geom_point(color = "orange") +
  geom_smooth(method = 'lm', color = 'blue', linetype = 'dashed', se = FALSE) +
  geom_smooth(color = 'red', se = FALSE) +
  labs(
    title = "Loan Amount vs. Profile Score",
    subtitle = paste("Linear Fit R-Squared =", round(rsquared, 3))
  ) +
  theme_classic()
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'

Interpret the coefficient for “Age”

coef_age <- coef(linear_model)["Age"]
cat("The coefficient for Age is", coef_age, "\n")
## The coefficient for Age is -0.1669711
cat("For each one-year increase in Age, the predicted Credit Score is expected to increase by", coef_age, "points.")
## For each one-year increase in Age, the predicted Credit Score is expected to increase by -0.1669711 points.