library(shiny)
library(shinymaterial)
library(flexdashboard)
library(dplyr)
library(tidyverse)
library(ggplot2)
library(plotly)
library(lime)
library(DT)
library(caret)
library(randomForest)
my_model <- base::readRDS("model/rf_default.rds")
customer scorecard inputs
main_vars <- c('Customer Name','Gender','Sector Code','Nationality','Account Type','Currency','Account Period in Month','Business Unit','Branch','Account Officer')
commercial_vars <- c('Product','Mobile Banking')
financial_vars <- c('Internet Banking','Total Charges')
customer_feature_vars <- c(main_vars,commercial_vars,financial_vars) %>% unique
Frequency_val <- c('Customer Demographics', 'Account Information', 'Bank Informantion', 'Financial Information')
base::load('data/dataset.RData')
churn_data_raw <- dataset.rf %>%
mutate(Numberoftransactions_bck =
dplyr::case_when (
No.OfDebitTrans < 3 ~ "<3k Trans",
No.OfDebitTrans < 6 ~ "3-5 Trans",
No.OfDebitTrans < 9 ~ "6-8 Trans",
No.OfDebitTrans < 15 ~ "9-14 Trans",
No.OfDebitTrans >= 15 ~ "Over 15 Trans",
TRUE ~ "NA"),
DebitTrans_Amt_bck = dplyr::case_when (
DebitTransAmt < 10000 ~ "<10k Trans.amt",
DebitTransAmt < 40000 ~ "10k-40k Trans.amt",
DebitTransAmt < 70000 ~ "40k-70k Trans.amt",
DebitTransAmt < 100000 ~ "70k-100k Trans.amt",
DebitTransAmt >= 10000 ~ "Over 100k Trans.amt",
TRUE ~ "NA"),
AvgBalance_bck = dplyr::case_when (
AvgBalance < 10000 ~ "<10k Avg.Balance",
AvgBalance < 180000 ~ "10k-180k Avg.Balance",
AvgBalance < 530000 ~ "180k-530k Avg.Balance",
AvgBalance >= 530000 ~ "Over 530k Avg.Balance",
TRUE ~ "NA"),
TotalCharges_bck = dplyr::case_when (
DebitTransAmt < 10000 ~ "<10k Charges",
DebitTransAmt < 40000 ~ "10k-40k Charges",
DebitTransAmt < 70000 ~ "40k-70k Charges",
DebitTransAmt < 100000 ~ "70k-100k Charges",
DebitTransAmt >= 100000 ~ "Over 100k Charges",
TRUE ~ "NA")
)
#tags$a(href='Mttutl',tags$img(src='side_nav1.png',height='60',width='90')),
ui <- material_page(
title = " ",
nav_bar_fixed = FALSE,
include_fonts = T,
includeCSS("www/css/material_css.css"),
nav_bar_color = "teal lighten-1",
material_side_nav(
image_source = "Vision_Excel.PNG",
material_side_nav_tabs(
side_nav_tabs = c(
"Customer Churn" = "example_side_nav_tab_1"
),
icons = c("person")
)
),
material_side_nav_tab_content(
side_nav_tab_id = "example_side_nav_tab_1",
material_tabs(
tabs = c(
"Customer Score" = "first_tab"
)
),
# Define tab content
material_tab_content(
tab_id = "first_tab",
material_row(
material_column(
width = 2,
material_card(
title = "",
depth = 6,
flexdashboard::gaugeOutput("churn_risk", width = "200px", height = "130px"),
tags$br(),
tags$br(),
sliderInput("range", "Churn Risk Range",min = 1, max = 100, value = c(70,100)),
material_dropdown(
input_id = "Branch",
label = "Branch",
choices = as.character(unique(churn_data_raw$Branch[order(churn_data_raw$predict.score[,2])])),
color = "#3c4858"
),
material_dropdown(
input_id = "account_Type",
label = "Account Type",
choices = as.character(unique(churn_data_raw$AccountType[order(churn_data_raw$predict.score[,2])])),
color = "#3c4858"
),
material_dropdown(
input_id = "customername",
label = "Customer Name",
choices = as.character(unique(churn_data_raw$CustomerName[order(churn_data_raw$predict.score[,2])])),
color = "#3c4858"
),
material_dropdown(
input_id = "accountno",
label = "Account No",
choices = as.character(unique(churn_data_raw$ContractID[order(churn_data_raw$predict.score[,2])])),
color = "#3c4858"
),
tags$br()
)
)
)
)
)
)
server <- function(input, output, session) {
churn_analysis_data_branch <- reactive({
churn_data_filtered <- churn_data_raw %>% dplyr::filter(churn_data_raw$predict.score[,2] >= input$range[1]/100 & churn_data_raw$predict.score[,2] <= input$range[2]/100 )
churn_data_filtered
})
observeEvent(
# c(
input$range,
# input$Branch,
# input$account_Type,
# input$customername,
# input$accountno
# ),
{
# if(input$range[1]< 0 | input$range[2]>100){
if(is.null(input$range)){
return(NULL)
}
churn_analysis_data_branch <- churn_analysis_data_branch()
update_material_dropdown(session, "Branch",
value = as.character(unique(churn_analysis_data_branch$Branch[order(churn_analysis_data_branch$predict.score[,2])]))[1],
choices = as.character(unique(churn_analysis_data_branch$Branch[order(churn_analysis_data_branch$predict.score[,2])]))
)
churn_analysis_data_acctype <- dplyr::filter(churn_analysis_data_branch(),Branch == input$Branch)
update_material_dropdown(
session,
"account_Type",
value = as.character(unique(churn_analysis_data_acctype$AccountType[order(churn_analysis_data_acctype$predict.score[,2])]))[1],
choices = as.character(unique(churn_analysis_data_acctype$AccountType[order(churn_analysis_data_acctype$predict.score[,2])]))
)
churn_analysis_data_custname <- dplyr::filter(churn_analysis_data_branch(),Branch == input$Branch & AccountType==input$account_Type)
update_material_dropdown(
session,
"customername",
value = as.character(unique(churn_analysis_data_custname$CustomerName[order(churn_analysis_data_custname$predict.score[,2])])[1]),
choices = unique(churn_analysis_data_custname$CustomerName[order(churn_analysis_data_custname$predict.score[,2])])
)
churn_analysis_data_custname <- dplyr::filter(churn_analysis_data_branch(),Branch == input$Branch & AccountType==input$account_Type & CustomerName==input$customername)
update_material_dropdown(
session,
"accountno",
value = as.character(unique(churn_analysis_data_custname$ContractID[order(churn_analysis_data_custname$predict.score[,2])])[1]),
choices = unique(churn_analysis_data_custname$ContractID[order(churn_analysis_data_custname$predict.score[,2])])
)
})
observeEvent(input$Branch, {
if(is.null(input$Branch)){
return(NULL)
}
churn_analysis_data_acctype <- dplyr::filter(churn_analysis_data_branch(),Branch == input$Branch)
update_material_dropdown(
session,
"account_Type",
value = as.character(unique(churn_analysis_data_acctype$AccountType[order(churn_analysis_data_acctype$predict.score[,2])]))[1],
choices = as.character(unique(churn_analysis_data_acctype$AccountType[order(churn_analysis_data_acctype$predict.score[,2])]))
)
churn_analysis_data_custname <- dplyr::filter(churn_analysis_data_branch(),Branch == input$Branch & AccountType==input$account_Type)
update_material_dropdown(
session,
"customername",
value = as.character(unique(churn_analysis_data_custname$CustomerName[order(churn_analysis_data_custname$predict.score[,2])])[1]),
choices = unique(churn_analysis_data_custname$CustomerName[order(churn_analysis_data_custname$predict.score[,2])])
)
churn_analysis_data_custname <- dplyr::filter(churn_analysis_data_branch(),Branch == input$Branch & AccountType==input$account_Type & CustomerName==input$customername)
update_material_dropdown(
session,
"accountno",
value = as.character(unique(churn_analysis_data_custname$ContractID[order(churn_analysis_data_custname$predict.score[,2])])[1]),
choices = unique(churn_analysis_data_custname$ContractID[order(churn_analysis_data_custname$predict.score[,2])])
)
})
observeEvent(input$account_Type, {
if(is.null(input$account_Type)){
return(NULL)
}
churn_analysis_data_custname <- dplyr::filter(churn_analysis_data_branch(),Branch == input$Branch & AccountType==input$account_Type)
update_material_dropdown(
session,
"customername",
value = as.character(unique(churn_analysis_data_custname$CustomerName[order(churn_analysis_data_custname$predict.score[,2])])[1]),
choices = unique(churn_analysis_data_custname$CustomerName[order(churn_analysis_data_custname$predict.score[,2])])
)
churn_analysis_data_custname <- dplyr::filter(churn_analysis_data_branch(),Branch == input$Branch & AccountType==input$account_Type & CustomerName==input$customername)
update_material_dropdown(
session,
"accountno",
value = as.character(unique(churn_analysis_data_custname$ContractID[order(churn_analysis_data_custname$predict.score[,2])])[1]),
choices = unique(churn_analysis_data_custname$ContractID[order(churn_analysis_data_custname$predict.score[,2])])
)
})
observeEvent(input$customername, {
if(is.null(input$customername == 0)){
return(NULL)
}
churn_analysis_data_custname <- dplyr::filter(churn_analysis_data_branch(),Branch == input$Branch & AccountType==input$account_Type & CustomerName==input$customername)
update_material_dropdown(
session,
"accountno",
value = as.character(unique(churn_analysis_data_custname$ContractID[order(churn_analysis_data_custname$predict.score[,2])])[1]),
choices = unique(churn_analysis_data_custname$ContractID[order(churn_analysis_data_custname$predict.score[,2])])
)
})
output$churn_risk <- renderGauge({
req(input$accountno)
selected_accountno <- churn_data_raw$ContractID[1]
selected_accountno <- input$accountno
# test_tbl_with_ids_predictions <- churn_data_raw %>%
# mutate(churn_prob = predictions$Yes)
customer_tbl <- churn_data_raw %>% dplyr::filter(ContractID == selected_accountno)
p1 <- flexdashboard::gauge(
round(customer_tbl$predict.score[,2]* 100, digits = 1),
min = 0,
max = 100,
gaugeSectors(
success = c(0,33),
warning = c(33, 66),
danger = c(67,100)
),
symbol = "%",
label = "Churn Risk"
)
})
}
shinyApp(ui = ui, server = server)
Gender | SeniorCitizen | Married | AccountPeriodinMonth | MobileBanking | Nationality | Branch | InternetBanking | Currency | State | City | Product | SectorCode | ConsumerComplaints | BusinessUnit | AccountType | AccountOfficer | No.OfDebitTrans | DebitTransAmt | AvgBalance | TotalCharges | Churn | predict.class | predict.score.0 | predict.score.1 | CustomerID | ContractID | Country | LeBook | CustomerName |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Female | 0 | Yes | 1 | No | KE | City Centre Branch | No | KES | Nairobi | Nairobi | CAA - CORPORATE | staff | No | Platinum Banking | CAA | JOB K KURGAT | 0 | 0 | 0 | 29.85 | 0 | 0 | 0.506 | 0.494 | 1000001 | 1001 | KE | 1 | AGUTA |
Male | 0 | No | 34 | Yes | IN | City Centre Branch | Yes | GBP | TamilNadu | Chennai | SBA - NIC SAVER | Private Enter./other non-fin corp | No | SME | SBA | ERIC M KASAMBA | 0 | 0 | 0 | 1889.5 | 0 | 0 | 0.994 | 0.006 | 1000001 | 1002 | KE | 1 | AGUTA |
Male | 0 | No | 2 | Yes | IN | City Centre Branch | Yes | KES | TamilNadu | Chennai | CAA - CORPORATE | staff | No | Platinum Banking | CAA | ERIC M KASAMBA | 2 | 49320 | 561120 | 108.15 | 1 | 1 | 0.382 | 0.618 | 1000001 | 1003 | KE | 1 | AGUTA |
Male | 0 | No | 45 | No | KE | City Centre Branch | Yes | GBP | Nairobi | Nairobi | SBA - NIC SAVER | Private Enter./other non-fin corp | Yes | SME | SBA | JENIFFER W. NJENGA | 4 | 60780 | 168397.5 | 1840.75 | 0 | 0 | 1 | 0 | 1000001 | 1004 | KE | 1 | AGUTA |
Female | 0 | No | 2 | Yes | IN | Junction Branch | No | GBP | TamilNadu | Chennai | CAA - CORPORATE | staff | No | Platinum Banking | CAA | JOB K KURGAT | 0 | 0 | 808480 | 151.65 | 1 | 1 | 0.09 | 0.91 | 1000001 | 1005 | KE | 1 | AGUTA |
Female | 0 | No | 8 | Yes | US | Junction Branch | No | GBP | Dallas | Texas | CAA - CORPORATE | staff | No | Platinum Banking | CAA | JOB K KURGAT | 0 | 0 | 379310 | 820.5 | 1 | 1 | 0.054 | 0.946 |