-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path22.16- Logistic Regresyon - Logistic Regresyon 16 - Regularization ile Model Tuning I.R
executable file
·248 lines (158 loc) · 6.27 KB
/
22.16- Logistic Regresyon - Logistic Regresyon 16 - Regularization ile Model Tuning I.R
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
### Logistic Regresyon #########################
library(caret)
library(glmnet)
library(tidyverse)
placement["sl_no"] <- NULL
placement["salary"] <- NULL
table(placement$status)
dataPlaced <- placement %>% filter(status == "Placed")
dataNotPlaced <- placement %>% filter(status == "Not Placed")
nrow(dataPlaced)
nrow(dataNotPlaced)
set.seed(155)
dataPlacedIndex <- sample(1:nrow(dataPlaced) , size = 0.75*nrow(dataNotPlaced) )
set.seed(155)
dataNotPlacedIndex <- sample(1:nrow(dataNotPlaced) , size = 0.75*nrow(dataNotPlaced) )
trainPlaced <- dataPlaced[ dataPlacedIndex , ]
trainNotPlaced <- dataNotPlaced[ dataNotPlacedIndex , ]
trainSet <- rbind(trainPlaced , trainNotPlaced)
table(trainSet$status)
testPlaced <- dataPlaced[ -dataPlacedIndex , ]
testNotPlaced <- dataNotPlaced[ -dataNotPlacedIndex , ]
testSet <- rbind(testPlaced , testNotPlaced)
table(testSet$status)
?glm
## GLM ile Logistic Regresyon Modeli Oluşturma
# modelLogit <- glm(status ~ . , data = trainSet , family = binomial(link = "logit"))
modelLogit <- glm(status ~ . , data = trainSet , family = "binomial")
modelLogit
summary(modelLogit)
### ANOVA Değişken Deviance Değerleri
anova(modelLogit)
summary(modelLogit)
## variable Importance
varImp(modelLogit)
### Model Üzerinden Tahminler
# install.packages("InformationValue")
library(InformationValue)
predictions1 <- predict(modelLogit , testSet , type = "response")
predictions2 <- plogis(predict(modelLogit , testSet))
cm <- InformationValue::confusionMatrix(testSet$status , predictedScores = predictions1)
accur <- (cm[1,1] + cm[2,2]) /sum(cm)
accur
errorRate <- (cm[1,2] + cm[2,1]) / sum(cm)
errorRate
### Optimal Cutoff value
summary(predictions1)
optCutoff <- InformationValue::optimalCutoff(testSet$status , predictedScores = predictions1)
optCutoff
cmOpt <- InformationValue::confusionMatrix(testSet$status ,
predictedScores = predictions1 ,
threshold = optCutoff )
cmOpt
accurOpt <- (cmOpt[1,1] + cmOpt[2,2]) /sum(cmOpt)
accurOpt
accur
cm
cmOpt
## Precision ve Recall Değerlerinin Hesaplanması
cmOpt_1 <- InformationValue::confusionMatrix(testSet$status ,
predictedScores = predictions1 ,
threshold = optCutoff )
cmOpt_1
names(cmOpt_1) <- c("Not Placed (Negative)" , "Placed (Positive)")
rownames(cmOpt_1) <- c("Not Placed (Negative)" , "Placed (Positive)")
cmOpt_1
precision_1 <- cmOpt_1[2,2] / (cmOpt_1[2,1] + cmOpt_1[2,2])
recall_1 <- cmOpt_1[2,2] / (cmOpt_1[1,2] + cmOpt_1[2,2])
cmOpt_2 <- InformationValue::confusionMatrix(testSet$status ,
predictedScores = predictions1 ,
threshold = optCutoff )
names(cmOpt_2) <- c("Not Placed (Positive)" , "Placed (Negative)")
rownames(cmOpt_2) <- c("Not Placed (Positive)" , "Placed (Negative)")
cmOpt_2
precision_2 <- cmOpt_2[1,1] / (cmOpt_2[1,2] + cmOpt_2[1,1])
recall_2 <- cmOpt_2[1,1] / (cmOpt_2[2,1] + cmOpt_2[1,1])
precision_2;recall_2
# Sensitivity ve Specificity
# Positive class Placed
cmOpt_1
sensitivity_1 <- cmOpt_1[2,2] / (cmOpt_1[1,2] + cmOpt_1[2,2])
sensitivity_1
specifitcity_1 <- cmOpt_1[1,1] / (cmOpt_1[2,1] + cmOpt_1[1,1])
specifitcity_1
## F1 Skoru
# Positive calss Palced
cmOpt_1
f1_1 <- 2 * ( (precision_1 * recall_1) / (precision_1 + recall_1) )
f1_1
# Positive class Not Placed
cmOpt_2
f1_2 <- 2 * ( (precision_2 * recall_2) / (precision_2 + recall_2) )
f1_2
### ROC Curve ve AUC (Area Under Curve)
## Gerekli Paket
# install.packages("pROC")
library(pROC)
?roc
rocModel_1 <- roc( testSet$status ~ predictions1)
# Control = negative class
# Case = positive class
plot(rocModel_1)
cmOpt_1
## AUC Değeri ve ROC modeli
rocModel_1
### Caret paketi ile Confusion Matrix
?caret::confusionMatrix
optCutoff
predictions1
table(testSet$status)
predictedClass <- ifelse(predictions1 > optCutoff , "Placed" , "Not Placed")
predictedClass <- as.factor(predictedClass)
## Positive class varsayılan olarak ilk class Not Placed
caret::confusionMatrix(predictedClass , reference = testSet$status )
## Positive class değiştirme Placed
caret::confusionMatrix(predictedClass , reference = testSet$status , positive = "Placed")
## Recall ve Precision ve F1 metriklerini ekleme
caret::confusionMatrix(predictedClass , reference = testSet$status ,
positive = "Placed" , mode = "prec_recall")
## Conf. mat. kaydetme ve içindeki değerlere erişme
cmOpt_1_caret <- caret::confusionMatrix(predictedClass , reference = testSet$status ,
positive = "Placed" , mode = "prec_recall")
cmOpt_1_caret$byClass[1]
### Kappa İstatistiği ve McNemar Test İstatistiği
caret::confusionMatrix(predictedClass , reference = testSet$status , positive = "Placed")
## Kappa 0.1 - 0.2 - Kötü
## Kappa 0.2 - 0.4 - Kötü idare eder
## Kappa 0.4 - 0.6 - Orta
## Kappa 0.6 - 0.8 - İyi
## Kappa 0.8 - 1.0 - Mükemmel
## Mcnemar p-value > 0.05 Tahmin edilen ve gerçek değerler
## birbirine benzer ilişkili. (Yani iyi bir model)
##### Regularization Yöntemleri İle Model Tuning
library(glmnet)
library(tidyverse)
summary(modelLogit)
head(placement)
## Dummy Değişken Oluşturma
modelDataDummy <- model.matrix( ~ . , data = placement)
View(modelDataDummy)
modelDataDummy <- modelDataDummy[ , -1]
modelDataDummy <- as.data.frame(modelDataDummy)
dataPlacedDummy <- modelDataDummy %>% filter(statusPlaced == 1 )
dataNotPlacedDummy <- modelDataDummy %>% filter(statusPlaced == 0)
nrow(dataPlacedDummy)
nrow(dataNotPlacedDummy)
set.seed(155)
dataPlacedIndexDummy <- sample(1:nrow(dataPlacedDummy) , size = 0.75*nrow(dataNotPlacedDummy) )
set.seed(155)
dataNotPlacedIndexDummy <- sample(1:nrow(dataNotPlacedDummy) , size = 0.75*nrow(dataNotPlacedDummy) )
trainPlacedDummy <- dataPlacedDummy[ dataPlacedIndexDummy , ]
trainNotPlacedDummy <- dataNotPlacedDummy[ dataNotPlacedIndexDummy , ]
trainSetDummy <- rbind(trainPlacedDummy , trainNotPlacedDummy)
table(trainSetDummy$statusPlaced)
testPlacedDummy <- dataPlacedDummy[ -dataPlacedIndexDummy , ]
testNotPlacedDummy <- dataNotPlacedDummy[ -dataNotPlacedIndexDummy , ]
testSetDummy <- rbind(testPlacedDummy , testNotPlacedDummy)
table(testSetDummy$status)
### Regularization Adımları