-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWoE and IV.R
310 lines (222 loc) · 13.6 KB
/
WoE and IV.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
rm(list=ls())
library(data.table)
library(ggplot2)
library(dplyr)
library(stringr)
library(RANN)
library(h2o)
library(caret)
library(gridExtra)
library(corrplot)
library(tidyr)
library(cvAUC)
library(plotly)
library(pwr)
library(reshape)
#Lets begin by reading in and examining our data
## Importing the Data
# Setup how the classes will be read in
class <- c( "numeric", "numeric", "numeric", "character", "character", "numeric", "numeric",
"numeric", "numeric", "numeric", "numeric", "numeric", "character", "character", "numeric",
"date", "numeric", "numeric", "character", "numeric", "numeric", "character")
path <- c("/Users/mustafaozturk/Desktop/eBay Case/DataSet/cars_dataset_may2019.csv")
# Read in and examine the data
cars <- data.table::fread(path, colClasses = class)
#Converting NA to 0
cars$telclicks <- ifelse(is.na(cars$telclicks), "0", cars$telclicks)
cars$bids <- ifelse(is.na(cars$bids), "0", cars$bids)
cars$webclicks <- ifelse(is.na(cars$webclicks), "0", cars$webclicks)
#Changing NA, None and ? to .
cars[cars == "None"] <- NA
cars[cars == "?"] <- NA
cars$energielabel <- as.factor(cars$energielabel)
carsAB <- cars %>% filter(group == "A" | group == "B")
# WOE and IF implementation in R
## We will use the scorecard package and an example dataset to investigate the concepts of WOE and IV and test its implementation in R
data <- carsAB %>%
as_tibble()
#replace '.' in variable names not compatible with f_train_lasso
vars = names(data) %>%
str_replace_all( '\\.', '_')
names(data) <- vars
data$telclicks <- as.numeric(data$telclicks)
data$bids <- as.numeric(data$bids)
data$webclicks <- as.numeric(data$webclicks)
data$n_asq <- as.numeric(data$n_asq)
#Create dummy variables
data <- data %>% mutate(age = as.numeric(format(Sys.Date(), "%Y")) -
as.integer(data$bouwjaar),
annual_emissions = as.numeric(emissie)/age,
annual_kms = kmstand / age)
# create an age grouping
data <- data %>% mutate(ageGroup = ifelse(age<= 3, "(<=3)",
ifelse(3 < age & age <= 6, "(4-6)",
ifelse(5 < age & age <= 10, "(7-10)",
ifelse(10 < age & age <= 15, "(11-15)",
ifelse(15 < age & age <= 20, "(16-20)", "(20+)"))))))
data$ageGroup <- as.factor(data$ageGroup)
# convert response factor variable to dummy variable
data <- data %>% mutate(total_clicks = (telclicks + webclicks + n_asq +bids))
# create the response variable (label)
data$clicked <- ifelse(data$total_clicks > 0, 1, 0)
data$clicked <- as.factor(data$clicked)
data$kleur <- as.factor(data$kleur)
data$kleur2 <- ifelse(data$kleur=="Zilver of Grijs","Zilver of Grijs",
ifelse(data$kleur=="Zwart","Zwart",
ifelse(data$kleur=="Blauw","Blauw",
ifelse(data$kleur=="Wit","Wit",
ifelse(data$kleur=="Rood","Rood",
ifelse(is.na(data$carrosserie),"NA's","Other"))))))
data$carrosserie <- as.factor(data$carrosserie)
data$carrosserie2 <- ifelse(data$carrosserie=="Hatchback (3/5-deurs)","Hatchback (3/5-deurs)",
ifelse(data$carrosserie=="MPV","MPV",
ifelse(data$carrosserie=="Sedan (2/4-deurs)","Sedan (2/4-deurs)",
ifelse(data$carrosserie=="Terreinwagen","Terreinwagen",
ifelse(data$carrosserie=="Stationwagon","Stationwagon",
ifelse(is.na(data$carrosserie),"NA's","Other"))))))
data$energielabel2 <- ifelse(data$energielabel=="A","A",
ifelse(data$energielabel=="B","B",
ifelse(data$energielabel=="C","C",
ifelse(data$energielabel=="D","D",
ifelse(data$energielabel=="E","E",
ifelse(is.na(data$energielabel),"NA's","Other"))))))
data$brand2 <- ifelse(data$brand=="VOLKSWAGEN","VOLKSWAGEN",
ifelse(data$brand=="PEUGEOT","PEUGEOT",
ifelse(data$brand=="RENAULT","RENAULT",
ifelse(data$brand=="OPEL","OPEL",
ifelse(data$brand=="FORD","FORD",
ifelse(data$brand=="BMW","BMW","Other"))))))
data$aantalstoelen2 <- ifelse(data$aantalstoelen=="2","2",
ifelse(data$aantalstoelen=="3","3",
ifelse(data$aantalstoelen=="4","4",
ifelse(data$aantalstoelen=="5","5",
ifelse(data$aantalstoelen=="7","7",
ifelse(is.na(data$aantalstoelen),"NA's","Other"))))))
data$emissie2 <- ifelse(data$emissie=="0","0",
ifelse(data$emissie=="139","139",
ifelse(data$emissie=="99","99",
ifelse(data$emissie=="119","119",
ifelse(data$emissie=="109","109",
ifelse(is.na(data$emissie),"NA's","Other"))))))
data$model2 <- ifelse(data$model=="Golf","Golf",
ifelse(data$model=="Polo","Polo",
ifelse(data$model=="Astra","Astra",
ifelse(data$model=="Focus","Focus",
ifelse(data$model=="Corsa","Corsa",
ifelse(is.na(data$model),"NA's","Other"))))))
data$price2 <- ifelse(data$price<10000,"[-inf,10.000)",
ifelse(data$price<50000,"[10.000,50.000)",
ifelse(data$price<90000,"[50.000,90.000)",
ifelse(is.na(data$price),"NA's","[90.000,inf)"))))
data$kleur2 <- as.factor(data$kleur2)
data$carrosserie2 <- as.factor(data$carrosserie2)
data$energielabel2 <- as.factor(data$energielabel2)
data$aantalstoelen2 <- as.factor(data$aantalstoelen2)
data$emissie2 <- as.factor(data$emissie2)
data$model2 <- as.factor(data$model2)
data$brand2 <- as.factor(data$brand2)
data$aantaldeuren <- as.factor(data$aantaldeuren)
data$emissie <- as.factor(data$emissie)
data$brand <- as.factor(data$brand)
data$l2 <- as.factor(data$l2)
data$model <- as.factor(data$model)
data$aantalstoelen <- as.factor(data$aantalstoelen)
summary(data)
#############################
######## A/B TESTING ########
#############################
data%>%group_by(group)%>%summarise(cnt=n())%>%group_by(clicked)%>%summarise(cnt2=n())
data
clickgroup <- group_by(data, group, clicked)
groupclick <- summarize(clickgroup, clicks = n())
groupclick
final<-cast(groupclick,group~clicked)
final
#Lift calculation
lift <- (final[final$group=='B',3]/(final[final$group=='B',3]+final[final$group=='B',2]))/(final[final$group=='A',3]/(final[final$group=='A',3]+final[final$group=='A',2]))
#Price Groups
priceclickgroup <- group_by(data, group,price2, clicked)
pricegroupclick <- summarize(priceclickgroup, clicks = n())
print(pricegroupclick)
pricefinal<-cast(pricegroupclick,group+price2~clicked)
print(pricefinal)
#Price [-inf,10.000)
liftprice1 <- (pricefinal[pricefinal$group=='A'&pricefinal$price2 =='[-inf,10.000)',4]/(pricefinal[pricefinal$group=='A'&pricefinal$price2 =='[-inf,10.000)',3 ]+pricefinal[pricefinal$group=='A'&pricefinal$price2 =='[-inf,10.000)',4 ]))/(pricefinal[pricefinal$group=='B'&pricefinal$price2 =='[-inf,10.000)',4 ]/(pricefinal[pricefinal$group=='B'&pricefinal$price2 =='[-inf,10.000)',3 ]+pricefinal[pricefinal$group=='B'&pricefinal$price2 =='[-inf,10.000)',4 ]))
print(liftprice1)
#Price [10.000,50.000)
liftprice2 <- (pricefinal[pricefinal$group=='A'&pricefinal$price2 =='[10.000,50.000)',4]/(pricefinal[pricefinal$group=='A'&pricefinal$price2 =='[10.000,50.000)',3 ]+pricefinal[pricefinal$group=='A'&pricefinal$price2 =='[10.000,50.000)',4 ]))/(pricefinal[pricefinal$group=='B'&pricefinal$price2 =='[10.000,50.000)',4 ]/(pricefinal[pricefinal$group=='B'&pricefinal$price2 =='[10.000,50.000)',3 ]+pricefinal[pricefinal$group=='B'&pricefinal$price2 =='[10.000,50.000)',4 ]))
print(liftprice2)
#Price [50.000,90.000)
liftprice3 <- (pricefinal[pricefinal$group=='A'&pricefinal$price2 =='[50.000,90.000)',4]/(pricefinal[pricefinal$group=='A'&pricefinal$price2 =='[50.000,90.000)',3 ]+pricefinal[pricefinal$group=='A'&pricefinal$price2 =='[50.000,90.000)',4 ]))/(pricefinal[pricefinal$group=='B'&pricefinal$price2 =='[50.000,90.000)',4 ]/(pricefinal[pricefinal$group=='B'&pricefinal$price2 =='[50.000,90.000)',3 ]+pricefinal[pricefinal$group=='B'&pricefinal$price2 =='[50.000,90.000)',4 ]))
print(liftprice3)
#Price [90.000,inf)
liftprice4 <- (pricefinal[pricefinal$group=='A'&pricefinal$price2 =='[90.000,inf)',4]/(pricefinal[pricefinal$group=='A'&pricefinal$price2 =='[90.000,inf)',3 ]+pricefinal[pricefinal$group=='A'&pricefinal$price2 =='[90.000,inf)',4 ]))/(pricefinal[pricefinal$group=='B'&pricefinal$price2 =='[90.000,inf)',4 ]/(pricefinal[pricefinal$group=='B'&pricefinal$price2 =='[90.000,inf)',3 ]+pricefinal[pricefinal$group=='B'&pricefinal$price2 =='[90.000,inf)',4 ]))
print(liftprice4)
resultPrice <- rbind(liftprice1,liftprice2,liftprice3,liftprice4)
#############################################
#Age Groups
ageclickgroup <- group_by(data, group,ageGroup, clicked)
agegroupclick <- summarize(ageclickgroup, clicks = n())
agegroupclick
agefinal<-cast(agegroupclick,group+ageGroup~clicked)
agefinal
#Age <=3
liftAge3 <- (agefinal[agefinal$group=='B'&agefinal$ageGroup =='(<=3)',4]/(agefinal[agefinal$group=='B'&agefinal$ageGroup =='(<=3)',3 ]+agefinal[agefinal$group=='B'&agefinal$ageGroup =='(<=3)',4 ]))/(agefinal[agefinal$group=='A'&agefinal$ageGroup =='(<=3)',4 ]/(agefinal[agefinal$group=='A'&agefinal$ageGroup =='(<=3)',3 ]+agefinal[agefinal$group=='A'&agefinal$ageGroup =='(<=3)',4 ]))
print(liftAge3)
#Age 4-6
liftAge46 <- (agefinal[agefinal$group=='A'&agefinal$ageGroup =='(4-6)',4]/(agefinal[agefinal$group=='A'&agefinal$ageGroup =='(4-6)',3 ]+agefinal[agefinal$group=='A'&agefinal$ageGroup =='(4-6)',4 ]))/(agefinal[agefinal$group=='B'&agefinal$ageGroup =='(4-6)',4 ]/(agefinal[agefinal$group=='B'&agefinal$ageGroup =='(4-6)',3 ]+agefinal[agefinal$group=='B'&agefinal$ageGroup =='(4-6)',4 ]))
print(liftAge46)
#Age 7-10
liftAge710 <- (agefinal[agefinal$group=='A'&agefinal$ageGroup =='(7-10)',4]/(agefinal[agefinal$group=='A'&agefinal$ageGroup =='(7-10)',3 ]+agefinal[agefinal$group=='A'&agefinal$ageGroup =='(7-10)',4 ]))/(agefinal[agefinal$group=='B'&agefinal$ageGroup =='(7-10)',4 ]/(agefinal[agefinal$group=='B'&agefinal$ageGroup =='(7-10)',3 ]+agefinal[agefinal$group=='B'&agefinal$ageGroup =='(7-10)',4 ]))
print(liftAge710)
#Age 11-15
liftAge1115 <- (agefinal[agefinal$group=='A'&agefinal$ageGroup =='(11-15)',4]/(agefinal[agefinal$group=='A'&agefinal$ageGroup =='(11-15)',3 ]+agefinal[agefinal$group=='A'&agefinal$ageGroup =='(11-15)',4 ]))/(agefinal[agefinal$group=='B'&agefinal$ageGroup =='(11-15)',4 ]/(agefinal[agefinal$group=='B'&agefinal$ageGroup =='(11-15)',3 ]+agefinal[agefinal$group=='B'&agefinal$ageGroup =='(11-15)',4 ]))
print(liftAge1115)
#Age 16-20
liftAge1620 <- (agefinal[agefinal$group=='A'&agefinal$ageGroup =='(16-20)',4]/(agefinal[agefinal$group=='A'&agefinal$ageGroup =='(16-20)',3 ]+agefinal[agefinal$group=='A'&agefinal$ageGroup =='(16-20)',4 ]))/(agefinal[agefinal$group=='B'&agefinal$ageGroup =='(16-20)',4 ]/(agefinal[agefinal$group=='B'&agefinal$ageGroup =='(16-20)',3 ]+agefinal[agefinal$group=='B'&agefinal$ageGroup =='(16-20)',4 ]))
print(liftAge1620)
#Age 20+
liftAge20<- (agefinal[agefinal$group=='A'&agefinal$ageGroup =='(20+)',4]/(agefinal[agefinal$group=='A'&agefinal$ageGroup =='(20+)',3 ]+agefinal[agefinal$group=='A'&agefinal$ageGroup =='(20+)',4 ]))/(agefinal[agefinal$group=='B'&agefinal$ageGroup =='(20+)',4 ]/(agefinal[agefinal$group=='B'&agefinal$ageGroup =='(20+)',3 ]+agefinal[agefinal$group=='B'&agefinal$ageGroup =='(20+)',4 ]))
print(liftAge20)
resultAge <- rbind(liftAge3,liftAge46,liftAge710,liftAge1115,liftAge1620,liftAge20)
#############################
######## SCORECARD ########
#############################
library(scorecard)
?scorecard
names(data)
data <- subset( data, select = -c(telclicks, bids,n_asq, l2,webclicks,emissie,model,brand,
total_clicks,kleur,carrosserie,energielabel,aantalstoelen) )
summary(data)
iv = iv(data, y = 'clicked') %>%
as_tibble() %>%
mutate( info_value = round(info_value, 3) ) %>%
arrange( desc(info_value) )
iv %>%
knitr::kable()
#Weight of Evidence Binning
bins = woebinMus(data, y = 'clicked')
woebin()
bins$price %>%
knitr::kable()
woebin_plot(bins$group)
woebin_plot(bins$price)
woebin_plot(bins$bouwjaar)
woebin_plot(bins$model2)
woebin_plot(bins$emissie)
woebin_plot(bins$kmstand)
woebin_plot(bins$days_live)
woebin_plot(bins$photo_cnt)
woebin_plot(bins$energielabel)
woebin_plot(bins$annual_emissions)
woebin_plot(bins$annual_kms)
woebin_plot(bins$vermogen)
woebin_plot(bins$brand)
woebin_plot(bins$aantaldeuren)
woebin_plot(bins$carrosserie)
woebin_plot(bins$kleur)
woebin_plot(bins$age)
#Devam edip bakılmalı
#WoE binning application
data_woe = woebin_ply( data, bins ) %>%
as_tibble()