Load the libraries

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## ######################### Warning from 'xts' package ##########################
## #                                                                             #
## # The dplyr lag() function breaks how base R's lag() function is supposed to  #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or       #
## # source() into this session won't work correctly.                            #
## #                                                                             #
## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop           #
## # dplyr from breaking base R's lag() function.                                #
## #                                                                             #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning.  #
## #                                                                             #
## ###############################################################################
## 
## Attaching package: 'xts'
## The following objects are masked from 'package:dplyr':
## 
##     first, last
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(PerformanceAnalytics)
## 
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
## 
##     legend
library(caret)
## Loading required package: lattice
library(caTools)
library(kableExtra)
## 
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
## 
##     group_rows
library(tidyr)
library(scales)

Load the colors

p_white <- "#FFFFFF"
p_green <- "#1FBF10"
p_blue <- "#5478DC"
p_red <- "#FF4F62"
p_purple <- "purple"
p_yellow <- "yellow"

Export the data

Credit_data <- read.csv("/Users/Dani/Desktop/default of credit card clients1.csv")
Credit_data %>%
  glimpse()
## Rows: 30,000
## Columns: 25
## $ ID                         <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, …
## $ LIMIT_BAL                  <int> 20000, 120000, 90000, 50000, 50000, 50000, …
## $ SEX                        <int> 2, 2, 2, 2, 1, 1, 1, 2, 2, 1, 2, 2, 2, 1, 1…
## $ EDUCATION                  <int> 2, 2, 2, 2, 2, 1, 1, 2, 3, 3, 3, 1, 2, 2, 1…
## $ MARRIAGE                   <int> 1, 2, 2, 1, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2…
## $ AGE                        <int> 24, 26, 34, 37, 57, 37, 29, 23, 28, 35, 34,…
## $ PAY_0                      <int> 2, -1, 0, 0, -1, 0, 0, 0, 0, -2, 0, -1, -1,…
## $ PAY_2                      <int> 2, 2, 0, 0, 0, 0, 0, -1, 0, -2, 0, -1, 0, 2…
## $ PAY_3                      <int> -1, 0, 0, 0, -1, 0, 0, -1, 2, -2, 2, -1, -1…
## $ PAY_4                      <int> -1, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, -1, -1, …
## $ PAY_5                      <int> -2, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, -1, -1, …
## $ PAY_6                      <int> -2, 2, 0, 0, 0, 0, 0, -1, 0, -1, -1, 2, -1,…
## $ BILL_AMT1                  <int> 3913, 2682, 29239, 46990, 8617, 64400, 3679…
## $ BILL_AMT2                  <int> 3102, 1725, 14027, 48233, 5670, 57069, 4120…
## $ BILL_AMT3                  <int> 689, 2682, 13559, 49291, 35835, 57608, 4450…
## $ BILL_AMT4                  <int> 0, 3272, 14331, 28314, 20940, 19394, 542653…
## $ BILL_AMT5                  <int> 0, 3455, 14948, 28959, 19146, 19619, 483003…
## $ BILL_AMT6                  <int> 0, 3261, 15549, 29547, 19131, 20024, 473944…
## $ PAY_AMT1                   <int> 0, 0, 1518, 2000, 2000, 2500, 55000, 380, 3…
## $ PAY_AMT2                   <int> 689, 1000, 1500, 2019, 36681, 1815, 40000, …
## $ PAY_AMT3                   <int> 0, 1000, 1000, 1200, 10000, 657, 38000, 0, …
## $ PAY_AMT4                   <int> 0, 1000, 1000, 1100, 9000, 1000, 20239, 581…
## $ PAY_AMT5                   <int> 0, 0, 1000, 1069, 689, 1000, 13750, 1687, 1…
## $ PAY_AMT6                   <int> 0, 2000, 5000, 1000, 679, 800, 13770, 1542,…
## $ default.payment.next.month <int> 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0…
Credit <- lm(default.payment.next.month ~ LIMIT_BAL + SEX + EDUCATION + MARRIAGE + AGE + PAY_0 + PAY_2 + PAY_3 + PAY_4 + PAY_5 + PAY_6 + BILL_AMT1 + BILL_AMT2 + BILL_AMT3 + BILL_AMT4 + BILL_AMT5 + BILL_AMT6 + PAY_AMT1 + PAY_AMT2 + PAY_AMT3 + PAY_AMT4 + PAY_AMT5 + PAY_AMT6, data = Credit_data)
summary(Credit)
## 
## Call:
## lm(formula = default.payment.next.month ~ LIMIT_BAL + SEX + EDUCATION + 
##     MARRIAGE + AGE + PAY_0 + PAY_2 + PAY_3 + PAY_4 + PAY_5 + 
##     PAY_6 + BILL_AMT1 + BILL_AMT2 + BILL_AMT3 + BILL_AMT4 + BILL_AMT5 + 
##     BILL_AMT6 + PAY_AMT1 + PAY_AMT2 + PAY_AMT3 + PAY_AMT4 + PAY_AMT5 + 
##     PAY_AMT6, data = Credit_data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.29527 -0.24079 -0.16177  0.03385  1.30480 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  3.142e-01  1.791e-02  17.541  < 2e-16 ***
## LIMIT_BAL   -9.053e-08  2.159e-08  -4.193 2.76e-05 ***
## SEX         -1.453e-02  4.642e-03  -3.130  0.00175 ** 
## EDUCATION   -1.513e-02  3.012e-03  -5.022 5.15e-07 ***
## MARRIAGE    -2.382e-02  4.768e-03  -4.996 5.88e-07 ***
## AGE          1.409e-03  2.749e-04   5.128 2.95e-07 ***
## PAY_0        9.571e-02  2.766e-03  34.596  < 2e-16 ***
## PAY_2        1.946e-02  3.339e-03   5.828 5.68e-09 ***
## PAY_3        1.167e-02  3.585e-03   3.256  0.00113 ** 
## PAY_4        3.362e-03  3.974e-03   0.846  0.39755    
## PAY_5        5.699e-03  4.304e-03   1.324  0.18545    
## PAY_6        7.920e-04  3.521e-03   0.225  0.82201    
## BILL_AMT1   -6.225e-07  1.141e-07  -5.453 4.98e-08 ***
## BILL_AMT2    1.587e-07  1.603e-07   0.990  0.32225    
## BILL_AMT3    3.005e-08  1.510e-07   0.199  0.84222    
## BILL_AMT4   -6.793e-08  1.573e-07  -0.432  0.66587    
## BILL_AMT5   -2.049e-08  1.845e-07  -0.111  0.91159    
## BILL_AMT6    1.153e-07  1.460e-07   0.789  0.42998    
## PAY_AMT1    -7.437e-07  1.770e-07  -4.201 2.67e-05 ***
## PAY_AMT2    -2.092e-07  1.457e-07  -1.436  0.15095    
## PAY_AMT3    -2.874e-08  1.689e-07  -0.170  0.86492    
## PAY_AMT4    -2.521e-07  1.839e-07  -1.371  0.17047    
## PAY_AMT5    -3.410e-07  1.908e-07  -1.787  0.07393 .  
## PAY_AMT6    -9.770e-08  1.365e-07  -0.716  0.47422    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3886 on 29976 degrees of freedom
## Multiple R-squared:  0.124,  Adjusted R-squared:  0.1233 
## F-statistic: 184.5 on 23 and 29976 DF,  p-value: < 2.2e-16
cor(Credit_data)
##                                       ID   LIMIT_BAL           SEX
## ID                          1.0000000000  0.02617916  0.0184974942
## LIMIT_BAL                   0.0261791577  1.00000000  0.0247552351
## SEX                         0.0184974942  0.02475524  1.0000000000
## EDUCATION                   0.0391767348 -0.21916070  0.0142319362
## MARRIAGE                   -0.0290794253 -0.10813941 -0.0313888401
## AGE                         0.0186777345  0.14471280 -0.0908736465
## PAY_0                      -0.0305749323 -0.27121433 -0.0576428789
## PAY_2                      -0.0112148683 -0.29638210 -0.0707710032
## PAY_3                      -0.0184935538 -0.28612295 -0.0660960564
## PAY_4                      -0.0027348898 -0.26746001 -0.0601732384
## PAY_5                      -0.0221992319 -0.24941139 -0.0550638850
## PAY_6                      -0.0202701341 -0.23519540 -0.0440077882
## BILL_AMT1                   0.0193886739  0.28542986 -0.0336418696
## BILL_AMT2                   0.0179818975  0.27831436 -0.0311834722
## BILL_AMT3                   0.0243540500  0.28323578 -0.0245633117
## BILL_AMT4                   0.0403506029  0.29398762 -0.0218796791
## BILL_AMT5                   0.0167053032  0.29556234 -0.0170051802
## BILL_AMT6                   0.0167296183  0.29038895 -0.0167331263
## PAY_AMT1                    0.0097424435  0.19523592 -0.0002424546
## PAY_AMT2                    0.0084061203  0.17840795 -0.0013909097
## PAY_AMT3                    0.0391505355  0.21016675 -0.0085966249
## PAY_AMT4                    0.0077931297  0.20324241 -0.0022289715
## PAY_AMT5                    0.0006521878  0.21720243 -0.0016671618
## PAY_AMT6                    0.0029997807  0.21959537 -0.0027660223
## default.payment.next.month -0.0139519548 -0.15351988 -0.0399605777
##                                EDUCATION     MARRIAGE         AGE       PAY_0
## ID                          0.0391767348 -0.029079425  0.01867773 -0.03057493
## LIMIT_BAL                  -0.2191606982 -0.108139410  0.14471280 -0.27121433
## SEX                         0.0142319362 -0.031388840 -0.09087365 -0.05764288
## EDUCATION                   1.0000000000 -0.143464340  0.17506066  0.10536400
## MARRIAGE                   -0.1434643404  1.000000000 -0.41416992  0.01991719
## AGE                         0.1750606615 -0.414169918  1.00000000 -0.03944738
## PAY_0                       0.1053639979  0.019917190 -0.03944738  1.00000000
## PAY_2                       0.1215655525  0.024199067 -0.05014778  0.67216438
## PAY_3                       0.1140249029  0.032687673 -0.05304844  0.57424509
## PAY_4                       0.1087934562  0.033121549 -0.04972167  0.53884063
## PAY_5                       0.0975201606  0.035629170 -0.05382598  0.50942606
## PAY_6                       0.0823159864  0.034344766 -0.04877343  0.47455309
## BILL_AMT1                   0.0235811807 -0.023471802  0.05623853  0.18706843
## BILL_AMT2                   0.0187487708 -0.021601780  0.05428307  0.18985911
## BILL_AMT3                   0.0130024007 -0.024909451  0.05370971  0.17978528
## BILL_AMT4                  -0.0004513453 -0.023343847  0.05135309  0.17912477
## BILL_AMT5                  -0.0075665030 -0.025393398  0.04934505  0.18063463
## BILL_AMT6                  -0.0090989547 -0.021206825  0.04761268  0.17698030
## PAY_AMT1                   -0.0374561839 -0.005979000  0.02614668 -0.07926871
## PAY_AMT2                   -0.0300381866 -0.008092703  0.02178489 -0.07010052
## PAY_AMT3                   -0.0399431409 -0.003541352  0.02924735 -0.07056084
## PAY_AMT4                   -0.0382181661 -0.012659279  0.02137901 -0.06400489
## PAY_AMT5                   -0.0403584549 -0.001204757  0.02284997 -0.05818989
## PAY_AMT6                   -0.0371999031 -0.006640942  0.01947815 -0.05867321
## default.payment.next.month  0.0280060777 -0.024339216  0.01388983  0.32479373
##                                  PAY_2        PAY_3        PAY_4        PAY_5
## ID                         -0.01121487 -0.018493554 -0.002734890 -0.022199232
## LIMIT_BAL                  -0.29638210 -0.286122954 -0.267460010 -0.249411395
## SEX                        -0.07077100 -0.066096056 -0.060173238 -0.055063885
## EDUCATION                   0.12156555  0.114024903  0.108793456  0.097520161
## MARRIAGE                    0.02419907  0.032687673  0.033121549  0.035629170
## AGE                        -0.05014778 -0.053048437 -0.049721674 -0.053825976
## PAY_0                       0.67216438  0.574245093  0.538840627  0.509426064
## PAY_2                       1.00000000  0.766551683  0.662067131  0.622780245
## PAY_3                       0.76655168  1.000000000  0.777358873  0.686774511
## PAY_4                       0.66206713  0.777358873  1.000000000  0.819835311
## PAY_5                       0.62278025  0.686774511  0.819835311  1.000000000
## PAY_6                       0.57550086  0.632683593  0.716449482  0.816900160
## BILL_AMT1                   0.23488653  0.208472889  0.202812069  0.206683964
## BILL_AMT2                   0.23525694  0.237294534  0.225816316  0.226913267
## BILL_AMT3                   0.22414586  0.227494327  0.244983138  0.243334681
## BILL_AMT4                   0.22223651  0.227202281  0.245917204  0.271915007
## BILL_AMT5                   0.22134837  0.225145193  0.242902058  0.269783090
## BILL_AMT6                   0.21940335  0.222326737  0.239154130  0.262509252
## PAY_AMT1                   -0.08070071  0.001294814 -0.009362136 -0.006088757
## PAY_AMT2                   -0.05899000 -0.066793396 -0.001943657 -0.003191332
## PAY_AMT3                   -0.05590123 -0.053310780 -0.069235204  0.009062363
## PAY_AMT4                   -0.04685841 -0.046066534 -0.043461430 -0.058298865
## PAY_AMT5                   -0.03709308 -0.035863071 -0.033589535 -0.033336504
## PAY_AMT6                   -0.03650038 -0.035861084 -0.026565088 -0.023027451
## default.payment.next.month  0.26355120  0.235252514  0.216613637  0.204148914
##                                   PAY_6   BILL_AMT1   BILL_AMT2   BILL_AMT3
## ID                         -0.020270134  0.01938867  0.01798190  0.02435405
## LIMIT_BAL                  -0.235195399  0.28542986  0.27831436  0.28323578
## SEX                        -0.044007788 -0.03364187 -0.03118347 -0.02456331
## EDUCATION                   0.082315986  0.02358118  0.01874877  0.01300240
## MARRIAGE                    0.034344766 -0.02347180 -0.02160178 -0.02490945
## AGE                        -0.048773426  0.05623853  0.05428307  0.05370971
## PAY_0                       0.474553086  0.18706843  0.18985911  0.17978528
## PAY_2                       0.575500862  0.23488653  0.23525694  0.22414586
## PAY_3                       0.632683593  0.20847289  0.23729453  0.22749433
## PAY_4                       0.716449482  0.20281207  0.22581632  0.24498314
## PAY_5                       0.816900160  0.20668396  0.22691327  0.24333468
## PAY_6                       1.000000000  0.20737313  0.22692443  0.24118115
## BILL_AMT1                   0.207373131  1.00000000  0.95148367  0.89227853
## BILL_AMT2                   0.226924431  0.95148367  1.00000000  0.92832626
## BILL_AMT3                   0.241181146  0.89227853  0.92832626  1.00000000
## BILL_AMT4                   0.266356069  0.86027219  0.89248229  0.92396946
## BILL_AMT5                   0.290893745  0.82977861  0.85977831  0.88390970
## BILL_AMT6                   0.285090991  0.80265019  0.83159356  0.85332009
## PAY_AMT1                   -0.001496289  0.14027728  0.28036536  0.24433524
## PAY_AMT2                   -0.005223268  0.09935503  0.10085088  0.31693598
## PAY_AMT3                    0.005833774  0.15688714  0.15071819  0.13001118
## PAY_AMT4                    0.019017863  0.15830253  0.14739810  0.14340460
## PAY_AMT5                   -0.046433641  0.16702571  0.15795741  0.17971235
## PAY_AMT6                   -0.025299338  0.17934112  0.17425617  0.18232597
## default.payment.next.month  0.186866362 -0.01964420 -0.01419322 -0.01407552
##                                BILL_AMT4    BILL_AMT5    BILL_AMT6
## ID                          0.0403506029  0.016705303  0.016729618
## LIMIT_BAL                   0.2939876237  0.295562338  0.290388951
## SEX                        -0.0218796791 -0.017005180 -0.016733126
## EDUCATION                  -0.0004513453 -0.007566503 -0.009098955
## MARRIAGE                   -0.0233438469 -0.025393398 -0.021206825
## AGE                         0.0513530932  0.049345048  0.047612678
## PAY_0                       0.1791247706  0.180634625  0.176980296
## PAY_2                       0.2222365116  0.221348373  0.219403351
## PAY_3                       0.2272022807  0.225145193  0.222326737
## PAY_4                       0.2459172042  0.242902058  0.239154130
## PAY_5                       0.2719150070  0.269783090  0.262509252
## PAY_6                       0.2663560690  0.290893745  0.285090991
## BILL_AMT1                   0.8602721890  0.829778606  0.802650189
## BILL_AMT2                   0.8924822913  0.859778307  0.831593559
## BILL_AMT3                   0.9239694566  0.883909697  0.853320091
## BILL_AMT4                   1.0000000000  0.940134404  0.900940955
## BILL_AMT5                   0.9401344041  1.000000000  0.946196807
## BILL_AMT6                   0.9009409548  0.946196807  1.000000000
## PAY_AMT1                    0.2330118522  0.217030824  0.199965006
## PAY_AMT2                    0.2075637290  0.181246491  0.172662937
## PAY_AMT3                    0.3000225291  0.252304862  0.233769793
## PAY_AMT4                    0.1301914078  0.293118463  0.250236825
## PAY_AMT5                    0.1604330376  0.141574180  0.307728890
## PAY_AMT6                    0.1776369942  0.164184450  0.115494167
## default.payment.next.month -0.0101564959 -0.006760464 -0.005372315
##                                 PAY_AMT1     PAY_AMT2     PAY_AMT3     PAY_AMT4
## ID                          0.0097424435  0.008406120  0.039150535  0.007793130
## LIMIT_BAL                   0.1952359152  0.178407954  0.210166748  0.203242410
## SEX                        -0.0002424546 -0.001390910 -0.008596625 -0.002228972
## EDUCATION                  -0.0374561839 -0.030038187 -0.039943141 -0.038218166
## MARRIAGE                   -0.0059789998 -0.008092703 -0.003541352 -0.012659279
## AGE                         0.0261466791  0.021784893  0.029247353  0.021379006
## PAY_0                      -0.0792687112 -0.070100521 -0.070560838 -0.064004889
## PAY_2                      -0.0807007098 -0.058989999 -0.055901231 -0.046858412
## PAY_3                       0.0012948143 -0.066793396 -0.053310780 -0.046066534
## PAY_4                      -0.0093621358 -0.001943657 -0.069235204 -0.043461430
## PAY_5                      -0.0060887571 -0.003191332  0.009062363 -0.058298865
## PAY_6                      -0.0014962890 -0.005223268  0.005833774  0.019017863
## BILL_AMT1                   0.1402772776  0.099355035  0.156887138  0.158302533
## BILL_AMT2                   0.2803653570  0.100850878  0.150718194  0.147398104
## BILL_AMT3                   0.2443352376  0.316935977  0.130011182  0.143404605
## BILL_AMT4                   0.2330118522  0.207563729  0.300022529  0.130191408
## BILL_AMT5                   0.2170308238  0.181246491  0.252304862  0.293118463
## BILL_AMT6                   0.1999650062  0.172662937  0.233769793  0.250236825
## PAY_AMT1                    1.0000000000  0.285575529  0.252191139  0.199557931
## PAY_AMT2                    0.2855755287  1.000000000  0.244770450  0.180106744
## PAY_AMT3                    0.2521911390  0.244770450  1.000000000  0.216325092
## PAY_AMT4                    0.1995579312  0.180106744  0.216325092  1.000000000
## PAY_AMT5                    0.1484592750  0.180907753  0.159213720  0.151830436
## PAY_AMT6                    0.1857352554  0.157633916  0.162740033  0.157833916
## default.payment.next.month -0.0729294878 -0.058578707 -0.056250351 -0.056827401
##                                 PAY_AMT5     PAY_AMT6
## ID                          0.0006521878  0.002999781
## LIMIT_BAL                   0.2172024324  0.219595369
## SEX                        -0.0016671618 -0.002766022
## EDUCATION                  -0.0403584549 -0.037199903
## MARRIAGE                   -0.0012047569 -0.006640942
## AGE                         0.0228499736  0.019478153
## PAY_0                      -0.0581898859 -0.058673214
## PAY_2                      -0.0370930798 -0.036500376
## PAY_3                      -0.0358630706 -0.035861084
## PAY_4                      -0.0335895347 -0.026565088
## PAY_5                      -0.0333365042 -0.023027451
## PAY_6                      -0.0464336411 -0.025299338
## BILL_AMT1                   0.1670257121  0.179341122
## BILL_AMT2                   0.1579574116  0.174256166
## BILL_AMT3                   0.1797123504  0.182325966
## BILL_AMT4                   0.1604330376  0.177636994
## BILL_AMT5                   0.1415741800  0.164184450
## BILL_AMT6                   0.3077288895  0.115494167
## PAY_AMT1                    0.1484592750  0.185735255
## PAY_AMT2                    0.1809077526  0.157633916
## PAY_AMT3                    0.1592137203  0.162740033
## PAY_AMT4                    0.1518304358  0.157833916
## PAY_AMT5                    1.0000000000  0.154895525
## PAY_AMT6                    0.1548955253  1.000000000
## default.payment.next.month -0.0551235156 -0.053183340
##                            default.payment.next.month
## ID                                       -0.013951955
## LIMIT_BAL                                -0.153519876
## SEX                                      -0.039960578
## EDUCATION                                 0.028006078
## MARRIAGE                                 -0.024339216
## AGE                                       0.013889834
## PAY_0                                     0.324793728
## PAY_2                                     0.263551202
## PAY_3                                     0.235252514
## PAY_4                                     0.216613637
## PAY_5                                     0.204148914
## PAY_6                                     0.186866362
## BILL_AMT1                                -0.019644197
## BILL_AMT2                                -0.014193218
## BILL_AMT3                                -0.014075518
## BILL_AMT4                                -0.010156496
## BILL_AMT5                                -0.006760464
## BILL_AMT6                                -0.005372315
## PAY_AMT1                                 -0.072929488
## PAY_AMT2                                 -0.058578707
## PAY_AMT3                                 -0.056250351
## PAY_AMT4                                 -0.056827401
## PAY_AMT5                                 -0.055123516
## PAY_AMT6                                 -0.053183340
## default.payment.next.month                1.000000000

Credit allocation

Credit <- lm(default.payment.next.month ~ LIMIT_BAL + SEX + EDUCATION + MARRIAGE + AGE + PAY_0 + PAY_2 + PAY_3 + BILL_AMT1 + PAY_AMT1, data = Credit_data)
summary(Credit)
## 
## Call:
## lm(formula = default.payment.next.month ~ LIMIT_BAL + SEX + EDUCATION + 
##     MARRIAGE + AGE + PAY_0 + PAY_2 + PAY_3 + BILL_AMT1 + PAY_AMT1, 
##     data = Credit_data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.30307 -0.23949 -0.16138  0.03274  1.26179 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  3.133e-01  1.791e-02  17.495  < 2e-16 ***
## LIMIT_BAL   -1.105e-07  2.082e-08  -5.309 1.11e-07 ***
## SEX         -1.429e-02  4.640e-03  -3.080  0.00207 ** 
## EDUCATION   -1.528e-02  3.011e-03  -5.075 3.91e-07 ***
## MARRIAGE    -2.403e-02  4.767e-03  -5.041 4.67e-07 ***
## AGE          1.412e-03  2.749e-04   5.138 2.79e-07 ***
## PAY_0        9.737e-02  2.740e-03  35.537  < 2e-16 ***
## PAY_2        2.007e-02  3.306e-03   6.071 1.28e-09 ***
## PAY_3        1.785e-02  2.983e-03   5.983 2.22e-09 ***
## BILL_AMT1   -4.587e-07  3.445e-08 -13.315  < 2e-16 ***
## PAY_AMT1    -7.858e-07  1.399e-07  -5.616 1.97e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3887 on 29989 degrees of freedom
## Multiple R-squared:  0.1232, Adjusted R-squared:  0.1229 
## F-statistic: 421.4 on 10 and 29989 DF,  p-value: < 2.2e-16
coef(Credit)
##   (Intercept)     LIMIT_BAL           SEX     EDUCATION      MARRIAGE 
##  3.133375e-01 -1.105301e-07 -1.428981e-02 -1.527764e-02 -2.402916e-02 
##           AGE         PAY_0         PAY_2         PAY_3     BILL_AMT1 
##  1.412348e-03  9.736761e-02  2.007169e-02  1.784680e-02 -4.587157e-07 
##      PAY_AMT1 
## -7.858450e-07
coefficients <- coef(Credit)
equation <- paste("Y =", round(coefficients[1], 10))

for (i in 2:length(coefficients)) {
  equation <- paste(equation, "+", round(coefficients[i], 10), "*", names(coefficients)[i])
}

print(equation)
## [1] "Y = 0.3133375065 + -1.105e-07 * LIMIT_BAL + -0.0142898065 * SEX + -0.0152776372 * EDUCATION + -0.0240291606 * MARRIAGE + 0.0014123482 * AGE + 0.0973676108 * PAY_0 + 0.0200716876 * PAY_2 + 0.0178468039 * PAY_3 + -4.587e-07 * BILL_AMT1 + -7.858e-07 * PAY_AMT1"
ggplot(data = Credit, aes(x = LIMIT_BAL + SEX + EDUCATION + MARRIAGE + AGE + PAY_0 + PAY_2 + PAY_3 + BILL_AMT1 + PAY_AMT1, y = default.payment.next.month)) +
  geom_point(size = 1,
             alpha = 0.6) +
  geom_smooth(method = "lm", se = FALSE, color = "red") +
  labs(title = "Modelo de regresión lineal")
## `geom_smooth()` using formula = 'y ~ x'
plot of chunk unnamed-chunk-5
Credit %>%
  ggplot(aes(x = LIMIT_BAL,
             y = AGE,
             col = factor(default.payment.next.month)))+
  geom_point(size = 1,
             alpha = 0.6)+
  labs(title = "Credit data",
       subtitle = "Default By AGE",
       x = "Amount of credit given",
       y = "AGE")+
  scale_color_manual(values = c(p_red, p_blue))
plot of chunk unnamed-chunk-6
Credit %>%
  ggplot(aes(x = LIMIT_BAL,
             y = PAY_0,
             col = factor(default.payment.next.month)))+
  geom_point(size = 1,
             alpha = 0.6)+
  labs(title = "Credit data",
       subtitle = "Default By Repayment Status",
       x = "Amount of credit given",
       y = "Repayment Status (Months)")+
  scale_color_manual(values = c(p_green, p_purple))
plot of chunk unnamed-chunk-6

Logistic regression

Credit <- lm(default.payment.next.month ~ PAY_0 + PAY_2 + PAY_3, data = Credit_data)
summary(Credit)
## 
## Call:
## lm(formula = default.payment.next.month ~ PAY_0 + PAY_2 + PAY_3, 
##     data = Credit_data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.22712 -0.22775 -0.22775  0.03408  1.03408 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.227746   0.002292  99.358  < 2e-16 ***
## PAY_0       0.097776   0.002738  35.705  < 2e-16 ***
## PAY_2       0.018332   0.003277   5.594 2.24e-08 ***
## PAY_3       0.014807   0.002964   4.995 5.92e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3916 on 29996 degrees of freedom
## Multiple R-squared:   0.11,  Adjusted R-squared:  0.1099 
## F-statistic:  1235 on 3 and 29996 DF,  p-value: < 2.2e-16