-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathgerman credit
More file actions
121 lines (84 loc) · 4.32 KB
/
Copy pathgerman credit
File metadata and controls
121 lines (84 loc) · 4.32 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
library(caret)
data(GermanCredit)
head(GermanCredit)
str(GermanCredit)
summary(GermanCredit)
# Converting Class variable from Bad/Good to 0/1
GermanCredit$Class=ifelse(GermanCredit$Class=="Bad",0,1)
GermanCredit$Class[1:5]
# We will split our dataset into Train and Test data.
#The Train dataset will be used to build the model and
#the Test dataset will used to validate how well our model is performing.
set.seed(1234)
inTrain=createDataPartition(GermanCredit$Class, p=0.7, list=FALSE)
inTrain
Training=GermanCredit[inTrain,]
Testing=GermanCredit[-inTrain,]
dim(Training)
dim(Testing)
summary(Training$Class)
sum(Training$Class)
sum(Testing$Class)
# Multicollinearity
#install.packages("usdm")
library(usdm)
vifstep(GermanCredit[,-10], th=2)
a=vifstep(GermanCredit[,-10], th=2)
GermanCredit2=exclude(GermanCredit,a)
fit10=glm(Class~.,
data = Training[,c("Class",names(GermanCredit2))],
family=binomial(link="logit"))
summary(fit10)
fit0=glm(Class~ Duration+InstallmentRatePercentage+ResidenceDuration+Age+NumberExistingCredits+
NumberPeopleMaintenance+Telephone+ForeignWorker+CheckingAccountStatus.0.to.200+
CheckingAccountStatus.gt.200+CheckingAccountStatus.none+CreditHistory.ThisBank.AllPaid+
CreditHistory.Delay+CreditHistory.Critical+Purpose.UsedCar+Purpose.Furniture.Equipment+
Purpose.Radio.Television+Purpose.DomesticAppliance+Purpose.Repairs+Purpose.Education+
Purpose.Retraining+Purpose.Business+ Purpose.Other+SavingsAccountBonds.100.to.500+
SavingsAccountBonds.500.to.1000+SavingsAccountBonds.gt.1000+SavingsAccountBonds.Unknown+
EmploymentDuration.1.to.4+EmploymentDuration.4.to.7+
EmploymentDuration.Unemployed+Personal.Female.NotSingle+Personal.Male.Married.Widowed+
OtherDebtorsGuarantors.CoApplicant+OtherDebtorsGuarantors.Guarantor+Property.Insurance+
Property.CarOther+OtherInstallmentPlans.Stores+OtherInstallmentPlans.None+
Housing.Own+Job.UnskilledResident+Job.Management.SelfEmp.HighlyQualified,
data=Training,family=binomial(link="logit"))
summary(fit0)
#Select the model obtained from VIF for stepwise
library(MASS)
step=stepAIC(fit0, direction="both")
fitA= glm(Class~ Duration + InstallmentRatePercentage + Age + NumberExistingCredits +
Telephone + CheckingAccountStatus.gt.200 + CheckingAccountStatus.none +
CreditHistory.Critical + Purpose.UsedCar + Purpose.Furniture.Equipment +
Purpose.Radio.Television + Purpose.Retraining + Purpose.Business +
Purpose.Other + SavingsAccountBonds.100.to.500 + SavingsAccountBonds.500.to.1000 +
SavingsAccountBonds.gt.1000 + SavingsAccountBonds.Unknown +
EmploymentDuration.4.to.7 + Personal.Female.NotSingle + OtherDebtorsGuarantors.Guarantor +
Property.Insurance + Property.CarOther + OtherInstallmentPlans.None +
Housing.Own, data=Training,family=binomial(link="logit"))
summary(fitA)
fitB=update(fitA, .~. -Property.Insurance-Pupose.Other-Purpose.Business-Purpose.Retraining-CreditHistory.ThisBank.AllPaid-Age-
-SavingsAccountBonds.100.to.500-CheckingAccountStatus.gt.200-Purpose.Furniture.Equipment, data=Training)
summary(fitB)
fitC=update(fitB, .~. -Purpose.Other-SavingsAccountBonds.100.to.500-Property.CarOther-
OtherInstallmentPlans.None , data=Training)
summary(fitC)
fitD=update(fitC, .~. -SavingsAccountBonds.500.to.1000 -Telephone -Housing.Own, data=Training)
summary(fitD)
Pred=predict(fitD, newdata=Training[,-10], type="response" )
Pred1=ifelse(Pred<0.5,0,1)
confusionMatrix(table(Training$Class,Pred1,dnn=list('actual','predicted')))
table(Training$Class,Pred1)
#install.packages("ResourceSelection")
library(ResourceSelection)
hoslem.test(Training$Class,fitted(fitD),g=10)
#install.packages("survey")
library(survey)
regTermTest(fitD,"Personal.Female.NotSingle")
library(pscl)
pR2(fitD)
#install.packages("pscl")
#install.packages("InformationValue")
library(InformationValue)
plotROC(actuals=Training$Class,predictedScores=as.numeric(fitted(fitD)))
ks_plot(actuals=Training$Class,predictedScores=as.numeric(fitted(fitD)))
ks_stat(actuals=Training$Class,predictedScores=as.numeric(fitted(fitD)))