-
Notifications
You must be signed in to change notification settings - Fork 0
/
XGBoost_bank.R
516 lines (394 loc) · 21.2 KB
/
XGBoost_bank.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#############################################################
# XG Boost - DEMO for web site #
#############################################################
library(shiny)
library(shinydashboard)
library(dplyr)
#######################################################
# DATA SOURCES #
#######################################################
Data <- read.csv("DataSample.csv")
######################################################
# FUNCTIONS #
######################################################
#######################################################
# USER INTERFACE #
#######################################################
ui <- dashboardPage(
dashboardHeader(title = h3("BANK LOAN"),
titleWidth = 300,
disable = F),
dashboardSidebar(
img(src = "logo-vertical-2022_388x300.jpg",height = 388, width = 300, align = "center"),
hr(),
h5(HTML("DATA UPLOAD AND CLEANING"), style="text-align:center"),
hr(),
disable = F,
width = 300,
collapsed = F,
actionButton("case_story"," 1.Read: Case Story"),
actionButton("Data"," 2.Read: data source"),
actionButton(inputId = "upload",
label = " 3.Data upload ",
icon = icon("refresh")),
hr(),
h5(HTML("THE XGBOOST ALGORITHM"), style="text-align:center"),
hr(),
actionButton("xgboost"," 4.Read: XGBoost - R code detail"),
actionButton(inputId = "upload_2",
label = " 5. R Code detail",
icon = icon("refresh")),
hr(),
h5(HTML("SHAP VALUE ANALYSIS"), style="text-align:center"),
hr(),
actionButton("charts"," 6. Chart drivers"),
selectInput("drivers","7. Select summary chart",
choices = list(
"Summary Plot" = "summary",
"Group main features" = "group",
"Observation range 500 - 650" = "feature1",
"Observation range 1500 - 1650" = "feature2",
"Observation range 3500 - 3650" = "feature3",
"Observation range 5500 - 5650" = "feature4",
"Observation range 8500 - 8650" = "feature5"),
selected = "Summary Plot"),
actionButton("plot1","Plot selected chart", icon("option-vertical",lib ="glyphicon")),
selectInput("mdrivers","8. Select main drivers",
choices = list(
"Duration" = "duration",
"Unknown contact" = "contact",
"Month:May" = "May",
"Housing" = "housing",
"Past days" = "day",
"Month:July" = "July",
"Balance" = "balance",
"Outcome" ="outcome",
"Age" = "age",
"Month:August" = "august"),
selected = "duration"),
actionButton("plot2","Plot selected chart", icon("option-vertical",lib ="glyphicon")),
selectInput("modrivers","9. Select other months graph",
choices = list(
"January" = "jan",
"February" = "feb",
"March" = "mar",
"June" = "jun",
"September" = "sep",
"November" = "nov",
"December" = "dec"),
selected = "jan"),
actionButton("plot3","Plot selected chart", icon("option-vertical",lib ="glyphicon")),
selectInput("jdrivers","10. Select job and activity graph",
choices = list(
"Unemployed" = "unemployed",
"Blue collar" = "bluecollar",
"Entrepreneur" = "entrepreneur",
"Housemaid" = "housemaid",
"Management" = "management",
"Self employed" = "selfemployed",
"Technician" = "technician",
"Unknown" = "unknown",
"Student" = "student",
"Retired" = "retired",
"Services" = "services"),
selected = "unemployed"),
actionButton("plot4","Plot selected chart", icon("option-vertical",lib ="glyphicon")),
hr(),
tags$div(class="header", checked=NA,
tags$p("¿DO YOU WANT TO TELL US ABOUT YOUR BUSINESS CASE?", align = "center"),
tags$a(href="https://www.gssg.com.co" , h5("CONTACT US HERE", align = "center", target = "_blank"))
),
hr(),
tags$div(class="header", checked=NA,
tags$a(href="http://insightdiscovery.co", h5("GO TO MAIN PAGE", align = "center", target = "_blank"))
),
hr(),
actionButton("refresh","Refresh", icon("refresh")),
hr()
),
dashboardBody(
box(
title = "Bank marketing data",
status = "info",
width = 12,
height = 600,
solidHeader = T,
DT::dataTableOutput("Data")
),
box(
title = "Check and download the R code PDF review",
status = "info",
width = 12,
height = 100,
solidHeader = T,
uiOutput("PDF_XGBoost")
),
box(
title = "Summary charts and plots",
status = "info",
width = 12,
height = 770,
solidHeader = T,
uiOutput("summary")
),
)
)
#######################################################
# server #
#######################################################
server <- function( input, output, session){
observeEvent(input$case_story,{
showModal(modalDialog(
title = "INTRODUCTION BANK LOAN STORY CASE - BANKING INDUSTRY ",
HTML("
This is a bank loan case with a binary (yes / no) answer option to accept or reject the loan <br>
in a tele marketing campaign. The detail description of the campaign and its data source is <br>
as follows:<br><br>
Relevant Information:<br><br>
The data is related with direct marketing campaigns of a Portuguese banking institution.<br>
The marketing campaigns were based on phone calls. Often, more than one contact to the <br>
same client was required, in order to access if the product (bank term deposit) would be (or not)<br>
subscribed.<br><BR>
key Question<br><br>
The positive answers rate are 12% in the real scenario; ¿ Which variable combination will reduce <br>
the negative answers (reduce the negative predictive value)? and which is the importance and direction (positive or negative ) influence on the decision ?<br><br>
<b> THE XGBOOST ALGORITHM WILL BE USED TO ANSWER THIS QUESTION </br>
"),
footer = tagList(modalButton("CLOSE WINDOW")),
size = "m",
easyClose = T,
fade = T
))
})
observeEvent(input$Data,{
showModal(modalDialog(
title = "DATA SOURCE ",
HTML(" This dataset is public available for research. The details are described in [Moro et al., 2011].<br>
Please include this citation if you plan to use this database: <br><br>
[Moro et al., 2011] S. Moro, R. Laureano and P. Cortez. Using Data Mining for Bank Direct Marketing:<br>
An Application of the CRISP-DM Methodology.<br>
In P. Novais et al. (Eds.), Proceedings of the European Simulation and Modelling Conference - <br>
ESM'2011, pp. 117-121, Guimarães, Portugal, October, 2011. EUROSIS.<br><br>
Available at: [pdf] http://hdl.handle.net/1822/14838 <br>
[bib] http://www3.dsi.uminho.pt/pcortez/bib/2011-esm-1.txt <br><br>
1. Title: Bank Marketing <br>
2. Sources<br>
Created by: Paulo Cortez (Univ. Minho) and Sérgio Moro (ISCTE-IUL) @ 2012<br>
3. Past Usage:<br>
The full dataset was described and analyzed in:<br>
S. Moro, R. Laureano and P. Cortez. Using Data Mining for Bank Direct Marketing: An Application of <br>
the CRISP-DM Methodology.<br>
In P. Novais et al. (Eds.), Proceedings of the European Simulation and Modelling Conference - ESM'2011,<br>
pp. 117-121, Guimarães, Portugal, October, 2011. EUROSIS.<br><br>
There are two datasets:<br><br>
1) bank-full.csv with all examples, ordered by date (from May 2008 to November 2010).<br>
2) DataSample.csv with 10% of the examples (4521), randomly selected from bank-full.csv.<br><br>
<b> THE ORIGINAL DATA SET HAS 45,211 OBSERVATIONS AND 17 VARIABLES; 10% WILL BE UPLOADED TO </b>
<b> UNDERSTAND THE DATA STRUCTURE. </b>"),
footer = tagList(modalButton("CLOSE WINDOW")),
size = "m",
easyClose = T,
fade = T
))
})
observeEvent(input$upload,{
updateActionButton(
session = session,
inputId = "upload",
label = "3. Loaded data",
icon = icon("ok",lib ="glyphicon")
)
output$Data <-
output$results <- DT::renderDataTable(
Data,
options = list(scrollX = TRUE)
)
})
observeEvent(input$xgboost,{
showModal(modalDialog(
title = " XG BOOST ALGORITHM R CODE DESCRIPTION ",
HTML("
The running time of the algorithm would be long,therefore the following document will<br>
review the steps to code with R the XGBoost algorithm to solve the key question. <br><br>
<b> THE XGBOOST ALGORITHM IS A SUPERVISED ALGORITHM THAT WORK WITH THE DECISION TREE ENSAMBLES </b>
<b> MODEL --- LEARN MORE HERE: https://xgboost.readthedocs.io/en/latest/tutorials/model.html --- </B>
"),
footer = tagList(modalButton("CLOSE WINDOW")),
size = "m",
easyClose = T,
fade = T
))
})
observeEvent(input$upload_2,{
updateActionButton(
session = session,
inputId = "upload_2",
label = "5. Select page and load document",
icon = icon("ok",lib ="glyphicon")
)
tags$div(class ="site.css", checked=NA,
output$PDF_XGBoost <- renderUI({tags$a(href="https://github.com/RicMendezP/Bank/blob/main/XG_Boost_bank_case_review.pdf" , h5("Download the XG Boost bank case review code", align = "center", class = "btn-secondary", target = "_blank"))}),
# {output$PNG_XGBoost <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="1.png")})}
# This is an example code to render an image exact page (from www - shiny ) if there is no link
)
})
observeEvent(input$charts,{
showModal(modalDialog(
title = "CHARTS PLOTS AND CONCLUSIONS",
HTML("Key insights will be analyzed using the SHAP values in the following steps: <br><br>
- Summary charts and detalied ranges of main features and its related observations.<br>
The summary graph will easily describe the <b>importance of each feature and its direction</b>;
if its effect is <b>positive or negative</b> to drive the customer decision get a loan. <br><br>
This graph will help to answer the stated key question at the introduction section .<br><br>
<b>Conclusions will be detailed on page 29 of step 4</b><br><br>
Other charts are:<br><br>
- Main variables charts to check its detailed positive or negative impact <br><br>
- Other month variables to check its detailed positive or negative impact<br><br>
- Main job and activities variables to check its detailed positive or negative impact<br><br>
----- learn about shap values: https://liuyanguu.github.io/post/2019/07/18/visualization-of-shap-for-xgboost/"),
footer = tagList(modalButton("CLOSE WINDOW")),
size = "m", #tamaño de la ventana
easyClose = T,
fade = T #Efecto
))
})
observeEvent(input$plot1,{
updateSelectInput(
session = session,
inputId = "drivers",
label = "7. Selected summary analysis",
choices = list(
"Summary Plot" = "summary",
"Group main features" = "group",
"Observation range 500 - 650" = "feature1",
"Observation range 1500 - 1650" = "feature2",
"Observation range 3500 - 3650" = "feature3",
"Observation range 5500 - 5650" = "feature4",
"Observation range 8500 - 8650" = "feature5"),
selected = "summary")
updateActionButton(
session = session,
inputId = "plot1",
label = " Select and Plot",
icon = icon("option-vertical",lib ="glyphicon"))
driver <- as.character(input$drivers)
if (driver=="summary") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_plot_summary.png")})}
if (driver=="group") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_by_group.png")})}
if (driver=="feature1") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_feature_500.png")})}
if (driver=="feature2") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_feature_1500.png")})}
if (driver=="feature3") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_feature_3500.png")})}
if (driver=="feature4") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_feature_5500.png")})}
if (driver=="feature5") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_feature_8500.png")})}
})
observeEvent(input$plot2,{
updateSelectInput(
session = session,
inputId = "mdrivers",
label = "8.Selected main drivers analysis",
choices = list(
"Duration" = "duration",
"Unknown contact" = "contact",
"Month:May" = "may",
"Housing" = "housing",
"Past days" = "day",
"Month:July" = "july",
"Balance" = "balance",
"Outcome" ="outcome",
"Age" = "age",
"Month:August" = "august"),
selected = "duration")
updateActionButton(
session = session,
inputId = "plot2",
label = " Select and Plot",
icon = icon("option-vertical",lib ="glyphicon"))
mdriver <- as.character(input$mdrivers)
if (mdriver=="duration") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_plot_duration.png")})}
if (mdriver=="contact") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_plot_contact_unknown.png")})}
if (mdriver=="may") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_plot_may.png")})}
if (mdriver=="housing") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_plot_housing_yes.png")})}
if (mdriver=="Paid days") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_value_pdays.png")})}
if (mdriver=="july") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_plot_jul.png")})}
if (mdriver=="balance") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_plot_balance.png")})}
if (mdriver=="outcome") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_plot_poutcome_success.png")})}
if (mdriver=="age") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_plot_age.png")})}
if (mdriver=="august") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_value_august.png")})}
})
observeEvent(input$plot3,{
updateSelectInput(
session = session,
inputId = "modrivers",
label = "9. Selected month analysis",
choices = list(
"January" = "jan",
"February" = "feb",
"March" = "mar",
"June" = "jun",
"September" = "sep",
"November" = "nov",
"December" = "dec"),
selected = "jan")
updateActionButton(
session = session,
inputId = "plot3",
label = " Select and Plot",
icon = icon("option-vertical",lib ="glyphicon"))
modriver <- as.character(input$modrivers)
if (modriver=="jan") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_plot_jan.png")})}
if (modriver=="feb") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shape_plot_feb.png")})}
if (modriver=="mar") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_plot_march.png")})}
if (modriver=="jun") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_plot_june.png")})}
if (modriver=="sep") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_plot_sept.png")})}
if (modriver=="nov") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shapplot_november.png")})}
if (modriver=="dec") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_plot_dec.png")})}
})
observeEvent(input$plot4,{
updateSelectInput(
session = session,
inputId = "jdrivers",
label = "10. Selected Job and Activity analysis",
choices = list(
"Unemployed" = "unemployed",
"Blue collar" = "bluecollar",
"Entrepreneur" = "entrepreneur",
"Housemaid" = "housemaid",
"Management" = "management",
"Self employed" = "selfemployed",
"Technician" = "technician",
"Unknown" = "unknown",
"Student" = "student",
"Retired" = "retired",
"Services" = "services"),
selected = "unemployed")
updateActionButton(
session = session,
inputId = "plot4",
label = " Select and Plot",
icon = icon("option-vertical",lib ="glyphicon"))
jdriver <- as.character(input$jdrivers)
if (jdriver=="unemployed") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_plot_unemployed.png")})}
if (jdriver=="bluecollar") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_plot_blue_collar.png")})}
if (jdriver=="entrepreneur") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_plot_entrepreneur.png")})}
if (jdriver=="housemaid") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_plot_housemaid.png")})}
if (jdriver=="management") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_plot_management.png")})}
if (jdriver=="selfemployed") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_plot_self_employed.png")})}
if (jdriver=="technician") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_plot_technician.png")})}
if (jdriver=="unknown") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_plot_job_unknown.png")})}
if (jdriver=="student") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_plot_student.png")})}
if (jdriver=="services") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_plot_job_services.png")})}
if (jdriver=="retired") {output$summary <- renderUI({tags$img(style = "height:700px;width:100%; scrolling=nyes",src="shap_plot_retired.png")})}
})
observeEvent(input$refresh,{
updateActionButton(
session = session,
inputId = "refresh",
label = "refresh",
icon = icon("ok",lib ="glyphicon")
)
session$reload()
})
}
#######################################################
# LAUNCH #
#######################################################
shinyApp( ui = ui , server = server)