|
|
|
@ -30,30 +30,27 @@ source("modals.R") |
|
|
|
|
|
|
|
|
|
|
|
# Define UI for application |
|
|
|
ui <- navbarPage( |
|
|
|
theme = shinytheme("cosmo"), |
|
|
|
title = "LoanRisk", |
|
|
|
panel1 |
|
|
|
|
|
|
|
) |
|
|
|
ui <- navbarPage(theme = shinytheme("cosmo"), |
|
|
|
title = "LoanRisk", |
|
|
|
panel1) |
|
|
|
|
|
|
|
# Define server logic required to draw a histogram |
|
|
|
server <- function(input, output) { |
|
|
|
# Data |
|
|
|
|
|
|
|
# Some reactive values |
|
|
|
collateral.dt<-reactiveVal() |
|
|
|
transaction.dt<-reactiveVal() |
|
|
|
collateral.dt <- reactiveVal() |
|
|
|
transaction.dt <- reactiveVal() |
|
|
|
|
|
|
|
# Adding initial data to reactive values |
|
|
|
collateral.dt(read.csv("./data/collateral.csv") ) |
|
|
|
collateral.dt(read.csv("./data/collateral.csv")) |
|
|
|
|
|
|
|
|
|
|
|
transaction.dt(read.csv("./data/transactions.csv") ) |
|
|
|
transaction.dt(read.csv("./data/transactions.csv")) |
|
|
|
|
|
|
|
# Transforming Data |
|
|
|
new.data <- reactive({ |
|
|
|
ndt<- transaction.dt() |
|
|
|
ndt <- transaction.dt() |
|
|
|
withProgress(message = "Trying to read", |
|
|
|
detail = "Hope the handwriting is legible!", |
|
|
|
value = 0, |
|
|
|
@ -68,12 +65,8 @@ server <- function(input, output) { |
|
|
|
report_date = ymd(report_date) |
|
|
|
) %>% |
|
|
|
# Add age of loan, loan tenure in months, which are compulsory parameters |
|
|
|
mutate(age_of_asset_months = round(as.numeric( |
|
|
|
report_date - origination_date |
|
|
|
) / 30)) %>% |
|
|
|
mutate(loan_tenure_months = round(as.numeric( |
|
|
|
maturity_date - origination_date |
|
|
|
) / 30)) %>% |
|
|
|
mutate(age_of_asset_months = round(as.numeric(report_date - origination_date) / 30)) %>% |
|
|
|
mutate(loan_tenure_months = round(as.numeric(maturity_date - origination_date) / 30)) %>% |
|
|
|
group_by(id) %>% |
|
|
|
# Arranging to avoid mistakes in lag |
|
|
|
arrange(report_date) %>% |
|
|
|
@ -104,7 +97,6 @@ server <- function(input, output) { |
|
|
|
|
|
|
|
# Show uploaded data |
|
|
|
output$up_data <- renderDataTable({ |
|
|
|
|
|
|
|
DT::datatable( |
|
|
|
new.data(), |
|
|
|
extensions = c("Buttons"), |
|
|
|
@ -114,15 +106,17 @@ server <- function(input, output) { |
|
|
|
dom = 'Bfrtip', |
|
|
|
filter = list(position = 'top', clear = FALSE), |
|
|
|
buttons = list( |
|
|
|
list(extend = "csv", text = "Download Visible", filename = "page", |
|
|
|
exportOptions = list( |
|
|
|
modifier = list(page = "current") |
|
|
|
) |
|
|
|
list( |
|
|
|
extend = "csv", |
|
|
|
text = "Download Visible", |
|
|
|
filename = "page", |
|
|
|
exportOptions = list(modifier = list(page = "current")) |
|
|
|
), |
|
|
|
list(extend = "csv", text = "Download All", filename = "data", |
|
|
|
exportOptions = list( |
|
|
|
modifier = list(page = "all") |
|
|
|
) |
|
|
|
list( |
|
|
|
extend = "csv", |
|
|
|
text = "Download All", |
|
|
|
filename = "data", |
|
|
|
exportOptions = list(modifier = list(page = "all")) |
|
|
|
) |
|
|
|
) |
|
|
|
) |
|
|
|
@ -146,7 +140,7 @@ server <- function(input, output) { |
|
|
|
databaseID <- "IFS" |
|
|
|
startdate = min(new.data()$report_date) |
|
|
|
enddate = max(new.data()$report_date) |
|
|
|
country = countries[countries$Country == input$country,]$Alpha.2.code |
|
|
|
country = countries[countries$Country == input$country, ]$Alpha.2.code |
|
|
|
withProgress(message = "Extracting data from IMF", |
|
|
|
detail = "Hope their server is up!", |
|
|
|
value = 0, |
|
|
|
@ -154,7 +148,7 @@ server <- function(input, output) { |
|
|
|
setProgress(value = 1, message = "Trying to reach..") |
|
|
|
print(country) |
|
|
|
imf.data <- tryCatch( |
|
|
|
expr={ |
|
|
|
expr = { |
|
|
|
imf_data( |
|
|
|
databaseID, |
|
|
|
c("NGDP_NSA_XDC", |
|
|
|
@ -166,21 +160,25 @@ server <- function(input, output) { |
|
|
|
return_raw = FALSE, |
|
|
|
print_url = T, |
|
|
|
times = 3 |
|
|
|
) |
|
|
|
) |
|
|
|
}, |
|
|
|
error = function(e){ # Specifying error message |
|
|
|
error = function(e) { |
|
|
|
# Specifying error message |
|
|
|
showModal( |
|
|
|
modalDialog( |
|
|
|
"Error in IMF database. Sorry for the inconvenience. Can you please try again later?" |
|
|
|
) |
|
|
|
) |
|
|
|
message("Error with IMF database. This is usually temporary. Sorry for the inconvenience. Please try again later.") |
|
|
|
message( |
|
|
|
"Error with IMF database. This is usually temporary. Sorry for the inconvenience. Please try again later." |
|
|
|
) |
|
|
|
}, |
|
|
|
finally = { # Specifying final message |
|
|
|
finally = { |
|
|
|
# Specifying final message |
|
|
|
message("Error with IMF database. Please try again later.") |
|
|
|
} |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
setProgress(value = 2, message = "Done") |
|
|
|
}) |
|
|
|
|
|
|
|
@ -212,7 +210,7 @@ server <- function(input, output) { |
|
|
|
# removing rows with no macroeconomic data |
|
|
|
dataset_eco <- |
|
|
|
dataset_with_eco[!is.na(dataset_with_eco$gdp_lag) & |
|
|
|
!is.na(dataset_with_eco$prices_lag), ] |
|
|
|
!is.na(dataset_with_eco$prices_lag),] |
|
|
|
setProgress(value = 2, message = "working..") |
|
|
|
}) |
|
|
|
|
|
|
|
@ -284,15 +282,17 @@ server <- function(input, output) { |
|
|
|
dom = 'Bfrtip', |
|
|
|
filter = list(position = 'top', clear = FALSE), |
|
|
|
buttons = list( |
|
|
|
list(extend = "csv", text = "Download Visible", filename = "page", |
|
|
|
exportOptions = list( |
|
|
|
modifier = list(page = "current") |
|
|
|
) |
|
|
|
list( |
|
|
|
extend = "csv", |
|
|
|
text = "Download Visible", |
|
|
|
filename = "page", |
|
|
|
exportOptions = list(modifier = list(page = "current")) |
|
|
|
), |
|
|
|
list(extend = "csv", text = "Download All", filename = "data", |
|
|
|
exportOptions = list( |
|
|
|
modifier = list(page = "all") |
|
|
|
) |
|
|
|
list( |
|
|
|
extend = "csv", |
|
|
|
text = "Download All", |
|
|
|
filename = "data", |
|
|
|
exportOptions = list(modifier = list(page = "all")) |
|
|
|
) |
|
|
|
) |
|
|
|
) |
|
|
|
@ -325,8 +325,9 @@ server <- function(input, output) { |
|
|
|
|
|
|
|
# Model Selection. Using functions. In production these are to be converted to APIs |
|
|
|
selected_model <- reactive({ |
|
|
|
req(input$start_model_selection,!is.null(dataset_with_eco())) |
|
|
|
model_sel(dff=dataset_with_eco()) |
|
|
|
req(input$start_model_selection, |
|
|
|
!is.null(dataset_with_eco())) |
|
|
|
model_sel(dff = dataset_with_eco()) |
|
|
|
}) |
|
|
|
|
|
|
|
# Preparing tables with predictions. Using functions. In production these are to be converted to APIs |
|
|
|
@ -334,11 +335,13 @@ server <- function(input, output) { |
|
|
|
input$start_model_selection |
|
|
|
req(!is.null(selected_model())) |
|
|
|
|
|
|
|
z<-predic_t(dff=dataset_with_eco(), |
|
|
|
gdpfor=gdp.forecast(), |
|
|
|
prfor=pats.forecast(), |
|
|
|
maxdate=maxdate(), |
|
|
|
final.model= selected_model()) |
|
|
|
z <- predic_t( |
|
|
|
dff = dataset_with_eco(), |
|
|
|
gdpfor = gdp.forecast(), |
|
|
|
prfor = pats.forecast(), |
|
|
|
maxdate = maxdate(), |
|
|
|
final.model = selected_model() |
|
|
|
) |
|
|
|
z |
|
|
|
}) |
|
|
|
|
|
|
|
@ -368,11 +371,9 @@ server <- function(input, output) { |
|
|
|
) |
|
|
|
)) %>% |
|
|
|
filter(!is.na(name)) %>% |
|
|
|
ggplot(aes( |
|
|
|
x = name, |
|
|
|
y = value, |
|
|
|
fill = name |
|
|
|
)) + |
|
|
|
ggplot(aes(x = name, |
|
|
|
y = value, |
|
|
|
fill = name)) + |
|
|
|
geom_violin() + |
|
|
|
geom_boxplot(width = 0.1, |
|
|
|
color = "black", |
|
|
|
@ -459,7 +460,7 @@ server <- function(input, output) { |
|
|
|
|
|
|
|
output$exposure_on_default <- renderPlot({ |
|
|
|
input$update |
|
|
|
req(nrow(predicted_table())>0) |
|
|
|
req(nrow(predicted_table()) > 0) |
|
|
|
discount_rate_pa <- input$discount_rate |
|
|
|
|
|
|
|
withProgress(message = "Plotting", |
|
|
|
@ -550,9 +551,7 @@ server <- function(input, output) { |
|
|
|
"Please click an drag to check the probabilities between ranges of possible loss." |
|
|
|
), |
|
|
|
plotOutput("simres", |
|
|
|
brush = brushOpts( |
|
|
|
id = "sim_res_sel", direction = "x" |
|
|
|
)), |
|
|
|
brush = brushOpts(id = "sim_res_sel", direction = "x")), |
|
|
|
verbatimTextOutput("cumprob") |
|
|
|
), |
|
|
|
column( |
|
|
|
@ -571,10 +570,10 @@ server <- function(input, output) { |
|
|
|
# }) |
|
|
|
|
|
|
|
simdata <- reactive({ |
|
|
|
req(nrow(predicted_table())>0) |
|
|
|
req(nrow(predicted_table()) > 0) |
|
|
|
req(!is.null(input$discount_rate)) |
|
|
|
input$update |
|
|
|
collateral<-collateral.dt() |
|
|
|
collateral <- collateral.dt() |
|
|
|
discount_rate_pa <- input$discount_rate |
|
|
|
withProgress(message = "Monte Carlo Simulation", |
|
|
|
detail = "1000 simulations", |
|
|
|
@ -647,7 +646,7 @@ server <- function(input, output) { |
|
|
|
}) |
|
|
|
|
|
|
|
output$cumprob <- renderText({ |
|
|
|
req(!is.null(input$sim_res_sel), !is.null(simresdata())) |
|
|
|
req(!is.null(input$sim_res_sel),!is.null(simresdata())) |
|
|
|
hist.pro <- simresdata() |
|
|
|
pro_dens <- data.frame(hist.pro$x, hist.pro$y) |
|
|
|
res <- brushedPoints(pro_dens, input$sim_res_sel) |
|
|
|
@ -751,29 +750,28 @@ server <- function(input, output) { |
|
|
|
# File uploading Module |
|
|
|
|
|
|
|
observeEvent(input$uploadnew, { |
|
|
|
showModal( |
|
|
|
modal1 |
|
|
|
) |
|
|
|
showModal(modal1) |
|
|
|
}) |
|
|
|
|
|
|
|
observeEvent(input$closemodal1,{ |
|
|
|
observeEvent(input$closemodal1, { |
|
|
|
removeModal() |
|
|
|
}) |
|
|
|
observeEvent(input$closemodal2,{ |
|
|
|
observeEvent(input$closemodal2, { |
|
|
|
removeModal() |
|
|
|
}) |
|
|
|
|
|
|
|
# Uploading temp file and collecting info about the columns |
|
|
|
observeEvent(input$uploadfiles,{ |
|
|
|
|
|
|
|
observeEvent(input$uploadfiles, { |
|
|
|
df.tr <- reactive({ |
|
|
|
inFile <- input$transaction |
|
|
|
if (is.null(inFile)) |
|
|
|
return(NULL) |
|
|
|
df <- read.csv(inFile$datapath, |
|
|
|
header = input$header, |
|
|
|
sep = input$sep, |
|
|
|
quote = input$quote) |
|
|
|
df <- read.csv( |
|
|
|
inFile$datapath, |
|
|
|
header = input$header, |
|
|
|
sep = input$sep, |
|
|
|
quote = input$quote |
|
|
|
) |
|
|
|
df |
|
|
|
}) |
|
|
|
|
|
|
|
@ -781,98 +779,130 @@ server <- function(input, output) { |
|
|
|
inFile <- input$collaterals |
|
|
|
if (is.null(inFile)) |
|
|
|
return(NULL) |
|
|
|
df <- read.csv(inFile$datapath, |
|
|
|
header = input$header, |
|
|
|
sep = input$sep, |
|
|
|
quote = input$quote) |
|
|
|
df <- read.csv( |
|
|
|
inFile$datapath, |
|
|
|
header = input$header, |
|
|
|
sep = input$sep, |
|
|
|
quote = input$quote |
|
|
|
) |
|
|
|
df |
|
|
|
}) |
|
|
|
updateVarSelectInput("collateralid","Select id column", df.c(),session = getDefaultReactiveDomain()) |
|
|
|
updateVarSelectInput("collateralvalue","Select Collateral value column", df.c(),session = getDefaultReactiveDomain()) |
|
|
|
updateVarSelectInput("reportdate","Select report date column", df.tr(),session = getDefaultReactiveDomain()) |
|
|
|
updateVarSelectInput("origindate","Select origin date column", df.tr(),session = getDefaultReactiveDomain()) |
|
|
|
updateVarSelectInput("maturitydate","Select maturity date column", df.tr(),session = getDefaultReactiveDomain()) |
|
|
|
updateVarSelectInput("assettype","Select asset classifier column", df.tr(),session = getDefaultReactiveDomain()) |
|
|
|
updateVarSelectInput("customertype","Select customer classifier column", df.tr(),session = getDefaultReactiveDomain()) |
|
|
|
updateVarSelectInput("otherfact","Select any other classifier column", df.tr(),session = getDefaultReactiveDomain()) |
|
|
|
updateVarSelectInput("bureauscore","Select bureau score column", df.tr(),session = getDefaultReactiveDomain()) |
|
|
|
updateVarSelectInput("balance","Select asset balance column", df.tr(),session = getDefaultReactiveDomain()) |
|
|
|
updateVarSelectInput("status","Select loan status", df.tr(),session = getDefaultReactiveDomain()) |
|
|
|
updateVarSelectInput("defaultflag","Select default flag column", df.tr(),session = getDefaultReactiveDomain()) |
|
|
|
updateVarSelectInput("transid","Select id column", df.tr(),session = getDefaultReactiveDomain()) |
|
|
|
updateVarSelectInput("collateralid", "Select id column", df.c(), session = getDefaultReactiveDomain()) |
|
|
|
updateVarSelectInput("collateralvalue", |
|
|
|
"Select Collateral value column", |
|
|
|
df.c(), |
|
|
|
session = getDefaultReactiveDomain()) |
|
|
|
updateVarSelectInput("reportdate", |
|
|
|
"Select report date column", |
|
|
|
df.tr(), |
|
|
|
session = getDefaultReactiveDomain()) |
|
|
|
updateVarSelectInput("origindate", |
|
|
|
"Select origin date column", |
|
|
|
df.tr(), |
|
|
|
session = getDefaultReactiveDomain()) |
|
|
|
updateVarSelectInput("maturitydate", |
|
|
|
"Select maturity date column", |
|
|
|
df.tr(), |
|
|
|
session = getDefaultReactiveDomain()) |
|
|
|
updateVarSelectInput("assettype", |
|
|
|
"Select asset classifier column", |
|
|
|
df.tr(), |
|
|
|
session = getDefaultReactiveDomain()) |
|
|
|
updateVarSelectInput("customertype", |
|
|
|
"Select customer classifier column", |
|
|
|
df.tr(), |
|
|
|
session = getDefaultReactiveDomain()) |
|
|
|
updateVarSelectInput("otherfact", |
|
|
|
"Select any other classifier column", |
|
|
|
df.tr(), |
|
|
|
session = getDefaultReactiveDomain()) |
|
|
|
updateVarSelectInput("bureauscore", |
|
|
|
"Select bureau score column", |
|
|
|
df.tr(), |
|
|
|
session = getDefaultReactiveDomain()) |
|
|
|
updateVarSelectInput("balance", |
|
|
|
"Select asset balance column", |
|
|
|
df.tr(), |
|
|
|
session = getDefaultReactiveDomain()) |
|
|
|
updateVarSelectInput("status", "Select loan status", df.tr(), session = getDefaultReactiveDomain()) |
|
|
|
updateVarSelectInput("defaultflag", |
|
|
|
"Select default flag column", |
|
|
|
df.tr(), |
|
|
|
session = getDefaultReactiveDomain()) |
|
|
|
updateVarSelectInput("transid", "Select id column", df.tr(), session = getDefaultReactiveDomain()) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
# Modal 2 |
|
|
|
|
|
|
|
observeEvent(input$uploadfiles, { |
|
|
|
showModal( |
|
|
|
modal2 |
|
|
|
) |
|
|
|
showModal(modal2) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
observeEvent(input$confirmupload,{ |
|
|
|
transaction.dt( |
|
|
|
{ |
|
|
|
df <- read.csv(input$transaction$datapath, |
|
|
|
header = input$header, |
|
|
|
sep = input$sep, |
|
|
|
quote = input$quote) |
|
|
|
head(df) |
|
|
|
|
|
|
|
df<- |
|
|
|
df %>% |
|
|
|
rename(report_date=input$reportdate, |
|
|
|
origination_date=input$origindate, |
|
|
|
maturity_date=input$maturitydate, |
|
|
|
asset_type=input$assettype, |
|
|
|
customer_type=input$customertype, |
|
|
|
bureau_score_orig=input$bureauscore, |
|
|
|
balance=input$balance, |
|
|
|
loan_status=input$status, |
|
|
|
default_flag=input$defaultflag, |
|
|
|
id=input$transid) |
|
|
|
|
|
|
|
if(input$dateformat=="ymd"){ |
|
|
|
df$report_date<-ymd(df$report_date) |
|
|
|
df$origination_date<-ymd(df$origination_date) |
|
|
|
df$maturity_date<-ymd(df$maturity_date) |
|
|
|
} else { |
|
|
|
df$report_date<-dmy(df$report_date) |
|
|
|
df$origination_date<-dmy(df$origination_date) |
|
|
|
df$maturity_date<-dmy(df$maturity_date) |
|
|
|
} |
|
|
|
|
|
|
|
df$loan_status<-as.integer(df$loan_status) |
|
|
|
|
|
|
|
# Covert to factors |
|
|
|
df$asset_type<-as.factor(df$asset_type) |
|
|
|
df$customer_type<-as.factor(df$customer_type) |
|
|
|
if(!is.null(input$otherfact)){ |
|
|
|
for(i in 1:length(input$otherfact)){ |
|
|
|
df$input$otherfact[i]<-as.factor(df$input$otherfact[i]) |
|
|
|
} |
|
|
|
observeEvent(input$confirmupload, { |
|
|
|
transaction.dt({ |
|
|
|
df <- read.csv( |
|
|
|
input$transaction$datapath, |
|
|
|
header = input$header, |
|
|
|
sep = input$sep, |
|
|
|
quote = input$quote |
|
|
|
) |
|
|
|
head(df) |
|
|
|
|
|
|
|
df <- |
|
|
|
df %>% |
|
|
|
rename( |
|
|
|
report_date = input$reportdate, |
|
|
|
origination_date = input$origindate, |
|
|
|
maturity_date = input$maturitydate, |
|
|
|
asset_type = input$assettype, |
|
|
|
customer_type = input$customertype, |
|
|
|
bureau_score_orig = input$bureauscore, |
|
|
|
balance = input$balance, |
|
|
|
loan_status = input$status, |
|
|
|
default_flag = input$defaultflag, |
|
|
|
id = input$transid |
|
|
|
) |
|
|
|
|
|
|
|
if (input$dateformat == "ymd") { |
|
|
|
df$report_date <- ymd(df$report_date) |
|
|
|
df$origination_date <- ymd(df$origination_date) |
|
|
|
df$maturity_date <- ymd(df$maturity_date) |
|
|
|
} else { |
|
|
|
df$report_date <- dmy(df$report_date) |
|
|
|
df$origination_date <- dmy(df$origination_date) |
|
|
|
df$maturity_date <- dmy(df$maturity_date) |
|
|
|
} |
|
|
|
|
|
|
|
df$loan_status <- as.integer(df$loan_status) |
|
|
|
|
|
|
|
# Covert to factors |
|
|
|
df$asset_type <- as.factor(df$asset_type) |
|
|
|
df$customer_type <- as.factor(df$customer_type) |
|
|
|
if (!is.null(input$otherfact)) { |
|
|
|
for (i in 1:length(input$otherfact)) { |
|
|
|
df$input$otherfact[i] <- as.factor(df$input$otherfact[i]) |
|
|
|
} |
|
|
|
|
|
|
|
df |
|
|
|
} |
|
|
|
) |
|
|
|
|
|
|
|
df |
|
|
|
}) |
|
|
|
|
|
|
|
collateral.dt( |
|
|
|
{ |
|
|
|
df <- read.csv(input$collaterals$datapath, |
|
|
|
header = input$header, |
|
|
|
sep = input$sep, |
|
|
|
quote = input$quote) |
|
|
|
df<- |
|
|
|
df %>% |
|
|
|
rename(id=input$collateralid, |
|
|
|
collateral=input$collateralvalue) |
|
|
|
df |
|
|
|
} |
|
|
|
) |
|
|
|
collateral.dt({ |
|
|
|
df <- read.csv( |
|
|
|
input$collaterals$datapath, |
|
|
|
header = input$header, |
|
|
|
sep = input$sep, |
|
|
|
quote = input$quote |
|
|
|
) |
|
|
|
df <- |
|
|
|
df %>% |
|
|
|
rename(id = input$collateralid, |
|
|
|
collateral = input$collateralvalue) |
|
|
|
df |
|
|
|
}) |
|
|
|
removeModal() |
|
|
|
}) |
|
|
|
|
|
|
|
|