Credit Risk Assessment using Logistic Regression: Mastering Precise Creditworthiness

Setup

We start by installing and loading the necessary packages for data manipulation, visualization, and modeling.

Introduction

This project provides an in-depth exploration of credit risk assessment at Apex Trust Bank, utilizing the statistical computing power of R to improve lending operations. My focus was on developing a robust, data-driven credit risk model to accurately predict the likelihood of loan defaults. Through detailed analysis and predictive modeling, I contributed to minimizing defaults and enhancing the bank’s financial stability. This hands-on project not only sharpened my skills in advanced analytics and risk management but also made a tangible impact on the bank’s profitability and reputation.

Business Overview/Problem

Apex Trust Bank is facing an increasing number of non-performing loans and defaults among its loan portfolio. This trend is not only negatively impacting the bank’s profitability but also eroding its reputation and financial stability. To address this critical issue and ensure the long-term sustainability of its lending operations, Apex Trust Bank needs an effective credit risk assessment solution.

The bank’s current credit risk assessment process relies heavily on traditional methods and manual underwriting, which are proving inadequate in accurately predicting the creditworthiness of loan applicants. Additionally, the bank lacks a standardized approach to assess and categorize applicants based on their credit risk, leading to inconsistent lending decisions.

Apex Trust Bank urgently needs to enhance its credit risk assessment process to reduce the number of bad loans, minimize defaults, and make more informed lending decisions. The bank is seeking a data-driven solution that leverages historical customer data to develop a robust credit risk assessment model. This model should enable the bank to categorize loan applicants as good or bad credit risks with a high degree of accuracy, thereby improving the quality of its loan portfolio and reducing financial losses.

Rationale for the Project

Logistic regression is a powerful statistical technique that’s particularly well-suited for binary classification problems, where the outcome variable has two possible states, such as ‘good credit’ or ‘bad credit’. In the context of credit risk assessment, our goal is to predict whether a loan applicant is likely to be credit-worthy or not.

Logistic regression is apt for this task as it models the relationship between the predictor variables (such as income, credit score, employment status) and the likelihood of a loan applicant falling into a specific category. Logistic regression outputs probabilities between 0 and 1, making it ideal for predicting probabilities of loan default or credit-worthiness.

Logistic regression also provides interpretable results, allowing us to understand the influence of each predictor variable on the probability of being credit-worthy. This transparency is crucial in a banking context, where regulatory compliance and understanding the factors contributing to credit risk are of paramount importance. By employing logistic regression, we can develop a robust predictive model that aids in making informed decisions regarding loan approvals, ultimately minimizing the risk of default and optimizing the bank’s lending practices

Aim of the Project

Credit risk assessment refers to the evaluation and analysis of the likelihood that a borrower or debtor will default on their financial obligations, such as repaying a loan or fulfilling a contractual agreement. It involves the comprehensive examination of various factors such as, the borrower’s credit history, financial stability, income sources, existing debts, and overall economic conditions. This assessment aims to quantify the level of risk associated with extending credit to an individual, business, or entity. By conducting a thorough credit risk assessment, lenders and financial institutions can make informed decisions about whether to grant credit, and if so, under what terms and conditions, thereby mitigating potential financial losses and ensuring the stability of their lending portfolios.

Implementing an effective credit risk assessment model is imperative for Apex Trust Bank’s long-term financial stability and reputation. This data-driven solution offers several crucial benefits. Firstly, it enables the bank to significantly reduce the number of bad loans and defaults, thereby safeguarding its profitability. Secondly, it provides a standardized approach to evaluate applicants, ensuring consistent lending decisions.

Moreover, by leveraging historical customer data, the model enhances the accuracy of creditworthiness predictions, leading to a higher quality loan portfolio. Ultimately, this case study equips Apex Trust Bank with a powerful tool to make more informed lending decisions and mitigate the risks associated with its lending operations

Data Description

Status:

This represents the status of the debtor’s checking account with the bank. It’s a categorical variable with four values, each with specific meanings:

1: No checking account

2: Balance less than 0 USD

3: Balance between 0 USD and 200 USD

4: Balance equal to or more than 200 USD

Duration:

This is the credit duration in months, measured quantitatively.

Credit History:

It describes the history of compliance with previous or concurrent credit contracts. This categorical variable has five values:

0: Delay in paying off in the past

1: Critical account/other credits elsewhere

2: No credits taken

3: Existing credits paid back duly till now

4: All credits at this bank paid back duly

Savings:

It describes the debtor’s savings and is a categorical ordinal variable with five values:

1: Unknown/no savings account

2: Savings less than 100 USD

3: Savings between 100 USD and 499 USD

4: Savings between 500 USD and 999 USD

5: Savings equal to or more than 1000 USD

Employment Duration:

This represents the duration of the debtor’s employment with their current employer. It’s an ordinal variable discretized quantitatively with the following possible values:

1: Unemployed

2: Less than 1 year

3: 1 to less than 4 years

4: 4 to less than 7 years

5: 7 years or more

Installment Rate:

It denotes credit installments as a percentage of the debtor’s disposable income. This ordinal variable discretized quantitatively has the following values:

1: 35% or more

2: Between 25% and 35%

3: Between 20% and 25%

4: Less than 20%

Other Debtors:

It indicates whether there is another debtor or a guarantor for the credit. This categorical variable has three values:

1: None

2: Co-applicant

3: Guarantor

Present Residence:

This represents the length of time in years that the debtor has lived in their current residence. It’s an ordinal variable discretized quantitatively with the following values:

1: Less than 1 year

2: 1 to less than 4 years

3: 4 to less than 7 years

4: 7 years or more

Property:

It indicates the debtor’s most valuable property. The highest applicable code is used, and if codes 3 or 4 are not applicable, code 2 is used. This ordinal variable has the following values:

1: Unknown/no property

2: Car or other

3: Building soc. savings agr./life insurance

4: Real estate

Age:

This is the age of the debtor, measured quantitatively in years.

Other Installment Plans:

It denotes installment plans from providers other than the credit-giving bank. This categorical variable has three values:

1: Bank

2: Stores

3: None

Housing:

It indicates the type of housing the debtor lives in. This categorical variable has three values:

1: For free

2: Rent

3: Own

Number of Credits:

This represents the number of credits, including the current one that the debtor has (or had) at this bank. It’s an ordinal variable discretized quantitatively with the following values:

1: 1

2: 2-3

3: 4-5

4: 6 or more

Job:

It defines the debtor’s job. This ordinal variable has the following values:

1: Unemployed/unskilled - non-resident

2: Unskilled – resident

3: Skilled employee/official

4: Manager/self-employee

People Liable:

This indicates the number of persons who financially depend on the debtor, i.e., are entitled to maintenance. It’s a binary variable discretized quantitatively:

1: 3 or more

2: 0 to 2

Telephone:

It indicates whether there is a telephone registered under the debtor’s name. This binary variable has two values:

1: No

2: Yes (under customer name)

Foreign Worker:

This binary variable indicates whether the debtor is a foreign worker:

1: Yes

2: No

Credit Risk (Label):

This is the label for the samples and indicates whether the credit contract has been complied with (good) or not (bad):

0: Bad

1: Good

Install and Load Packages

First, we install and load the necessary R packages for data manipulation, visualization, and modeling.

Data Import and Preparation

We begin by importing the dataset and preparing it for exploratory data analysis (EDA).

# import data
Apex_Trust_Dataset <- read.csv("C:/Users/Administrator/Downloads/Apex_Trust_Dataset.csv")

# duplicate the data for EDA
eda_data = Apex_Trust_Dataset

# Assign all the categorical column names to an object
cat_cols <- c("status", "credit_history", "purpose", 
              "savings", "employment_duration", "installment_rate",
              "other_debtors",
              "present_residence", "property",
              "other_installment_plans",
              "housing", "number_credits",
              "job", "people_liable",
              "telephone", "foreign_worker",
              "credit_risk")

# convert columns to factor variable
eda_data[,cat_cols] <- lapply(eda_data[,cat_cols],factor)


# rename variable values to something more meaningful for EDA
eda_data = eda_data |> mutate(credit_risk = ifelse(credit_risk == 0, 'bad', 'good'))

eda_data$status = ifelse(eda_data$status == 1, 'no checking account',
                         ifelse(eda_data$status == 2, '<0 USD',
                                ifelse(eda_data$status == 3, '0 USD >== & < 200 USD', '>200 USD')))

eda_data$status = factor(eda_data$status, levels = c('no checking account','<0 USD', '0 USD >== & < 200 USD',
                                                     '>200 USD'))

eda_data$savings = ifelse(eda_data$savings == 1, 'unknown/ no savings account',
                         ifelse(eda_data$savings == 2, '< 100 USD',
                                ifelse(eda_data$savings == 3, '100 >= & < 500 USD',
                                       ifelse(eda_data$savings == 4, '500>= & < 1000 USD', '>=1000 USD'))))

eda_data$savings = factor(eda_data$savings, levels = c('unknown/ no savings account','<100 USD', '100 >= & < 500 USD',
                                                     '500>= & < 1000 USD', '>=1000 USD'))

eda_data$credit_risk = as.factor(eda_data$credit_risk)


# check the transformed data types
str(eda_data)
## 'data.frame':    2542 obs. of  20 variables:
##  $ status                 : Factor w/ 4 levels "no checking account",..: 4 4 2 2 1 4 3 1 1 2 ...
##  $ duration               : int  76 65 87 67 96 99 25 58 32 72 ...
##  $ credit_history         : Factor w/ 5 levels "0","1","2","3",..: 4 3 2 3 5 2 3 2 5 4 ...
##  $ purpose                : Factor w/ 11 levels "0","1","2","3",..: 10 7 1 2 4 4 4 9 2 2 ...
##  $ amount                 : int  325 4825 3300 9575 5525 4750 9475 3850 9250 6125 ...
##  $ savings                : Factor w/ 5 levels "unknown/ no savings account",..: 4 NA 1 NA 1 4 NA NA 1 3 ...
##  $ employment_duration    : Factor w/ 5 levels "1","2","3","4",..: 3 4 4 3 1 2 4 2 2 2 ...
##  $ installment_rate       : Factor w/ 4 levels "1","2","3","4": 2 3 2 3 3 3 2 4 1 3 ...
##  $ other_debtors          : Factor w/ 3 levels "1","2","3": 3 2 1 3 3 1 1 1 3 2 ...
##  $ present_residence      : Factor w/ 4 levels "1","2","3","4": 2 4 4 1 4 2 2 4 3 2 ...
##  $ property               : Factor w/ 4 levels "1","2","3","4": 4 3 1 4 3 3 1 2 4 3 ...
##  $ age                    : int  53 82 52 44 23 76 52 30 34 80 ...
##  $ other_installment_plans: Factor w/ 3 levels "1","2","3": 3 2 2 1 2 1 3 2 1 3 ...
##  $ housing                : Factor w/ 3 levels "1","2","3": 3 3 2 2 3 3 3 3 2 3 ...
##  $ number_credits         : Factor w/ 4 levels "1","2","3","4": 1 4 4 2 4 3 1 3 1 4 ...
##  $ job                    : Factor w/ 4 levels "1","2","3","4": 2 1 4 2 2 4 3 2 1 2 ...
##  $ people_liable          : Factor w/ 2 levels "1","2": 1 1 1 2 1 2 1 1 1 1 ...
##  $ telephone              : Factor w/ 2 levels "1","2": 1 2 1 2 1 1 2 1 1 1 ...
##  $ foreign_worker         : Factor w/ 3 levels "0","1","2": 2 1 1 2 1 2 2 2 2 2 ...
##  $ credit_risk            : Factor w/ 2 levels "bad","good": 1 1 1 1 1 1 1 1 1 1 ...

Exploratory Data Analysis

Summary Statistics

##                    status       duration      credit_history    purpose   
##  no checking account  :636   Min.   :  2.00   0:378          3      :421  
##  <0 USD               :667   1st Qu.: 16.00   1:405          0      :346  
##  0 USD >== & < 200 USD:509   Median : 33.00   2:824          2      :301  
##  >200 USD             :730   Mean   : 41.13   3:375          1      :275  
##                              3rd Qu.: 65.75   4:560          9      :257  
##                              Max.   :100.00                  10     :184  
##                                                              (Other):758  
##      amount                             savings    employment_duration
##  Min.   :  125   unknown/ no savings account:877   1:353              
##  1st Qu.: 1382   <100 USD                   :  0   2:483              
##  Median : 2550   100 >= & < 500 USD         :364   3:656              
##  Mean   : 3540   500>= & < 1000 USD         :370   4:492              
##  3rd Qu.: 5075   >=1000 USD                 :521   5:558              
##  Max.   :18424   NA's                       :410                      
##                                                                       
##  installment_rate other_debtors present_residence property      age       
##  1:555            1:1415        1:484             1:721    Min.   :18.00  
##  2:599            2: 583        2:765             2:668    1st Qu.:27.00  
##  3:536            3: 544        3:542             3:674    Median :36.00  
##  4:852                          4:751             4:479    Mean   :40.53  
##                                                            3rd Qu.:51.00  
##                                                            Max.   :85.00  
##                                                                           
##  other_installment_plans housing  number_credits job     people_liable
##  1: 648                  1: 744   1:998          1:411   1: 969       
##  2: 542                  2:1351   2:678          2:543   2:1573       
##  3:1352                  3: 447   3:429          3:987                
##                                   4:437          4:601                
##                                                                       
##                                                                       
##                                                                       
##  telephone foreign_worker credit_risk
##  1:1387    0:785          bad :1226  
##  2:1155    1:794          good:1316  
##            2:963                     
##                                      
##                                      
##                                      
## 

Visual EDA for Categorical Variables

We visualize the distribution of the response variable and the distributions of individual variables by credit risk using histograms.

#determine the response variable distribution
credit_risk_dist <- ggplot(eda_data, aes(x= credit_risk))+
  geom_bar(width = 0.25, fill = 'darkblue') +
  theme_minimal()+
  labs(x = 'Credit Risk',
       y = 'Count',
       title = 'Distribution of Response Variable')+
  theme(plot.title = element_text(size = 17, family = "Arial", hjust = 0.5),
        plot.subtitle = element_text(size = 12, family = "Arial", hjust = 0.5),
        plot.background = element_rect(fill = "#F8F8F8"))

credit_risk_dist

Visualizing Distributions by Credit Risk

Boxplots for Numerical Variables

We use boxplots to compare the distribution of numerical variables by credit risk.

df_num = Apex_Trust_Dataset |> select(age, amount, duration)
df_num = cbind(df_num, eda_data$credit_risk)
colnames(df_num) = c('age', 'amount', 'duration', 'credit_risk')

# Age
bp1 <- ggplot(df_num, aes(age))+
  geom_boxplot(fill = "darkblue", color = "black", alpha = 0.3, width = 0.5)+
  facet_wrap(~credit_risk) + coord_flip()+
  theme_minimal()

bp1

# Duration
bp2 <- ggplot(df_num, aes(duration))+
  geom_boxplot(fill = "darkblue", color = "black", alpha = 0.3, width = 0.5)+
  facet_wrap(~credit_risk) + coord_flip()+
  theme_minimal()

bp2

# Amount
bp3 <- ggplot(df_num, aes(amount))+
  geom_boxplot(fill = "darkblue", color = "black", alpha = 0.3, width = 0.5)+
  facet_wrap(~credit_risk) + coord_flip()+
  theme_minimal()

bp3

# Arrange boxplots
grid.arrange(bp1,bp2,bp3)

Checking for Correlations

We check for correlations within the variables to identify any multicollinearity issues.

# duplicate the data and transform all factor variables to numeric
cor_data = Apex_Trust_Dataset |> mutate_if(is.factor, as.numeric)

#correlation plot
corPlot(cor_data, alpha = 0.7)

Data Preprocessing

Missing Values

We visualize the missing values in the dataset.

Model Development

Data Partitioning

We set a seed for random number generation to ensure repeatability and then partition the data into training (70%) and test (30%) sets.

#set a seed for random number generation to ensure repeatability
set.seed(1234)

#randomly partition data into training(70%) and test(30%) sets
ind <- sample(2, nrow(Apex_Trust_Dataset), replace = TRUE, prob = c(0.7, 0.3))
train <- Apex_Trust_Dataset[ind == 1,]
test <- Apex_Trust_Dataset[ind == 2,]

Logistic Regression Model

We fit a logistic regression model to predict credit risk using all other variables.

#fit a logistic regression model to predict the credit risk using all other variables
m1 <- glm(credit_risk~., data = train, family = 'binomial')

summary(m1)
## 
## Call:
## glm(formula = credit_risk ~ ., family = "binomial", data = train)
## 
## Coefficients:
##                           Estimate Std. Error z value Pr(>|z|)    
## (Intercept)              1.220e+00  5.873e-01   2.078 0.037724 *  
## status                   3.717e-01  5.494e-02   6.766 1.33e-11 ***
## duration                 1.014e-03  2.588e-03   0.392 0.695320    
## credit_history           9.165e-02  5.024e-02   1.824 0.068117 .  
## purpose                  1.064e-02  2.066e-02   0.515 0.606718    
## amount                  -4.263e-04  2.836e-05 -15.031  < 2e-16 ***
## savings                  1.234e-01  4.290e-02   2.876 0.004028 ** 
## employment_duration      1.093e-01  4.821e-02   2.267 0.023363 *  
## installment_rate        -2.338e-01  5.751e-02  -4.065 4.80e-05 ***
## other_debtors           -3.934e-03  9.004e-02  -0.044 0.965147    
## present_residence        1.330e-01  5.805e-02   2.292 0.021916 *  
## property                -3.265e-02  5.891e-02  -0.554 0.579412    
## age                     -6.255e-02  4.580e-03 -13.657  < 2e-16 ***
## other_installment_plans  2.167e-01  7.851e-02   2.760 0.005773 ** 
## housing                 -3.862e-01  1.007e-01  -3.835 0.000126 ***
## number_credits          -1.041e-02  6.427e-02  -0.162 0.871386    
## job                      2.693e-01  6.484e-02   4.152 3.29e-05 ***
## people_liable           -7.592e-02  1.365e-01  -0.556 0.577960    
## telephone                1.275e-01  1.319e-01   0.966 0.333802    
## foreign_worker           6.256e-01  1.093e-01   5.725 1.04e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 2502.8  on 1808  degrees of freedom
## Residual deviance: 1613.4  on 1789  degrees of freedom
## AIC: 1653.4
## 
## Number of Fisher Scoring iterations: 5

Feature Importance

We visualize the most important featues using the ‘vip’ package.

Predictions and Model Evaluation

We make predictions on the training and test data and evaluate the model using confusion matrices.

#predictions on the training data
p1 <- predict(m1, train, type = 'response')
pred1 <- ifelse(p1 > 0.5, 1, 0)

pred1
##    1    2    3    4    6    7    8    9   10   11   12   13   15   17   18   19 
##    1    0    0    0    0    0    0    0    0    1    0    0    0    0    0    0 
##   20   21   22   23   24   25   27   30   31   32   33   34   35   37   38   41 
##    0    0    1    1    0    0    0    0    0    0    0    1    0    1    0    0 
##   42   43   44   45   46   47   48   49   51   52   54   55   56   57   59   62 
##    0    0    0    0    0    0    0    0    1    0    0    0    0    1    0    0 
##   63   64   65   67   68   69   70   71   73   75   76   77   78   79   80   82 
##    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0 
##   83   84   85   87   88   89   91   93   94   95   96   97   98   99  101  102 
##    0    0    0    1    1    0    0    1    0    0    0    0    0    0    0    0 
##  103  104  105  106  107  108  109  110  112  114  115  118  119  125  126  127 
##    0    1    1    0    0    0    0    0    0    0    1    0    0    0    0    1 
##  128  129  130  132  133  134  136  138  139  141  143  144  145  146  148  150 
##    0    0    0    0    0    0    0    0    1    0    0    0    0    0    0    0 
##  151  152  153  155  157  159  160  161  162  163  164  165  166  167  168  170 
##    0    0    0    0    0    0    0    1    0    0    0    0    0    0    0    0 
##  172  174  175  177  178  179  180  181  182  183  184  186  188  189  191  193 
##    0    0    1    0    0    0    0    0    0    0    0    0    0    0    0    0 
##  200  201  202  203  205  207  208  209  211  212  213  215  217  219  221  222 
##    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0 
##  223  224  225  226  228  229  230  232  233  235  236  238  239  241  242  243 
##    0    1    0    0    0    1    1    0    1    0    0    0    0    0    0    0 
##  244  246  248  251  252  253  254  255  256  257  258  259  260  262  263  264 
##    0    1    0    0    0    0    0    0    0    1    0    0    0    1    0    0 
##  265  266  268  269  271  273  275  276  278  279  280  281  282  286  287  288 
##    0    0    0    1    0    0    0    0    0    0    0    0    0    1    0    0 
##  289  291  292  294  295  297  298  299  300  301  303  304  306  307  309  312 
##    0    0    0    0    0    0    0    1    0    0    0    0    0    0    1    0 
##  313  314  315  316  317  318  319  321  323  324  325  326  327  328  329  334 
##    0    0    0    0    0    1    0    0    0    0    0    0    0    0    0    0 
##  335  336  337  338  340  342  343  345  346  347  348  350  351  353  357  359 
##    0    0    0    0    0    0    1    0    0    0    0    0    0    0    0    0 
##  361  362  363  364  367  368  372  374  375  376  378  379  380  381  382  383 
##    0    0    0    0    0    1    0    1    0    0    0    0    0    0    0    0 
##  384  385  386  387  388  391  394  395  397  398  399  400  401  402  405  406 
##    0    1    0    0    1    0    0    1    0    0    0    0    0    0    0    0 
##  408  409  410  414  416  417  419  420  421  423  424  426  427  428  429  430 
##    0    0    0    0    1    0    0    0    0    0    1    0    0    0    0    0 
##  431  433  435  436  437  440  444  446  448  449  450  451  456  457  458  459 
##    0    0    1    1    1    0    0    0    0    0    0    0    1    0    0    0 
##  460  461  463  464  465  466  467  468  469  470  471  472  473  475  477  478 
##    0    1    0    0    0    0    1    0    0    0    0    0    0    0    1    1 
##  480  481  482  483  484  485  486  490  491  494  495  496  497  498  499  500 
##    0    0    0    0    0    0    1    0    0    0    0    0    0    0    1    0 
##  501  504  505  509  511  514  515  517  518  519  520  522  523  525  527  528 
##    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0 
##  529  530  531  532  533  535  537  538  540  541  542  544  546  551  552  554 
##    0    0    0    1    1    0    0    0    0    0    1    0    0    0    0    0 
##  557  558  559  561  563  565  568  569  570  571  573  574  575  576  577  578 
##    0    0    0    1    0    0    0    0    0    0    0    0    0    0    0    0 
##  579  582  583  584  585  586  587  590  593  594  595  596  597  598  599  600 
##    0    0    1    0    0    0    0    1    0    0    0    0    1    0    0    0 
##  601  602  603  604  605  606  608  609  610  611  613  614  616  617  618  620 
##    0    0    0    1    0    0    0    0    0    0    1    0    0    0    0    0 
##  622  623  625  627  629  630  632  633  634  637  639  640  641  642  643  645 
##    0    0    0    0    0    0    0    0    0    0    1    1    0    0    0    1 
##  646  647  649  651  652  654  655  656  658  659  661  664  665  666  668  669 
##    0    0    0    0    0    0    0    0    0    0    0    1    0    0    1    0 
##  671  673  674  677  679  681  682  683  684  686  688  689  690  692  693  694 
##    0    0    0    0    1    0    0    0    0    0    1    1    0    0    0    0 
##  695  696  697  698  699  701  702  704  705  706  707  708  709  710  714  715 
##    0    0    1    0    0    0    1    0    0    0    0    0    0    0    0    1 
##  716  717  718  720  722  724  725  727  728  729  732  733  734  735  737  741 
##    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0 
##  742  743  744  745  746  747  748  749  750  751  753  755  757  759  760  761 
##    0    0    0    1    0    1    1    0    0    0    0    0    0    0    0    1 
##  762  763  764  765  766  768  770  774  775  776  777  778  779  780  782  783 
##    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0 
##  784  785  786  788  790  792  793  794  796  797  799  801  803  804  805  806 
##    0    0    0    1    0    0    0    1    0    0    0    0    0    0    0    0 
##  807  808  809  810  811  812  814  815  816  817  819  820  821  822  823  824 
##    0    0    0    0    1    1    0    1    0    1    0    0    0    0    0    0 
##  825  827  828  829  830  831  835  836  838  839  840  841  842  843  844  845 
##    0    0    0    0    0    0    1    0    0    0    0    0    0    0    0    0 
##  847  851  854  855  856  857  858  861  862  863  864  865  867  869  870  874 
##    0    0    1    0    1    1    0    1    0    0    0    0    0    0    0    0 
##  876  877  878  879  880  881  882  883  884  885  887  888  889  890  893  894 
##    0    0    0    0    0    0    0    0    1    0    0    0    0    0    0    0 
##  895  896  897  898  901  902  904  906  907  908  910  911  912  913  914  915 
##    0    0    1    0    1    0    0    0    0    0    0    0    0    0    0    1 
##  916  917  921  922  923  924  925  926  928  929  930  931  933  934  935  936 
##    0    0    1    1    0    0    0    0    1    1    1    1    1    1    1    0 
##  937  938  939  940  942  945  946  947  948  949  950  953  956  958  959  960 
##    1    1    1    1    1    1    1    1    1    1    1    1    0    1    1    1 
##  961  962  965  966  967  968  969  970  973  975  976  978  980  983  984  986 
##    0    1    1    1    1    1    1    1    0    1    1    1    1    1    0    1 
##  987  989  990  991  992  994  995  996  998 1000 1002 1003 1004 1006 1007 1008 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    0    1    1 
## 1011 1013 1014 1015 1016 1017 1018 1019 1021 1022 1023 1025 1026 1028 1029 1030 
##    1    1    1    1    0    1    1    1    1    1    1    1    1    0    1    1 
## 1031 1033 1035 1036 1037 1038 1040 1042 1043 1046 1047 1048 1049 1051 1053 1054 
##    1    1    1    1    1    1    1    1    0    1    1    1    1    1    1    1 
## 1055 1056 1057 1059 1061 1062 1064 1065 1068 1069 1070 1071 1072 1073 1074 1075 
##    1    0    1    1    1    1    0    1    0    1    1    1    1    1    1    1 
## 1076 1077 1078 1079 1080 1081 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 
##    0    1    1    0    1    1    1    1    1    1    1    1    0    1    1    1 
## 1094 1095 1096 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1112 1113 
##    1    1    1    1    1    1    1    1    1    1    1    1    0    1    1    1 
## 1115 1118 1120 1121 1123 1125 1126 1127 1128 1130 1133 1134 1135 1136 1138 1139 
##    1    1    1    1    1    1    0    1    1    1    1    1    1    0    1    1 
## 1141 1142 1143 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 
##    0    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 1158 1159 1160 1161 1163 1164 1165 1167 1168 1170 1171 1172 1173 1174 1177 1178 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    0    0    1 
## 1179 1180 1181 1182 1185 1186 1188 1189 1190 1191 1192 1193 1194 1196 1197 1198 
##    1    1    1    1    0    1    1    1    1    1    1    1    1    1    1    1 
## 1199 1200 1201 1202 1204 1205 1209 1211 1212 1214 1217 1220 1221 1223 1224 1225 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    0 
## 1226 1227 1228 1229 1231 1232 1235 1237 1238 1239 1240 1242 1243 1244 1245 1246 
##    1    0    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 1247 1248 1252 1253 1255 1256 1258 1259 1262 1265 1266 1267 1268 1269 1270 1271 
##    1    1    1    1    1    1    1    1    0    0    1    1    1    1    1    0 
## 1272 1273 1274 1275 1276 1277 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 
##    1    1    1    1    1    1    1    0    1    1    1    1    1    0    1    1 
## 1291 1292 1293 1295 1297 1298 1299 1300 1301 1303 1304 1305 1307 1308 1309 1310 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    0    1 
## 1311 1312 1314 1315 1316 1317 1319 1320 1321 1325 1326 1327 1328 1329 1330 1331 
##    0    0    1    0    1    1    1    1    1    1    1    1    1    1    1    1 
## 1332 1333 1334 1335 1336 1338 1339 1340 1345 1347 1348 1349 1350 1351 1353 1354 
##    0    1    1    1    1    1    1    1    1    0    1    1    1    1    1    1 
## 1356 1357 1358 1359 1360 1361 1362 1363 1364 1366 1367 1368 1369 1370 1371 1375 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 1377 1378 1379 1380 1381 1386 1388 1389 1390 1391 1392 1394 1395 1396 1398 1401 
##    1    1    1    1    1    1    0    0    1    1    1    1    1    1    1    1 
## 1402 1405 1407 1408 1409 1410 1411 1412 1413 1414 1415 1418 1419 1420 1424 1425 
##    1    1    0    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 1427 1428 1429 1430 1431 1433 1434 1435 1436 1437 1440 1441 1442 1443 1446 1447 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1466 1467 1468 
##    1    1    1    1    0    1    1    1    1    1    1    0    1    1    1    1 
## 1469 1470 1471 1472 1475 1476 1477 1478 1479 1480 1483 1484 1486 1487 1488 1489 
##    1    1    1    1    1    0    1    1    1    1    1    1    1    1    1    1 
## 1491 1492 1494 1495 1496 1497 1498 1501 1502 1503 1507 1509 1510 1511 1512 1513 
##    1    1    1    1    1    1    1    0    1    1    1    1    1    1    1    1 
## 1515 1516 1517 1518 1519 1520 1522 1523 1524 1525 1528 1529 1530 1532 1533 1534 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 1535 1536 1537 1538 1540 1542 1543 1544 1545 1546 1548 1549 1550 1551 1552 1553 
##    1    1    1    1    1    0    1    1    1    1    0    0    1    0    1    1 
## 1555 1556 1557 1558 1559 1560 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 
##    1    0    1    1    0    1    0    1    0    1    1    0    0    1    1    1 
## 1572 1574 1575 1576 1577 1578 1579 1580 1583 1585 1587 1588 1589 1590 1591 1592 
##    0    1    1    1    1    1    1    1    1    1    0    1    1    1    1    1 
## 1594 1595 1597 1598 1599 1601 1602 1603 1604 1605 1606 1608 1609 1610 1612 1613 
##    1    1    1    0    1    1    1    1    1    1    0    1    1    1    1    1 
## 1614 1615 1616 1618 1619 1620 1625 1626 1627 1628 1630 1631 1633 1634 1635 1637 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    0 
## 1638 1639 1641 1642 1644 1645 1650 1653 1654 1655 1656 1657 1659 1661 1662 1663 
##    1    1    1    0    1    1    1    1    1    1    1    1    1    1    1    0 
## 1664 1665 1666 1668 1670 1672 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 
##    1    1    1    0    1    1    1    1    1    1    1    1    1    1    1    1 
## 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1695 1696 1698 1699 1700 1701 
##    1    1    1    1    1    0    1    1    1    1    0    1    1    1    1    0 
## 1702 1703 1704 1705 1706 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 
##    1    1    1    1    1    1    1    0    1    1    1    1    1    1    1    1 
## 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1732 1733 1735 1736 
##    1    1    1    0    1    1    0    1    1    1    1    1    1    1    0    1 
## 1737 1738 1741 1742 1743 1744 1745 1747 1748 1749 1752 1753 1758 1759 1761 1762 
##    1    1    1    1    1    0    1    1    1    1    1    1    1    1    0    1 
## 1763 1764 1765 1766 1770 1772 1773 1774 1776 1778 1781 1784 1786 1788 1789 1790 
##    1    1    1    1    1    1    1    1    0    0    1    0    1    1    1    1 
## 1792 1793 1794 1795 1796 1798 1800 1801 1802 1803 1804 1805 1806 1808 1809 1812 
##    1    1    0    1    1    1    1    0    0    1    1    1    1    1    1    1 
## 1813 1814 1815 1816 1817 1819 1821 1822 1823 1824 1827 1828 1829 1830 1831 1832 
##    1    1    1    1    1    1    1    1    1    1    0    1    1    1    1    1 
## 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1852 
##    1    1    1    0    1    1    1    1    0    1    1    1    1    1    1    0 
## 1853 1854 1856 1857 1858 1859 1861 1865 1867 1868 1871 1872 1873 1874 1878 1879 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 1880 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1895 1897 1898 1899 
##    1    1    1    1    1    1    1    1    1    1    1    1    0    1    0    0 
## 1901 1902 1903 1905 1906 1909 1912 1913 1914 1915 1916 1917 1918 1919 1920 1923 
##    1    1    1    1    1    1    1    0    1    1    1    1    1    1    1    1 
## 1924 1926 1927 1928 1931 1932 1933 1935 1936 1937 1939 1940 1941 1942 1943 1944 
##    1    1    1    1    1    1    1    1    1    1    0    1    1    0    1    1 
## 1946 1947 1948 1949 1950 1952 1953 1955 1957 1958 1959 1960 1962 1967 1968 1969 
##    1    1    0    1    1    1    1    1    1    1    1    1    1    1    1    0 
## 1970 1973 1975 1978 1979 1981 1982 1983 1984 1986 1988 1989 1990 1991 1992 1994 
##    1    1    1    1    1    1    0    1    1    1    1    1    1    1    0    1 
## 1995 1999 2000 2001 2002 2003 2004 2005 2006 2008 2010 2011 2014 2015 2016 2017 
##    0    1    1    1    1    0    0    1    0    1    1    1    1    1    1    1 
## 2019 2020 2024 2025 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 
##    1    1    1    1    1    1    1    1    1    1    1    1    0    1    1    1 
## 2039 2040 2041 2042 2043 2045 2047 2048 2049 2050 2051 2052 2054 2057 2058 2059 
##    1    1    1    1    1    1    0    1    1    1    1    1    1    1    1    1 
## 2060 2062 2063 2064 2065 2067 2068 2069 2070 2071 2072 2074 2075 2077 2078 2080 
##    1    1    1    0    1    1    0    1    1    1    1    1    1    1    1    1 
## 2081 2082 2083 2084 2085 2086 2088 2090 2092 2094 2095 2096 2097 2099 2100 2101 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    0    1    1 
## 2103 2105 2106 2107 2108 2110 2111 2112 2113 2114 2115 2117 2118 2119 2120 2122 
##    1    0    1    1    1    1    1    0    1    0    0    0    0    0    1    0 
## 2123 2125 2126 2127 2130 2131 2132 2133 2134 2135 2137 2138 2139 2140 2141 2143 
##    1    1    1    0    1    1    1    1    1    0    0    0    1    0    1    0 
## 2144 2145 2146 2147 2150 2151 2153 2155 2157 2158 2159 2160 2161 2162 2164 2165 
##    1    0    1    1    1    1    1    1    0    1    0    1    1    1    1    0 
## 2166 2167 2168 2170 2171 2172 2174 2175 2176 2177 2178 2179 2180 2181 2182 2185 
##    1    0    1    1    1    0    0    1    1    1    0    0    1    1    0    0 
## 2187 2188 2189 2190 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2207 
##    1    0    1    1    1    1    0    1    1    1    1    1    1    1    1    1 
## 2208 2209 2210 2212 2214 2215 2216 2218 2219 2221 2223 2224 2225 2226 2227 2229 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 2231 2232 2235 2238 2240 2241 2243 2244 2246 2247 2249 2250 2251 2252 2253 2254 
##    1    1    1    0    1    1    0    0    0    1    1    1    0    1    0    0 
## 2255 2258 2259 2260 2261 2263 2266 2267 2270 2273 2274 2275 2277 2278 2281 2282 
##    1    1    0    1    1    0    0    1    1    0    1    1    0    1    1    0 
## 2285 2286 2287 2288 2289 2290 2294 2296 2297 2300 2301 2302 2303 2304 2305 2307 
##    1    0    1    1    1    0    1    1    0    0    1    1    1    0    1    1 
## 2308 2310 2312 2314 2315 2316 2317 2318 2320 2322 2323 2324 2327 2328 2329 2330 
##    1    1    1    1    1    0    1    1    1    1    0    1    1    1    1    0 
## 2331 2333 2336 2339 2340 2341 2342 2343 2344 2347 2348 2349 2350 2352 2354 2355 
##    1    1    0    1    1    1    1    1    1    1    1    1    0    1    0    1 
## 2356 2357 2358 2359 2360 2361 2363 2364 2365 2368 2369 2370 2372 2373 2374 2375 
##    1    0    1    1    1    1    0    0    1    1    0    1    1    0    1    1 
## 2376 2378 2381 2382 2383 2384 2385 2386 2387 2388 2390 2391 2393 2395 2397 2398 
##    1    0    1    1    1    1    1    0    0    1    0    0    1    1    1    0 
## 2399 2400 2401 2402 2404 2407 2414 2415 2416 2417 2421 2422 2423 2424 2425 2427 
##    1    1    0    1    1    0    0    1    0    1    1    1    0    1    0    1 
## 2428 2429 2432 2434 2438 2440 2442 2443 2444 2446 2447 2449 2450 2451 2455 2456 
##    0    1    0    0    0    1    1    1    0    0    1    0    1    0    1    1 
## 2457 2458 2462 2464 2465 2466 2467 2468 2470 2471 2472 2473 2475 2476 2479 2480 
##    1    1    0    0    1    0    1    1    1    0    0    1    1    1    1    1 
## 2481 2482 2483 2484 2486 2487 2488 2489 2491 2492 2494 2495 2496 2498 2499 2500 
##    1    0    0    1    0    0    0    1    0    1    1    1    0    0    0    1 
## 2502 2503 2504 2505 2506 2507 2508 2511 2512 2513 2514 2516 2517 2519 2520 2521 
##    1    0    1    0    1    1    0    1    0    1    0    0    1    0    0    1 
## 2522 2524 2526 2527 2528 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 
##    1    1    1    0    1    1    1    1    0    0    1    0    1    0    0    0 
## 2542 
##    0
#prediction on the test data
p2 <- predict(m1, test, type = 'response')
pred2 <- ifelse(p2 > 0.5, 1, 0)

#calculate confusion matrix for test data
clmg1 <- confusionMatrix(factor(pred1), factor(train$credit_risk), positive = '1')

#print
clmg1
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction   0   1
##          0 636 127
##          1 221 825
##                                           
##                Accuracy : 0.8076          
##                  95% CI : (0.7887, 0.8256)
##     No Information Rate : 0.5263          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.6121          
##                                           
##  Mcnemar's Test P-Value : 6.186e-07       
##                                           
##             Sensitivity : 0.8666          
##             Specificity : 0.7421          
##          Pos Pred Value : 0.7887          
##          Neg Pred Value : 0.8336          
##              Prevalence : 0.5263          
##          Detection Rate : 0.4561          
##    Detection Prevalence : 0.5782          
##       Balanced Accuracy : 0.8044          
##                                           
##        'Positive' Class : 1               
## 
#calculate confusion matrix for test data
clmg2 <- confusionMatrix(factor(pred2), factor(test$credit_risk), positive = '1')

#print
clmg2
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction   0   1
##          0 265  45
##          1 104 319
##                                           
##                Accuracy : 0.7967          
##                  95% CI : (0.7657, 0.8253)
##     No Information Rate : 0.5034          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.5939          
##                                           
##  Mcnemar's Test P-Value : 2.019e-06       
##                                           
##             Sensitivity : 0.8764          
##             Specificity : 0.7182          
##          Pos Pred Value : 0.7541          
##          Neg Pred Value : 0.8548          
##              Prevalence : 0.4966          
##          Detection Rate : 0.4352          
##    Detection Prevalence : 0.5771          
##       Balanced Accuracy : 0.7973          
##                                           
##        'Positive' Class : 1               
## 

Conclusion

This project successfully demonstrated the process of developing a logistic regression model to assess credit risk at Apex Trust Bank.