-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlab3_page3.R
More file actions
69 lines (42 loc) · 1.56 KB
/
Copy pathlab3_page3.R
File metadata and controls
69 lines (42 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
setwd("C:/Fish/classes/summer_2015/Analytic_edge/data")
loans<-read.csv("loans.csv")
table(loans$not.fully.paid)
table(loans$not.fully.paid)[2]/sum(table(loans$not.fully.paid))
library(mice)
library(caTools)
set.seed(144)
vars.for.imputation = setdiff(names(loans), "not.fully.paid")
imputed = complete(mice(loans[vars.for.imputation]))
loans[vars.for.imputation] = imputed
###
loans<-read.csv("loans_imputed.csv")
set.seed(144)
split=sample.split(loans$not.fully.paid,SplitRatio = 0.7)
train<-loans[split==TRUE, ]
test<-loans[split==FALSE, ]
mod1<-glm(not.fully.paid~., data=train, family=binomial)
mod1$coefficients['fico']
predicted.risk=predict(mod1,newdata=test, type="response")
test$predicted.risk=predicted.risk
# load ROCR package
library(ROCR)
# Prediction function
ROCRpred = prediction(test$predicted.risk,test$not.fully.paid)
# Performance function
ROCRperf = performance(ROCRpred, "tpr", "fpr")
# Plot ROC curve
plot(ROCRperf)
# Add colors
plot(ROCRperf, colorize=TRUE)
# Add threshold labels
plot(ROCRperf, colorize=TRUE, print.cutoffs.at=seq(0,1,by=0.1), text.adj=c(-0.2,1.7))
# AUC
as.numeric(performance(ROCRpred, "auc")@y.values)
mod2<-glm(not.fully.paid~int.rate, data=train, family=binomial)
pred2<-predict(mod2,newdata=test,type="response")
ROCRpred = prediction(pred2,test$not.fully.paid)
test$profit = exp(test$int.rate*3) - 1
test$profit[test$not.fully.paid == 1] = -1
highInterest<-subset(test,test$int.rate>=0.15)
cutoff = sort(highInterest$predicted.risk, decreasing=FALSE)[100]
invest<-subset(highInterest,highInterest$predicted.risk<=cutoff)