rm(list=ls(all=T))
options(digits=4, scipen=12)
library(magrittr)
議題:議題:使用貸款人的資料,預測他會不會還款
【1.1 基礎機率】What proportion of the loans in the dataset were not paid in full?
1533/(1533+8045)
[1] 0.1600543
【1.2 檢查缺項】Which of the following variables has at least one missing observation?
summary(loan)
credit.policy purpose int.rate installment
Min. :0.000 all_other :2331 Min. :0.0600 Min. : 15.67
1st Qu.:1.000 credit_card :1262 1st Qu.:0.1039 1st Qu.:163.77
Median :1.000 debt_consolidation:3957 Median :0.1221 Median :268.95
Mean :0.805 educational : 343 Mean :0.1226 Mean :319.09
3rd Qu.:1.000 home_improvement : 629 3rd Qu.:0.1407 3rd Qu.:432.76
Max. :1.000 major_purchase : 437 Max. :0.2164 Max. :940.14
small_business : 619
log.annual.inc dti fico days.with.cr.line
Min. : 7.548 Min. : 0.000 Min. :612.0 Min. : 179
1st Qu.:10.558 1st Qu.: 7.213 1st Qu.:682.0 1st Qu.: 2820
Median :10.928 Median :12.665 Median :707.0 Median : 4140
Mean :10.932 Mean :12.607 Mean :710.8 Mean : 4562
3rd Qu.:11.290 3rd Qu.:17.950 3rd Qu.:737.0 3rd Qu.: 5730
Max. :14.528 Max. :29.960 Max. :827.0 Max. :17640
NA's :4 NA's :29
revol.bal revol.util inq.last.6mths delinq.2yrs
Min. : 0 Min. : 0.00 Min. : 0.000 Min. : 0.0000
1st Qu.: 3187 1st Qu.: 22.70 1st Qu.: 0.000 1st Qu.: 0.0000
Median : 8596 Median : 46.40 Median : 1.000 Median : 0.0000
Mean : 16914 Mean : 46.87 Mean : 1.572 Mean : 0.1638
3rd Qu.: 18250 3rd Qu.: 71.00 3rd Qu.: 2.000 3rd Qu.: 0.0000
Max. :1207359 Max. :119.00 Max. :33.000 Max. :13.0000
NA's :62 NA's :29 NA's :29
pub.rec not.fully.paid
Min. :0.0000 Min. :0.0000
1st Qu.:0.0000 1st Qu.:0.0000
Median :0.0000 Median :0.0000
Mean :0.0621 Mean :0.1601
3rd Qu.:0.0000 3rd Qu.:0.0000
Max. :5.0000 Max. :1.0000
NA's :29
【1.3 決定是否要補缺項】Which of the following is the best reason to fill in the missing values for these variables instead of removing observations with missing data?
#We want to be able to predict risk for all borrowers, instead of just the ones with all data reported.
【1.4 補缺項工具】What best describes the process we just used to handle missing values?
install.packages("mice")
library(mice)
set.seed(144)
vars.for.imputation = setdiff(names(loan), "not.fully.paid")
imputed = complete(mice(loan[vars.for.imputation]))
loan[vars.for.imputation] = imputed
#predicted missing variable values using the available independent variables for each observation.
loan=read.csv("Unit3/loans_imputed.csv")
summary(loan) #no missing data
【2.1 顯著性】Which independent variables are significant in our model?
set.seed(144) #設定隨機種子
Warning message:
In strsplit(code, "\n", fixed = TRUE) :
input string 1 is invalid in this locale
library(caTools)
split = sample.split(loan$not.fully.paid, SplitRatio = 0.7)
tr = subset(loan, split == TRUE)
ts = subset(loan, split == FALSE)
mod= glm(not.fully.paid~., tr, family = "binomial")
summary(mod)
Call:
glm(formula = not.fully.paid ~ ., family = "binomial", data = tr)
Deviance Residuals:
Min 1Q Median 3Q Max
-2.2049 -0.6205 -0.4951 -0.3606 2.6397
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 9.187e+00 1.554e+00 5.910 3.42e-09 ***
credit.policy -3.368e-01 1.011e-01 -3.332 0.000861 ***
purposecredit_card -6.141e-01 1.344e-01 -4.568 4.93e-06 ***
purposedebt_consolidation -3.212e-01 9.183e-02 -3.498 0.000469 ***
purposeeducational 1.347e-01 1.753e-01 0.768 0.442201
purposehome_improvement 1.727e-01 1.480e-01 1.167 0.243135
purposemajor_purchase -4.830e-01 2.009e-01 -2.404 0.016203 *
purposesmall_business 4.120e-01 1.419e-01 2.905 0.003678 **
int.rate 6.110e-01 2.085e+00 0.293 0.769446
installment 1.275e-03 2.092e-04 6.093 1.11e-09 ***
log.annual.inc -4.337e-01 7.148e-02 -6.067 1.30e-09 ***
dti 4.638e-03 5.502e-03 0.843 0.399288
fico -9.317e-03 1.710e-03 -5.448 5.08e-08 ***
days.with.cr.line 2.371e-06 1.588e-05 0.149 0.881343
revol.bal 3.085e-06 1.168e-06 2.641 0.008273 **
revol.util 1.839e-03 1.535e-03 1.199 0.230722
inq.last.6mths 8.437e-02 1.600e-02 5.275 1.33e-07 ***
delinq.2yrs -8.320e-02 6.561e-02 -1.268 0.204762
pub.rec 3.300e-01 1.139e-01 2.898 0.003756 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 5896.6 on 6704 degrees of freedom
Residual deviance: 5485.2 on 6686 degrees of freedom
AIC: 5523.2
Number of Fisher Scoring iterations: 5
#credit.policy, purpose2, purpose3, purpose6, purpose7, installment, log.annual.inc,
#fico, revol.bal, inq.last.6mths, pub.rec
【2.2 從回歸係數估計邊際效用】Consider two loan applications, which are identical other than the fact that the borrower in Application A has FICO credit score 700 while the borrower in Application B has FICO credit score 710. What is the value of Logit(A) - Logit(B)? What is the value of O(A)/O(B)?
logit_A_logit_B ; exp(logit_A_logit_B)
[1] 0.09317
[1] 1.097648
【2.3 混淆矩陣、正確率 vs 底線機率】What is the accuracy of the logistic regression model? What is the accuracy of the baseline model?
ACC ; base_ACC
[1] 0.8364079
[1] 0.8398886
【2.4 ROC & AUC】Use the ROCR package to compute the test set AUC.
as.numeric(performance(ROCRpred, "auc") @y.values)
[1] 0.6720995
【3.1 高底線模型】The variable int.rate is highly significant in the bivariate model, but it is not significant at the 0.05 level in the model trained with all the independent variables. What is the most likely explanation for this difference?
#int.rate is correlated with other risk-related variables, and therefore does not incrementally improve the model when those other variables are included.
#base on老師上課講的故事(Tony Chuo, 2018)。
【3.2 高底線模型的預測值】What is the highest predicted probability of a loan not being paid in full on the testing set? With a logistic regression cutoff of 0.5, how many loans would be predicted as not being paid in full on the testing set?
table(ts$not.fully.paid, as.numeric(pred2 >= 0.5))
0
0 2413
1 460
【3.3 高底線模型的辨識率】What is the test set AUC of the bivariate model?
as.numeric(performance(ROCRpred, "auc") @y.values)
[1] 0.6239081
【4.1 投資價值的算法】How much does a $10 investment with an annual interest rate of 6% pay back after 3 years, using continuous compounding of interest?
10* exp(0.06*3)
[1] 11.97217
【4.2 投資獲利的算法,合約完成】While the investment has value c * exp(rt) dollars after collecting interest, the investor had to pay $c for the investment. What is the profit to the investor if the investment is paid back in full?
#c * exp(rt) - c
【4.3 投資獲利的算法,違約】Now, consider the case where the investor made a $c investment, but it was not paid back in full. Assume, conservatively, that no money was received from the borrower (often a lender will receive some but not all of the value of the loan, making this a pessimistic assumption of how much is received). What is the profit to the investor in this scenario?
# -c
【5.1 計算測試資料的實際投報率】What is the maximum profit of a $10 investment in any loan in the testing set?
0.8895*10
[1] 8.895
A simple investment strategy of equally investing in all the loans would yield profit $20.94 for a $100 investment. But this simple investment strategy does not leverage the prediction model we built earlier in this problem.
【6.1 高利率、高風險】What is the average profit of a $1 investment in one of these high-interest loans (do not include the $ sign in your answer)? What proportion of the high-interest loans were not paid back in full?
M ; pro_not
[1] 0.2251015
[1] 0.2517162
【6.2 高利率之中的低風險】What is the profit of the investor, who invested $1 in each of these 100 loans? How many of 100 selected loans were not paid back in full?
table(selectedLoans$not.fully.paid) #19
0 1
81 19
【Q】利用我們建好的模型,你可以設計出比上述的方法獲利更高的投資方法嗎?請詳述你的作法?
#
#
#
#