my-vignette

library(autoScorecard)

#Quick Modeling

##step 1: Import Data
accepts <- read.csv(system.file("extdata", "accepts.csv", package = "autoScorecard" ))

##step 2: Create scorecard
##Considering efficiency and readability, many parameters of this automatic modeling function 
##are default, which requires high-precision modeling and needs to be manually established step by step
auto_scorecard1 <- auto_scorecard( feature = accepts[1:2000,], key_var= "application_id",
                                   y_var = "bad_ind",sample_rate = 0.7, points0 = 600, odds0=1/20, pdo = 50,
                                   max_depth =3,tree_p = 0.1, missing_rate = 0, single_var_rate = 1, iv_set=0.02,
                                   char_to_number = TRUE , na.omit = TRUE)
#> Start:  AIC=814.44
#> bad_ind ~ application_id + vehicle_year + vehicle_make + tot_derog + 
#>     tot_tr + age_oldest_tr + tot_open_tr + tot_rev_tr + tot_rev_debt + 
#>     tot_rev_line + rev_util + fico_score + purch_price + msrp + 
#>     down_pyt + loan_term + loan_amt + ltv + tot_income + veh_mileage
#> 
#>                  Df Deviance    AIC
#> - tot_rev_line    1   772.48 812.48
#> - tot_tr          1   772.52 812.52
#> - tot_open_tr     1   772.67 812.67
#> - application_id  1   772.76 812.76
#> - vehicle_year    1   773.05 813.05
#> - down_pyt        1   773.64 813.64
#> - tot_derog       1   774.19 814.19
#> <none>                772.44 814.44
#> - purch_price     1   775.14 815.14
#> - vehicle_make    1   775.43 815.43
#> - tot_income      1   775.75 815.75
#> - loan_amt        1   775.92 815.92
#> - veh_mileage     1   777.18 817.18
#> - age_oldest_tr   1   779.56 819.56
#> - loan_term       1   779.72 819.72
#> - msrp            1   779.94 819.94
#> - tot_rev_tr      1   780.48 820.48
#> - rev_util        1   785.17 825.17
#> - ltv             1   787.28 827.28
#> - tot_rev_debt    1   787.73 827.73
#> - fico_score      1   795.95 835.95
#> 
#> Step:  AIC=812.48
#> bad_ind ~ application_id + vehicle_year + vehicle_make + tot_derog + 
#>     tot_tr + age_oldest_tr + tot_open_tr + tot_rev_tr + tot_rev_debt + 
#>     rev_util + fico_score + purch_price + msrp + down_pyt + loan_term + 
#>     loan_amt + ltv + tot_income + veh_mileage
#> 
#>                  Df Deviance    AIC
#> - tot_tr          1   772.53 810.53
#> - tot_open_tr     1   772.74 810.74
#> - application_id  1   772.80 810.80
#> - vehicle_year    1   773.07 811.07
#> - down_pyt        1   773.69 811.69
#> <none>                772.48 812.48
#> - tot_derog       1   774.82 812.82
#> - purch_price     1   775.19 813.19
#> - vehicle_make    1   775.45 813.45
#> - tot_income      1   775.99 813.99
#> - loan_amt        1   776.02 814.02
#> + tot_rev_line    1   772.44 814.44
#> - veh_mileage     1   777.22 815.22
#> - loan_term       1   779.77 817.77
#> - msrp            1   780.05 818.05
#> - age_oldest_tr   1   780.26 818.26
#> - tot_rev_tr      1   780.65 818.65
#> - rev_util        1   785.62 823.62
#> - ltv             1   787.29 825.29
#> - tot_rev_debt    1   789.46 827.46
#> - fico_score      1   796.14 834.14
#> 
#> Step:  AIC=810.53
#> bad_ind ~ application_id + vehicle_year + vehicle_make + tot_derog + 
#>     age_oldest_tr + tot_open_tr + tot_rev_tr + tot_rev_debt + 
#>     rev_util + fico_score + purch_price + msrp + down_pyt + loan_term + 
#>     loan_amt + ltv + tot_income + veh_mileage
#> 
#>                  Df Deviance    AIC
#> - tot_open_tr     1   772.75 808.75
#> - application_id  1   772.86 808.86
#> - vehicle_year    1   773.15 809.15
#> - down_pyt        1   773.72 809.72
#> <none>                772.53 810.53
#> - tot_derog       1   775.07 811.07
#> - purch_price     1   775.30 811.30
#> - vehicle_make    1   775.51 811.51
#> - tot_income      1   776.00 812.00
#> - loan_amt        1   776.07 812.07
#> + tot_tr          1   772.48 812.48
#> + tot_rev_line    1   772.52 812.52
#> - veh_mileage     1   777.22 813.22
#> - loan_term       1   779.81 815.81
#> - msrp            1   780.11 816.11
#> - tot_rev_tr      1   780.76 816.76
#> - age_oldest_tr   1   781.50 817.50
#> - rev_util        1   785.65 821.65
#> - ltv             1   787.35 823.35
#> - tot_rev_debt    1   789.83 825.83
#> - fico_score      1   796.19 832.19
#> 
#> Step:  AIC=808.75
#> bad_ind ~ application_id + vehicle_year + vehicle_make + tot_derog + 
#>     age_oldest_tr + tot_rev_tr + tot_rev_debt + rev_util + fico_score + 
#>     purch_price + msrp + down_pyt + loan_term + loan_amt + ltv + 
#>     tot_income + veh_mileage
#> 
#>                  Df Deviance    AIC
#> - application_id  1   773.10 807.10
#> - vehicle_year    1   773.36 807.36
#> - down_pyt        1   773.92 807.92
#> <none>                772.75 808.75
#> - tot_derog       1   775.30 809.30
#> - purch_price     1   775.55 809.55
#> - vehicle_make    1   775.73 809.73
#> - tot_income      1   776.30 810.30
#> - loan_amt        1   776.46 810.46
#> + tot_open_tr     1   772.53 810.53
#> + tot_rev_line    1   772.71 810.71
#> + tot_tr          1   772.74 810.74
#> - veh_mileage     1   777.51 811.51
#> - loan_term       1   780.03 814.03
#> - msrp            1   780.50 814.50
#> - age_oldest_tr   1   781.85 815.85
#> - tot_rev_tr      1   784.44 818.44
#> - rev_util        1   785.67 819.67
#> - ltv             1   787.42 821.42
#> - tot_rev_debt    1   791.24 825.24
#> - fico_score      1   796.50 830.50
#> 
#> Step:  AIC=807.1
#> bad_ind ~ vehicle_year + vehicle_make + tot_derog + age_oldest_tr + 
#>     tot_rev_tr + tot_rev_debt + rev_util + fico_score + purch_price + 
#>     msrp + down_pyt + loan_term + loan_amt + ltv + tot_income + 
#>     veh_mileage
#> 
#>                  Df Deviance    AIC
#> - vehicle_year    1   773.73 805.73
#> - down_pyt        1   774.34 806.34
#> <none>                773.10 807.10
#> - tot_derog       1   775.62 807.62
#> - purch_price     1   775.89 807.89
#> - vehicle_make    1   776.06 808.06
#> - tot_income      1   776.66 808.66
#> - loan_amt        1   776.70 808.70
#> + application_id  1   772.75 808.75
#> + tot_open_tr     1   772.86 808.86
#> + tot_rev_line    1   773.07 809.07
#> + tot_tr          1   773.09 809.09
#> - veh_mileage     1   777.84 809.84
#> - loan_term       1   780.55 812.55
#> - msrp            1   780.89 812.89
#> - age_oldest_tr   1   782.05 814.05
#> - tot_rev_tr      1   784.74 816.74
#> - rev_util        1   785.92 817.92
#> - ltv             1   787.60 819.60
#> - tot_rev_debt    1   791.56 823.56
#> - fico_score      1   796.90 828.90
#> 
#> Step:  AIC=805.73
#> bad_ind ~ vehicle_make + tot_derog + age_oldest_tr + tot_rev_tr + 
#>     tot_rev_debt + rev_util + fico_score + purch_price + msrp + 
#>     down_pyt + loan_term + loan_amt + ltv + tot_income + veh_mileage
#> 
#>                  Df Deviance    AIC
#> - down_pyt        1   775.04 805.04
#> <none>                773.73 805.73
#> - tot_derog       1   776.30 806.30
#> - vehicle_make    1   776.57 806.57
#> - purch_price     1   776.66 806.66
#> - tot_income      1   777.00 807.00
#> + vehicle_year    1   773.10 807.10
#> + application_id  1   773.36 807.36
#> - loan_amt        1   777.49 807.49
#> + tot_open_tr     1   773.49 807.49
#> + tot_tr          1   773.71 807.71
#> + tot_rev_line    1   773.71 807.71
#> - veh_mileage     1   777.95 807.95
#> - loan_term       1   780.88 810.88
#> - msrp            1   781.17 811.17
#> - age_oldest_tr   1   782.61 812.61
#> - tot_rev_tr      1   785.37 815.37
#> - rev_util        1   786.69 816.69
#> - ltv             1   788.20 818.20
#> - tot_rev_debt    1   792.24 822.24
#> - fico_score      1   797.08 827.08
#> 
#> Step:  AIC=805.04
#> bad_ind ~ vehicle_make + tot_derog + age_oldest_tr + tot_rev_tr + 
#>     tot_rev_debt + rev_util + fico_score + purch_price + msrp + 
#>     loan_term + loan_amt + ltv + tot_income + veh_mileage
#> 
#>                  Df Deviance    AIC
#> <none>                775.04 805.04
#> - tot_derog       1   777.64 805.64
#> + down_pyt        1   773.73 805.73
#> - vehicle_make    1   777.87 805.87
#> - tot_income      1   777.97 805.97
#> - purch_price     1   778.26 806.26
#> + vehicle_year    1   774.34 806.34
#> + application_id  1   774.58 806.58
#> + tot_open_tr     1   774.81 806.81
#> - loan_amt        1   778.94 806.94
#> + tot_rev_line    1   775.00 807.00
#> + tot_tr          1   775.03 807.03
#> - veh_mileage     1   779.81 807.81
#> - loan_term       1   782.22 810.22
#> - msrp            1   782.69 810.69
#> - age_oldest_tr   1   784.15 812.15
#> - tot_rev_tr      1   786.75 814.75
#> - rev_util        1   788.07 816.07
#> - ltv             1   790.64 818.64
#> - tot_rev_debt    1   794.01 822.01
#> - fico_score      1   798.38 826.38

#> Warning in ggplot2::geom_bar(stat = "identity", color = "black", position =
#> ggplot2::position_dodge(), : Ignoring unknown parameters: `size`
#> Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
#> ℹ Please use the `linewidth` argument instead.
#> ℹ The deprecated feature was likely used in the autoScorecard package.
#>   Please report the issue to the authors.
#> This warning is displayed once per session.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
#> Warning: The `size` argument of `element_rect()` is deprecated as of ggplot2 3.4.0.
#> ℹ Please use the `linewidth` argument instead.
#> ℹ The deprecated feature was likely used in the autoScorecard package.
#>   Please report the issue to the authors.
#> This warning is displayed once per session.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.






#Step-by-Step  Modeling

##step 1: Import Data
accepts <- read.csv(system.file("extdata", "accepts.csv", package = "autoScorecard" ))



##step 2: Data Description
data_detect1 <- data_detect( df = accepts, key_var = c("application_id","account_number") ,
                    y_var = "bad_ind" )

head(data_detect1)
#>         variable     class nrow missing_rate unique_count identical_rate min
#> 1   vehicle_year   integer 5845 0.0001710864           21    0.356372968   0
#> 2   vehicle_make character 5845 0.0000000000          155    0.190248075  NA
#> 3 bankruptcy_ind character 5845 0.0000000000            3    0.886227545  NA
#> 4      tot_derog   integer 5845 0.0364414029           30    0.488793841   0
#> 5         tot_tr   integer 5845 0.0364414029           68    0.039349872   0
#> 6  age_oldest_tr   integer 5845 0.0369546621          461    0.007869974   1
#>    p25  p50  p75  max        mean         sd        cv
#> 1 1997 1999 2000 9999 1901.793634 488.024392 0.2566127
#> 2   NA   NA   NA   NA          NA         NA        NA
#> 3   NA   NA   NA   NA          NA         NA        NA
#> 4    0    0    2   32    1.910156   3.274744 1.7143852
#> 5    9   16   24   77   17.084695  10.814056 0.6329675
#> 6   78  137  205  588  154.304317  99.940540 0.6476847


##step 3: Data Filtering
feature<- filter_var( df = accepts , key_var = c("application_id","account_number"), y_var = "bad_ind" , 
                      missing_rate = 0 , single_var_rate = 1 , 
                      iv_set = 0.02, char_to_number = TRUE , na.omit = TRUE )



##step 4: Select Training Sample 
d = sort( sample( nrow( feature ), nrow( feature )*0.7))
train <- feature[d,]
test  <- feature[-d,]




##step 5: Data Distribution Comparison
comparison_two_data( df1 = train , df2 = test ,key_var = c("application_id","account_number"), y_var="bad_ind")





##step 6: Automatic binning of data
##Decision Tree Binning
treebins_train <- bins_tree(df= train, key_var= c("application_id","account_number"), y_var="bad_ind", 
                            max_depth=3, p=0.1)


##Equal Frequency Binning
binning_eqfreq1 <- binning_eqfreq( df= train, feat= 'tot_derog', label = 'bad_ind', nbins = 3)


##Equal Width Binning
binning_eqwid1 <- binning_eqwid( df = train, feat = 'tot_derog', label = 'bad_ind', nbins = 3)



##The K-means Binning
binning_kmean1 <- binning_kmean( df = train, feat= 'loan_term', label = 'bad_ind', nbins = 3)



##Chi-Square Binning
bins_chim1 <- bins_chim( df = train[1:200,], key_var = "application_id", y_var = "bad_ind" , alpha=0.1 )



##Unsupervised Automatic Binning Function
f_1 <-bins_unsupervised(  df = feature[1:200,] , id="application_id" , label="bad_ind" ,
                          methods = c("k_means", "equal_width","equal_freq")  ,  bin_nums=5  )



##The Combination of Two Bins Produces the Best Binning Result
best1 <- best_iv( df=f_1 ,bin=c('bins') , method = c('method') ,variable= c( "variable" ) ,label_iv='miv'  )
vs1   <- best_vs( df1 = treebins_train[,-c(3)], df2 = best1[,-c(1:2)] ,variable="variable" ,label_iv='miv' )





##step 7: Replace Feature Data by Binning Template
woe_train <- rep_woe(  df= train ,key_var="application_id", y_var="bad_ind"  , tool=treebins_train ,
                       var_label= "variable",col_woe='woe', lower='lower' ,upper ='upper'  )


woe_test <-  rep_woe(  df= test ,key_var="application_id", y_var="bad_ind"  , tool=treebins_train ,
                       var_label= "variable",col_woe='woe', lower='lower' ,upper ='upper'  )


##step 8: Modeling
lg<-stats::glm(bad_ind~.,family=stats::binomial(link='logit'),data= woe_train)

lg_both<-stats::step(lg,direction = "both")
#> Start:  AIC=2519.9
#> bad_ind ~ application_id + account_number + vehicle_year + vehicle_make + 
#>     tot_derog + tot_tr + age_oldest_tr + tot_open_tr + tot_rev_tr + 
#>     tot_rev_debt + tot_rev_line + rev_util + fico_score + purch_price + 
#>     msrp + down_pyt + loan_term + loan_amt + ltv + tot_income + 
#>     veh_mileage
#> 
#>                  Df Deviance    AIC
#> - account_number  1   2475.9 2517.9
#> - vehicle_year    1   2475.9 2517.9
#> - tot_rev_tr      1   2476.2 2518.2
#> - application_id  1   2476.3 2518.3
#> - msrp            1   2476.3 2518.3
#> - purch_price     1   2476.4 2518.4
#> - tot_income      1   2477.2 2519.2
#> - loan_term       1   2477.3 2519.3
#> <none>                2475.9 2519.9
#> - tot_open_tr     1   2478.2 2520.2
#> - tot_rev_line    1   2478.4 2520.4
#> - vehicle_make    1   2478.8 2520.8
#> - down_pyt        1   2479.1 2521.1
#> - tot_rev_debt    1   2479.3 2521.3
#> - loan_amt        1   2479.7 2521.7
#> - rev_util        1   2480.2 2522.2
#> - tot_tr          1   2481.3 2523.3
#> - veh_mileage     1   2481.4 2523.4
#> - tot_derog       1   2483.1 2525.1
#> - age_oldest_tr   1   2484.8 2526.8
#> - ltv             1   2535.1 2577.1
#> - fico_score      1   2568.8 2610.8
#> 
#> Step:  AIC=2517.91
#> bad_ind ~ application_id + vehicle_year + vehicle_make + tot_derog + 
#>     tot_tr + age_oldest_tr + tot_open_tr + tot_rev_tr + tot_rev_debt + 
#>     tot_rev_line + rev_util + fico_score + purch_price + msrp + 
#>     down_pyt + loan_term + loan_amt + ltv + tot_income + veh_mileage
#> 
#>                  Df Deviance    AIC
#> - vehicle_year    1   2476.0 2516.0
#> - tot_rev_tr      1   2476.2 2516.2
#> - msrp            1   2476.3 2516.3
#> - application_id  1   2476.3 2516.3
#> - purch_price     1   2476.4 2516.4
#> - tot_income      1   2477.3 2517.3
#> - loan_term       1   2477.3 2517.3
#> <none>                2475.9 2517.9
#> - tot_open_tr     1   2478.2 2518.2
#> - tot_rev_line    1   2478.4 2518.4
#> - vehicle_make    1   2478.8 2518.8
#> - down_pyt        1   2479.1 2519.1
#> - tot_rev_debt    1   2479.3 2519.3
#> - loan_amt        1   2479.7 2519.7
#> + account_number  1   2475.9 2519.9
#> - rev_util        1   2480.2 2520.2
#> - tot_tr          1   2481.3 2521.3
#> - veh_mileage     1   2481.5 2521.5
#> - tot_derog       1   2483.1 2523.1
#> - age_oldest_tr   1   2484.8 2524.8
#> - ltv             1   2535.3 2575.3
#> - fico_score      1   2568.8 2608.8
#> 
#> Step:  AIC=2515.96
#> bad_ind ~ application_id + vehicle_make + tot_derog + tot_tr + 
#>     age_oldest_tr + tot_open_tr + tot_rev_tr + tot_rev_debt + 
#>     tot_rev_line + rev_util + fico_score + purch_price + msrp + 
#>     down_pyt + loan_term + loan_amt + ltv + tot_income + veh_mileage
#> 
#>                  Df Deviance    AIC
#> - tot_rev_tr      1   2476.2 2514.2
#> - msrp            1   2476.4 2514.4
#> - purch_price     1   2476.4 2514.4
#> - application_id  1   2476.4 2514.4
#> - tot_income      1   2477.3 2515.3
#> - loan_term       1   2477.4 2515.4
#> <none>                2476.0 2516.0
#> - tot_open_tr     1   2478.3 2516.3
#> - tot_rev_line    1   2478.4 2516.4
#> - vehicle_make    1   2479.1 2517.1
#> - down_pyt        1   2479.1 2517.1
#> - tot_rev_debt    1   2479.3 2517.3
#> - loan_amt        1   2479.8 2517.8
#> + vehicle_year    1   2475.9 2517.9
#> + account_number  1   2475.9 2517.9
#> - rev_util        1   2480.2 2518.2
#> - tot_tr          1   2481.3 2519.3
#> - veh_mileage     1   2482.2 2520.2
#> - tot_derog       1   2483.2 2521.2
#> - age_oldest_tr   1   2484.9 2522.9
#> - ltv             1   2535.3 2573.3
#> - fico_score      1   2569.1 2607.1
#> 
#> Step:  AIC=2514.22
#> bad_ind ~ application_id + vehicle_make + tot_derog + tot_tr + 
#>     age_oldest_tr + tot_open_tr + tot_rev_debt + tot_rev_line + 
#>     rev_util + fico_score + purch_price + msrp + down_pyt + loan_term + 
#>     loan_amt + ltv + tot_income + veh_mileage
#> 
#>                  Df Deviance    AIC
#> - msrp            1   2476.6 2512.6
#> - purch_price     1   2476.6 2512.6
#> - application_id  1   2476.6 2512.6
#> - tot_income      1   2477.5 2513.5
#> - loan_term       1   2477.6 2513.6
#> <none>                2476.2 2514.2
#> - tot_open_tr     1   2478.4 2514.4
#> - tot_rev_line    1   2479.2 2515.2
#> - vehicle_make    1   2479.3 2515.3
#> - down_pyt        1   2479.4 2515.4
#> - tot_rev_debt    1   2479.8 2515.8
#> + tot_rev_tr      1   2476.0 2516.0
#> - loan_amt        1   2480.1 2516.1
#> + vehicle_year    1   2476.2 2516.2
#> + account_number  1   2476.2 2516.2
#> - rev_util        1   2480.2 2516.2
#> - tot_tr          1   2481.6 2517.6
#> - veh_mileage     1   2482.5 2518.5
#> - tot_derog       1   2483.3 2519.3
#> - age_oldest_tr   1   2485.2 2521.2
#> - ltv             1   2535.8 2571.8
#> - fico_score      1   2569.2 2605.2
#> 
#> Step:  AIC=2512.61
#> bad_ind ~ application_id + vehicle_make + tot_derog + tot_tr + 
#>     age_oldest_tr + tot_open_tr + tot_rev_debt + tot_rev_line + 
#>     rev_util + fico_score + purch_price + down_pyt + loan_term + 
#>     loan_amt + ltv + tot_income + veh_mileage
#> 
#>                  Df Deviance    AIC
#> - application_id  1   2477.0 2511.0
#> - purch_price     1   2477.3 2511.3
#> - tot_income      1   2477.7 2511.7
#> - loan_term       1   2478.0 2512.0
#> <none>                2476.6 2512.6
#> - tot_open_tr     1   2478.8 2512.8
#> - tot_rev_line    1   2479.5 2513.5
#> - vehicle_make    1   2479.6 2513.6
#> - down_pyt        1   2479.7 2513.7
#> - tot_rev_debt    1   2480.2 2514.2
#> + msrp            1   2476.2 2514.2
#> - loan_amt        1   2480.3 2514.3
#> + tot_rev_tr      1   2476.4 2514.4
#> + vehicle_year    1   2476.6 2514.6
#> + account_number  1   2476.6 2514.6
#> - rev_util        1   2480.7 2514.7
#> - tot_tr          1   2482.0 2516.0
#> - veh_mileage     1   2482.7 2516.7
#> - tot_derog       1   2483.7 2517.7
#> - age_oldest_tr   1   2485.6 2519.6
#> - ltv             1   2535.8 2569.8
#> - fico_score      1   2569.5 2603.5
#> 
#> Step:  AIC=2511.01
#> bad_ind ~ vehicle_make + tot_derog + tot_tr + age_oldest_tr + 
#>     tot_open_tr + tot_rev_debt + tot_rev_line + rev_util + fico_score + 
#>     purch_price + down_pyt + loan_term + loan_amt + ltv + tot_income + 
#>     veh_mileage
#> 
#>                  Df Deviance    AIC
#> - purch_price     1   2477.7 2509.7
#> - tot_income      1   2478.1 2510.1
#> - loan_term       1   2478.4 2510.4
#> <none>                2477.0 2511.0
#> - tot_open_tr     1   2479.2 2511.2
#> - tot_rev_line    1   2479.9 2511.9
#> - vehicle_make    1   2479.9 2511.9
#> - down_pyt        1   2480.1 2512.1
#> + application_id  1   2476.6 2512.6
#> - tot_rev_debt    1   2480.6 2512.6
#> + msrp            1   2476.6 2512.6
#> - loan_amt        1   2480.7 2512.7
#> + tot_rev_tr      1   2476.8 2512.8
#> + vehicle_year    1   2477.0 2513.0
#> + account_number  1   2477.0 2513.0
#> - rev_util        1   2481.1 2513.1
#> - tot_tr          1   2482.5 2514.5
#> - veh_mileage     1   2483.1 2515.1
#> - tot_derog       1   2484.0 2516.0
#> - age_oldest_tr   1   2485.9 2517.9
#> - ltv             1   2536.9 2568.9
#> - fico_score      1   2570.1 2602.1
#> 
#> Step:  AIC=2509.73
#> bad_ind ~ vehicle_make + tot_derog + tot_tr + age_oldest_tr + 
#>     tot_open_tr + tot_rev_debt + tot_rev_line + rev_util + fico_score + 
#>     down_pyt + loan_term + loan_amt + ltv + tot_income + veh_mileage
#> 
#>                  Df Deviance    AIC
#> - tot_income      1   2478.6 2508.6
#> - loan_term       1   2478.9 2508.9
#> <none>                2477.7 2509.7
#> - tot_open_tr     1   2479.9 2509.9
#> - vehicle_make    1   2480.4 2510.4
#> - down_pyt        1   2480.4 2510.4
#> - tot_rev_line    1   2480.6 2510.6
#> - loan_amt        1   2481.0 2511.0
#> + purch_price     1   2477.0 2511.0
#> + msrp            1   2477.0 2511.0
#> - tot_rev_debt    1   2481.3 2511.3
#> + application_id  1   2477.3 2511.3
#> + tot_rev_tr      1   2477.5 2511.5
#> + vehicle_year    1   2477.7 2511.7
#> + account_number  1   2477.7 2511.7
#> - rev_util        1   2481.9 2511.9
#> - tot_tr          1   2483.0 2513.0
#> - veh_mileage     1   2483.1 2513.1
#> - tot_derog       1   2484.7 2514.7
#> - age_oldest_tr   1   2486.9 2516.9
#> - ltv             1   2538.4 2568.4
#> - fico_score      1   2570.6 2600.6
#> 
#> Step:  AIC=2508.57
#> bad_ind ~ vehicle_make + tot_derog + tot_tr + age_oldest_tr + 
#>     tot_open_tr + tot_rev_debt + tot_rev_line + rev_util + fico_score + 
#>     down_pyt + loan_term + loan_amt + ltv + veh_mileage
#> 
#>                  Df Deviance    AIC
#> - loan_term       1   2479.8 2507.8
#> <none>                2478.6 2508.6
#> - tot_open_tr     1   2480.7 2508.7
#> - vehicle_make    1   2481.2 2509.2
#> - down_pyt        1   2481.4 2509.4
#> - tot_rev_line    1   2481.6 2509.6
#> + tot_income      1   2477.7 2509.7
#> - loan_amt        1   2481.9 2509.9
#> + purch_price     1   2478.1 2510.1
#> + msrp            1   2478.2 2510.2
#> + application_id  1   2478.2 2510.2
#> - tot_rev_debt    1   2482.3 2510.3
#> + tot_rev_tr      1   2478.4 2510.4
#> + vehicle_year    1   2478.6 2510.6
#> + account_number  1   2478.6 2510.6
#> - rev_util        1   2482.7 2510.7
#> - veh_mileage     1   2484.2 2512.2
#> - tot_tr          1   2484.3 2512.3
#> - tot_derog       1   2485.5 2513.5
#> - age_oldest_tr   1   2488.2 2516.2
#> - ltv             1   2540.1 2568.1
#> - fico_score      1   2572.7 2600.7
#> 
#> Step:  AIC=2507.78
#> bad_ind ~ vehicle_make + tot_derog + tot_tr + age_oldest_tr + 
#>     tot_open_tr + tot_rev_debt + tot_rev_line + rev_util + fico_score + 
#>     down_pyt + loan_amt + ltv + veh_mileage
#> 
#>                  Df Deviance    AIC
#> <none>                2479.8 2507.8
#> - tot_open_tr     1   2481.8 2507.8
#> - vehicle_make    1   2482.3 2508.3
#> + loan_term       1   2478.6 2508.6
#> - down_pyt        1   2482.6 2508.6
#> - tot_rev_line    1   2482.7 2508.7
#> + tot_income      1   2478.9 2508.9
#> - loan_amt        1   2483.2 2509.2
#> + application_id  1   2479.4 2509.4
#> + purch_price     1   2479.5 2509.5
#> + msrp            1   2479.5 2509.5
#> - tot_rev_debt    1   2483.6 2509.6
#> + tot_rev_tr      1   2479.6 2509.6
#> + account_number  1   2479.8 2509.8
#> + vehicle_year    1   2479.8 2509.8
#> - rev_util        1   2484.0 2510.0
#> - tot_tr          1   2485.8 2511.8
#> - veh_mileage     1   2485.8 2511.8
#> - tot_derog       1   2486.9 2512.9
#> - age_oldest_tr   1   2489.5 2515.5
#> - ltv             1   2542.9 2568.9
#> - fico_score      1   2575.8 2601.8


logit<-stats::predict(lg_both,woe_test)
woe_test$lg_both_p<-exp(logit)/(1+exp(logit))


pred_both <- ROCR::prediction(woe_test$lg_both_p, woe_test$bad_ind)
perf_both <- ROCR::performance(pred_both,"tpr","fpr")




##step 9: Correlation Diagram
coe = (lg_both$coefficients)
cor1<-stats::cor(  Xvar_df<- woe_train[-which(colnames(feature) %in% c("application_id","account_number","bad_ind"))])

corrplot::corrplot(cor1)
corrplot::corrplot(cor1,method = "number")




##step 10: Manually Input Parameters to Generate Scorecards
##scorecard
Score<-noauto_scorecard( bins_card= woe_test, fit= lg_both,bins_woe=treebins_train  ,points0 = 600, 
                         odds0 = 1/20, pdo = 50 ,k = 2)
Score_2<-noauto_scorecard( bins_card= woe_train, fit= lg_both,bins_woe=treebins_train  ,points0 = 600, 
                           odds0 = 1/20, pdo = 50 ,k = 3)


##scorecard2
Score<-noauto_scorecard2( bins_card= woe_test, fit= lg_both,bins_woe=treebins_train  ,points0 = 600,
                          odds0 = 1/20, pdo = 50 ,k = 2)
Score_2<-noauto_scorecard2( bins_card= woe_train, fit= lg_both,bins_woe=treebins_train  ,points0 = 600, 
                            odds0 = 1/20, pdo = 50 ,k = 3)





##step 11: PSI
data_train <- Score_2$data_score
data_test <- Score$data_score
psi_1<-psi_cal( df_train = data_train , df_test = data_test,feat='Score',label='bad_ind' , nbins=10)
#> Warning in ggplot2::geom_bar(stat = "identity", color = "black", position =
#> ggplot2::position_dodge(), : Ignoring unknown parameters: `size`



##step 12: Data Painter
plot_board( label= woe_test$bad_ind, pred = woe_test$lg_both_p   )