Auto prediction and forecasting added

This commit is contained in:
2022-03-02 21:10:36 +01:00
parent e73b19da49
commit bdb1f315df
4 changed files with 366 additions and 306 deletions

View File

@@ -1,6 +1,6 @@
{ {
"source_window_id": "", "source_window_id": "",
"Source": "Source", "Source": "Source",
"cursorPosition": "8,11", "cursorPosition": "27,0",
"scrollLine": "0" "scrollLine": "4"
} }

View File

@@ -12,15 +12,15 @@
"properties": { "properties": {
"source_window_id": "", "source_window_id": "",
"Source": "Source", "Source": "Source",
"cursorPosition": "8,11", "cursorPosition": "27,0",
"scrollLine": "0" "scrollLine": "4"
}, },
"folds": "", "folds": "",
"lastKnownWriteTime": 1686049765, "lastKnownWriteTime": 1686050003,
"encoding": "UTF-8", "encoding": "UTF-8",
"collab_server": "", "collab_server": "",
"source_window": "", "source_window": "",
"last_content_update": 1686049765357, "last_content_update": 1686050003754,
"read_only": false, "read_only": false,
"read_only_alternatives": [] "read_only_alternatives": []
} }

View File

@@ -30,12 +30,9 @@ source("modals.R")
# Define UI for application # Define UI for application
ui <- navbarPage( ui <- navbarPage(theme = shinytheme("cosmo"),
theme = shinytheme("cosmo"),
title = "LoanRisk", title = "LoanRisk",
panel1 panel1)
)
# Define server logic required to draw a histogram # Define server logic required to draw a histogram
server <- function(input, output) { server <- function(input, output) {
@@ -68,12 +65,8 @@ server <- function(input, output) {
report_date = ymd(report_date) report_date = ymd(report_date)
) %>% ) %>%
# Add age of loan, loan tenure in months, which are compulsory parameters # Add age of loan, loan tenure in months, which are compulsory parameters
mutate(age_of_asset_months = round(as.numeric( mutate(age_of_asset_months = round(as.numeric(report_date - origination_date) / 30)) %>%
report_date - origination_date mutate(loan_tenure_months = round(as.numeric(maturity_date - origination_date) / 30)) %>%
) / 30)) %>%
mutate(loan_tenure_months = round(as.numeric(
maturity_date - origination_date
) / 30)) %>%
group_by(id) %>% group_by(id) %>%
# Arranging to avoid mistakes in lag # Arranging to avoid mistakes in lag
arrange(report_date) %>% arrange(report_date) %>%
@@ -104,7 +97,6 @@ server <- function(input, output) {
# Show uploaded data # Show uploaded data
output$up_data <- renderDataTable({ output$up_data <- renderDataTable({
DT::datatable( DT::datatable(
new.data(), new.data(),
extensions = c("Buttons"), extensions = c("Buttons"),
@@ -114,15 +106,17 @@ server <- function(input, output) {
dom = 'Bfrtip', dom = 'Bfrtip',
filter = list(position = 'top', clear = FALSE), filter = list(position = 'top', clear = FALSE),
buttons = list( buttons = list(
list(extend = "csv", text = "Download Visible", filename = "page", list(
exportOptions = list( extend = "csv",
modifier = list(page = "current") text = "Download Visible",
) filename = "page",
exportOptions = list(modifier = list(page = "current"))
), ),
list(extend = "csv", text = "Download All", filename = "data", list(
exportOptions = list( extend = "csv",
modifier = list(page = "all") text = "Download All",
) filename = "data",
exportOptions = list(modifier = list(page = "all"))
) )
) )
) )
@@ -168,15 +162,19 @@ server <- function(input, output) {
times = 3 times = 3
) )
}, },
error = function(e){ # Specifying error message error = function(e) {
# Specifying error message
showModal( showModal(
modalDialog( modalDialog(
"Error in IMF database. Sorry for the inconvenience. Can you please try again later?" "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.") message("Error with IMF database. Please try again later.")
} }
) )
@@ -284,15 +282,17 @@ server <- function(input, output) {
dom = 'Bfrtip', dom = 'Bfrtip',
filter = list(position = 'top', clear = FALSE), filter = list(position = 'top', clear = FALSE),
buttons = list( buttons = list(
list(extend = "csv", text = "Download Visible", filename = "page", list(
exportOptions = list( extend = "csv",
modifier = list(page = "current") text = "Download Visible",
) filename = "page",
exportOptions = list(modifier = list(page = "current"))
), ),
list(extend = "csv", text = "Download All", filename = "data", list(
exportOptions = list( extend = "csv",
modifier = list(page = "all") text = "Download All",
) filename = "data",
exportOptions = list(modifier = list(page = "all"))
) )
) )
) )
@@ -325,7 +325,8 @@ server <- function(input, output) {
# Model Selection. Using functions. In production these are to be converted to APIs # Model Selection. Using functions. In production these are to be converted to APIs
selected_model <- reactive({ selected_model <- reactive({
req(input$start_model_selection,!is.null(dataset_with_eco())) req(input$start_model_selection,
!is.null(dataset_with_eco()))
model_sel(dff = dataset_with_eco()) model_sel(dff = dataset_with_eco())
}) })
@@ -334,11 +335,13 @@ server <- function(input, output) {
input$start_model_selection input$start_model_selection
req(!is.null(selected_model())) req(!is.null(selected_model()))
z<-predic_t(dff=dataset_with_eco(), z <- predic_t(
dff = dataset_with_eco(),
gdpfor = gdp.forecast(), gdpfor = gdp.forecast(),
prfor = pats.forecast(), prfor = pats.forecast(),
maxdate = maxdate(), maxdate = maxdate(),
final.model= selected_model()) final.model = selected_model()
)
z z
}) })
@@ -368,11 +371,9 @@ server <- function(input, output) {
) )
)) %>% )) %>%
filter(!is.na(name)) %>% filter(!is.na(name)) %>%
ggplot(aes( ggplot(aes(x = name,
x = name,
y = value, y = value,
fill = name fill = name)) +
)) +
geom_violin() + geom_violin() +
geom_boxplot(width = 0.1, geom_boxplot(width = 0.1,
color = "black", color = "black",
@@ -550,9 +551,7 @@ server <- function(input, output) {
"Please click an drag to check the probabilities between ranges of possible loss." "Please click an drag to check the probabilities between ranges of possible loss."
), ),
plotOutput("simres", plotOutput("simres",
brush = brushOpts( brush = brushOpts(id = "sim_res_sel", direction = "x")),
id = "sim_res_sel", direction = "x"
)),
verbatimTextOutput("cumprob") verbatimTextOutput("cumprob")
), ),
column( column(
@@ -751,9 +750,7 @@ server <- function(input, output) {
# File uploading Module # File uploading Module
observeEvent(input$uploadnew, { observeEvent(input$uploadnew, {
showModal( showModal(modal1)
modal1
)
}) })
observeEvent(input$closemodal1, { observeEvent(input$closemodal1, {
@@ -765,15 +762,16 @@ server <- function(input, output) {
# Uploading temp file and collecting info about the columns # Uploading temp file and collecting info about the columns
observeEvent(input$uploadfiles, { observeEvent(input$uploadfiles, {
df.tr <- reactive({ df.tr <- reactive({
inFile <- input$transaction inFile <- input$transaction
if (is.null(inFile)) if (is.null(inFile))
return(NULL) return(NULL)
df <- read.csv(inFile$datapath, df <- read.csv(
inFile$datapath,
header = input$header, header = input$header,
sep = input$sep, sep = input$sep,
quote = input$quote) quote = input$quote
)
df df
}) })
@@ -781,24 +779,56 @@ server <- function(input, output) {
inFile <- input$collaterals inFile <- input$collaterals
if (is.null(inFile)) if (is.null(inFile))
return(NULL) return(NULL)
df <- read.csv(inFile$datapath, df <- read.csv(
inFile$datapath,
header = input$header, header = input$header,
sep = input$sep, sep = input$sep,
quote = input$quote) quote = input$quote
)
df df
}) })
updateVarSelectInput("collateralid", "Select id column", df.c(), session = getDefaultReactiveDomain()) updateVarSelectInput("collateralid", "Select id column", df.c(), session = getDefaultReactiveDomain())
updateVarSelectInput("collateralvalue","Select Collateral value column", df.c(),session = getDefaultReactiveDomain()) updateVarSelectInput("collateralvalue",
updateVarSelectInput("reportdate","Select report date column", df.tr(),session = getDefaultReactiveDomain()) "Select Collateral value column",
updateVarSelectInput("origindate","Select origin date column", df.tr(),session = getDefaultReactiveDomain()) df.c(),
updateVarSelectInput("maturitydate","Select maturity date column", df.tr(),session = getDefaultReactiveDomain()) session = getDefaultReactiveDomain())
updateVarSelectInput("assettype","Select asset classifier column", df.tr(),session = getDefaultReactiveDomain()) updateVarSelectInput("reportdate",
updateVarSelectInput("customertype","Select customer classifier column", df.tr(),session = getDefaultReactiveDomain()) "Select report date column",
updateVarSelectInput("otherfact","Select any other classifier column", df.tr(),session = getDefaultReactiveDomain()) df.tr(),
updateVarSelectInput("bureauscore","Select bureau score column", df.tr(),session = getDefaultReactiveDomain()) session = getDefaultReactiveDomain())
updateVarSelectInput("balance","Select asset balance 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("status", "Select loan status", df.tr(), session = getDefaultReactiveDomain())
updateVarSelectInput("defaultflag","Select default flag column", df.tr(),session = getDefaultReactiveDomain()) updateVarSelectInput("defaultflag",
"Select default flag column",
df.tr(),
session = getDefaultReactiveDomain())
updateVarSelectInput("transid", "Select id column", df.tr(), session = getDefaultReactiveDomain()) updateVarSelectInput("transid", "Select id column", df.tr(), session = getDefaultReactiveDomain())
}) })
@@ -806,25 +836,25 @@ server <- function(input, output) {
# Modal 2 # Modal 2
observeEvent(input$uploadfiles, { observeEvent(input$uploadfiles, {
showModal( showModal(modal2)
modal2
)
}) })
observeEvent(input$confirmupload, { observeEvent(input$confirmupload, {
transaction.dt( transaction.dt({
{ df <- read.csv(
df <- read.csv(input$transaction$datapath, input$transaction$datapath,
header = input$header, header = input$header,
sep = input$sep, sep = input$sep,
quote = input$quote) quote = input$quote
)
head(df) head(df)
df <- df <-
df %>% df %>%
rename(report_date=input$reportdate, rename(
report_date = input$reportdate,
origination_date = input$origindate, origination_date = input$origindate,
maturity_date = input$maturitydate, maturity_date = input$maturitydate,
asset_type = input$assettype, asset_type = input$assettype,
@@ -833,7 +863,8 @@ server <- function(input, output) {
balance = input$balance, balance = input$balance,
loan_status = input$status, loan_status = input$status,
default_flag = input$defaultflag, default_flag = input$defaultflag,
id=input$transid) id = input$transid
)
if (input$dateformat == "ymd") { if (input$dateformat == "ymd") {
df$report_date <- ymd(df$report_date) df$report_date <- ymd(df$report_date)
@@ -857,22 +888,21 @@ server <- function(input, output) {
} }
df df
} })
)
collateral.dt( collateral.dt({
{ df <- read.csv(
df <- read.csv(input$collaterals$datapath, input$collaterals$datapath,
header = input$header, header = input$header,
sep = input$sep, sep = input$sep,
quote = input$quote) quote = input$quote
)
df <- df <-
df %>% df %>%
rename(id = input$collateralid, rename(id = input$collateralid,
collateral = input$collateralvalue) collateral = input$collateralvalue)
df df
} })
)
removeModal() removeModal()
}) })

182
app.R
View File

@@ -30,12 +30,9 @@ source("modals.R")
# Define UI for application # Define UI for application
ui <- navbarPage( ui <- navbarPage(theme = shinytheme("cosmo"),
theme = shinytheme("cosmo"),
title = "LoanRisk", title = "LoanRisk",
panel1 panel1)
)
# Define server logic required to draw a histogram # Define server logic required to draw a histogram
server <- function(input, output) { server <- function(input, output) {
@@ -68,12 +65,8 @@ server <- function(input, output) {
report_date = ymd(report_date) report_date = ymd(report_date)
) %>% ) %>%
# Add age of loan, loan tenure in months, which are compulsory parameters # Add age of loan, loan tenure in months, which are compulsory parameters
mutate(age_of_asset_months = round(as.numeric( mutate(age_of_asset_months = round(as.numeric(report_date - origination_date) / 30)) %>%
report_date - origination_date mutate(loan_tenure_months = round(as.numeric(maturity_date - origination_date) / 30)) %>%
) / 30)) %>%
mutate(loan_tenure_months = round(as.numeric(
maturity_date - origination_date
) / 30)) %>%
group_by(id) %>% group_by(id) %>%
# Arranging to avoid mistakes in lag # Arranging to avoid mistakes in lag
arrange(report_date) %>% arrange(report_date) %>%
@@ -104,7 +97,6 @@ server <- function(input, output) {
# Show uploaded data # Show uploaded data
output$up_data <- renderDataTable({ output$up_data <- renderDataTable({
DT::datatable( DT::datatable(
new.data(), new.data(),
extensions = c("Buttons"), extensions = c("Buttons"),
@@ -114,15 +106,17 @@ server <- function(input, output) {
dom = 'Bfrtip', dom = 'Bfrtip',
filter = list(position = 'top', clear = FALSE), filter = list(position = 'top', clear = FALSE),
buttons = list( buttons = list(
list(extend = "csv", text = "Download Visible", filename = "page", list(
exportOptions = list( extend = "csv",
modifier = list(page = "current") text = "Download Visible",
) filename = "page",
exportOptions = list(modifier = list(page = "current"))
), ),
list(extend = "csv", text = "Download All", filename = "data", list(
exportOptions = list( extend = "csv",
modifier = list(page = "all") text = "Download All",
) filename = "data",
exportOptions = list(modifier = list(page = "all"))
) )
) )
) )
@@ -168,15 +162,19 @@ server <- function(input, output) {
times = 3 times = 3
) )
}, },
error = function(e){ # Specifying error message error = function(e) {
# Specifying error message
showModal( showModal(
modalDialog( modalDialog(
"Error in IMF database. Sorry for the inconvenience. Can you please try again later?" "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.") message("Error with IMF database. Please try again later.")
} }
) )
@@ -284,15 +282,17 @@ server <- function(input, output) {
dom = 'Bfrtip', dom = 'Bfrtip',
filter = list(position = 'top', clear = FALSE), filter = list(position = 'top', clear = FALSE),
buttons = list( buttons = list(
list(extend = "csv", text = "Download Visible", filename = "page", list(
exportOptions = list( extend = "csv",
modifier = list(page = "current") text = "Download Visible",
) filename = "page",
exportOptions = list(modifier = list(page = "current"))
), ),
list(extend = "csv", text = "Download All", filename = "data", list(
exportOptions = list( extend = "csv",
modifier = list(page = "all") text = "Download All",
) filename = "data",
exportOptions = list(modifier = list(page = "all"))
) )
) )
) )
@@ -325,7 +325,8 @@ server <- function(input, output) {
# Model Selection. Using functions. In production these are to be converted to APIs # Model Selection. Using functions. In production these are to be converted to APIs
selected_model <- reactive({ selected_model <- reactive({
req(input$start_model_selection,!is.null(dataset_with_eco())) req(input$start_model_selection,
!is.null(dataset_with_eco()))
model_sel(dff = dataset_with_eco()) model_sel(dff = dataset_with_eco())
}) })
@@ -334,11 +335,13 @@ server <- function(input, output) {
input$start_model_selection input$start_model_selection
req(!is.null(selected_model())) req(!is.null(selected_model()))
z<-predic_t(dff=dataset_with_eco(), z <- predic_t(
dff = dataset_with_eco(),
gdpfor = gdp.forecast(), gdpfor = gdp.forecast(),
prfor = pats.forecast(), prfor = pats.forecast(),
maxdate = maxdate(), maxdate = maxdate(),
final.model= selected_model()) final.model = selected_model()
)
z z
}) })
@@ -368,11 +371,9 @@ server <- function(input, output) {
) )
)) %>% )) %>%
filter(!is.na(name)) %>% filter(!is.na(name)) %>%
ggplot(aes( ggplot(aes(x = name,
x = name,
y = value, y = value,
fill = name fill = name)) +
)) +
geom_violin() + geom_violin() +
geom_boxplot(width = 0.1, geom_boxplot(width = 0.1,
color = "black", color = "black",
@@ -550,9 +551,7 @@ server <- function(input, output) {
"Please click an drag to check the probabilities between ranges of possible loss." "Please click an drag to check the probabilities between ranges of possible loss."
), ),
plotOutput("simres", plotOutput("simres",
brush = brushOpts( brush = brushOpts(id = "sim_res_sel", direction = "x")),
id = "sim_res_sel", direction = "x"
)),
verbatimTextOutput("cumprob") verbatimTextOutput("cumprob")
), ),
column( column(
@@ -751,9 +750,7 @@ server <- function(input, output) {
# File uploading Module # File uploading Module
observeEvent(input$uploadnew, { observeEvent(input$uploadnew, {
showModal( showModal(modal1)
modal1
)
}) })
observeEvent(input$closemodal1, { observeEvent(input$closemodal1, {
@@ -765,15 +762,16 @@ server <- function(input, output) {
# Uploading temp file and collecting info about the columns # Uploading temp file and collecting info about the columns
observeEvent(input$uploadfiles, { observeEvent(input$uploadfiles, {
df.tr <- reactive({ df.tr <- reactive({
inFile <- input$transaction inFile <- input$transaction
if (is.null(inFile)) if (is.null(inFile))
return(NULL) return(NULL)
df <- read.csv(inFile$datapath, df <- read.csv(
inFile$datapath,
header = input$header, header = input$header,
sep = input$sep, sep = input$sep,
quote = input$quote) quote = input$quote
)
df df
}) })
@@ -781,24 +779,56 @@ server <- function(input, output) {
inFile <- input$collaterals inFile <- input$collaterals
if (is.null(inFile)) if (is.null(inFile))
return(NULL) return(NULL)
df <- read.csv(inFile$datapath, df <- read.csv(
inFile$datapath,
header = input$header, header = input$header,
sep = input$sep, sep = input$sep,
quote = input$quote) quote = input$quote
)
df df
}) })
updateVarSelectInput("collateralid", "Select id column", df.c(), session = getDefaultReactiveDomain()) updateVarSelectInput("collateralid", "Select id column", df.c(), session = getDefaultReactiveDomain())
updateVarSelectInput("collateralvalue","Select Collateral value column", df.c(),session = getDefaultReactiveDomain()) updateVarSelectInput("collateralvalue",
updateVarSelectInput("reportdate","Select report date column", df.tr(),session = getDefaultReactiveDomain()) "Select Collateral value column",
updateVarSelectInput("origindate","Select origin date column", df.tr(),session = getDefaultReactiveDomain()) df.c(),
updateVarSelectInput("maturitydate","Select maturity date column", df.tr(),session = getDefaultReactiveDomain()) session = getDefaultReactiveDomain())
updateVarSelectInput("assettype","Select asset classifier column", df.tr(),session = getDefaultReactiveDomain()) updateVarSelectInput("reportdate",
updateVarSelectInput("customertype","Select customer classifier column", df.tr(),session = getDefaultReactiveDomain()) "Select report date column",
updateVarSelectInput("otherfact","Select any other classifier column", df.tr(),session = getDefaultReactiveDomain()) df.tr(),
updateVarSelectInput("bureauscore","Select bureau score column", df.tr(),session = getDefaultReactiveDomain()) session = getDefaultReactiveDomain())
updateVarSelectInput("balance","Select asset balance 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("status", "Select loan status", df.tr(), session = getDefaultReactiveDomain())
updateVarSelectInput("defaultflag","Select default flag column", df.tr(),session = getDefaultReactiveDomain()) updateVarSelectInput("defaultflag",
"Select default flag column",
df.tr(),
session = getDefaultReactiveDomain())
updateVarSelectInput("transid", "Select id column", df.tr(), session = getDefaultReactiveDomain()) updateVarSelectInput("transid", "Select id column", df.tr(), session = getDefaultReactiveDomain())
}) })
@@ -806,25 +836,25 @@ server <- function(input, output) {
# Modal 2 # Modal 2
observeEvent(input$uploadfiles, { observeEvent(input$uploadfiles, {
showModal( showModal(modal2)
modal2
)
}) })
observeEvent(input$confirmupload, { observeEvent(input$confirmupload, {
transaction.dt( transaction.dt({
{ df <- read.csv(
df <- read.csv(input$transaction$datapath, input$transaction$datapath,
header = input$header, header = input$header,
sep = input$sep, sep = input$sep,
quote = input$quote) quote = input$quote
)
head(df) head(df)
df <- df <-
df %>% df %>%
rename(report_date=input$reportdate, rename(
report_date = input$reportdate,
origination_date = input$origindate, origination_date = input$origindate,
maturity_date = input$maturitydate, maturity_date = input$maturitydate,
asset_type = input$assettype, asset_type = input$assettype,
@@ -833,7 +863,8 @@ server <- function(input, output) {
balance = input$balance, balance = input$balance,
loan_status = input$status, loan_status = input$status,
default_flag = input$defaultflag, default_flag = input$defaultflag,
id=input$transid) id = input$transid
)
if (input$dateformat == "ymd") { if (input$dateformat == "ymd") {
df$report_date <- ymd(df$report_date) df$report_date <- ymd(df$report_date)
@@ -857,22 +888,21 @@ server <- function(input, output) {
} }
df df
} })
)
collateral.dt( collateral.dt({
{ df <- read.csv(
df <- read.csv(input$collaterals$datapath, input$collaterals$datapath,
header = input$header, header = input$header,
sep = input$sep, sep = input$sep,
quote = input$quote) quote = input$quote
)
df <- df <-
df %>% df %>%
rename(id = input$collateralid, rename(id = input$collateralid,
collateral = input$collateralvalue) collateral = input$collateralvalue)
df df
} })
)
removeModal() removeModal()
}) })