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": "Source",
"cursorPosition": "8,11",
"scrollLine": "0"
"cursorPosition": "27,0",
"scrollLine": "4"
}

View File

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

View File

@@ -30,12 +30,9 @@ source("modals.R")
# Define UI for application
ui <- navbarPage(
theme = shinytheme("cosmo"),
ui <- navbarPage(theme = shinytheme("cosmo"),
title = "LoanRisk",
panel1
)
panel1)
# Define server logic required to draw a histogram
server <- function(input, output) {
@@ -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"))
)
)
)
@@ -168,15 +162,19 @@ server <- function(input, output) {
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.")
}
)
@@ -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,7 +325,8 @@ 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()))
req(input$start_model_selection,
!is.null(dataset_with_eco()))
model_sel(dff = dataset_with_eco())
})
@@ -334,11 +335,13 @@ server <- function(input, output) {
input$start_model_selection
req(!is.null(selected_model()))
z<-predic_t(dff=dataset_with_eco(),
z <- predic_t(
dff = dataset_with_eco(),
gdpfor = gdp.forecast(),
prfor = pats.forecast(),
maxdate = maxdate(),
final.model= selected_model())
final.model = selected_model()
)
z
})
@@ -368,11 +371,9 @@ server <- function(input, output) {
)
)) %>%
filter(!is.na(name)) %>%
ggplot(aes(
x = name,
ggplot(aes(x = name,
y = value,
fill = name
)) +
fill = name)) +
geom_violin() +
geom_boxplot(width = 0.1,
color = "black",
@@ -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(
@@ -751,9 +750,7 @@ server <- function(input, output) {
# File uploading Module
observeEvent(input$uploadnew, {
showModal(
modal1
)
showModal(modal1)
})
observeEvent(input$closemodal1, {
@@ -765,15 +762,16 @@ server <- function(input, output) {
# Uploading temp file and collecting info about the columns
observeEvent(input$uploadfiles, {
df.tr <- reactive({
inFile <- input$transaction
if (is.null(inFile))
return(NULL)
df <- read.csv(inFile$datapath,
df <- read.csv(
inFile$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
quote = input$quote
)
df
})
@@ -781,24 +779,56 @@ server <- function(input, output) {
inFile <- input$collaterals
if (is.null(inFile))
return(NULL)
df <- read.csv(inFile$datapath,
df <- read.csv(
inFile$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
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("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("defaultflag",
"Select default flag column",
df.tr(),
session = getDefaultReactiveDomain())
updateVarSelectInput("transid", "Select id column", df.tr(), session = getDefaultReactiveDomain())
})
@@ -806,25 +836,25 @@ server <- function(input, output) {
# Modal 2
observeEvent(input$uploadfiles, {
showModal(
modal2
)
showModal(modal2)
})
observeEvent(input$confirmupload, {
transaction.dt(
{
df <- read.csv(input$transaction$datapath,
transaction.dt({
df <- read.csv(
input$transaction$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
quote = input$quote
)
head(df)
df <-
df %>%
rename(report_date=input$reportdate,
rename(
report_date = input$reportdate,
origination_date = input$origindate,
maturity_date = input$maturitydate,
asset_type = input$assettype,
@@ -833,7 +863,8 @@ server <- function(input, output) {
balance = input$balance,
loan_status = input$status,
default_flag = input$defaultflag,
id=input$transid)
id = input$transid
)
if (input$dateformat == "ymd") {
df$report_date <- ymd(df$report_date)
@@ -857,22 +888,21 @@ server <- function(input, output) {
}
df
}
)
})
collateral.dt(
{
df <- read.csv(input$collaterals$datapath,
collateral.dt({
df <- read.csv(
input$collaterals$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
quote = input$quote
)
df <-
df %>%
rename(id = input$collateralid,
collateral = input$collateralvalue)
df
}
)
})
removeModal()
})

182
app.R
View File

@@ -30,12 +30,9 @@ source("modals.R")
# Define UI for application
ui <- navbarPage(
theme = shinytheme("cosmo"),
ui <- navbarPage(theme = shinytheme("cosmo"),
title = "LoanRisk",
panel1
)
panel1)
# Define server logic required to draw a histogram
server <- function(input, output) {
@@ -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"))
)
)
)
@@ -168,15 +162,19 @@ server <- function(input, output) {
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.")
}
)
@@ -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,7 +325,8 @@ 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()))
req(input$start_model_selection,
!is.null(dataset_with_eco()))
model_sel(dff = dataset_with_eco())
})
@@ -334,11 +335,13 @@ server <- function(input, output) {
input$start_model_selection
req(!is.null(selected_model()))
z<-predic_t(dff=dataset_with_eco(),
z <- predic_t(
dff = dataset_with_eco(),
gdpfor = gdp.forecast(),
prfor = pats.forecast(),
maxdate = maxdate(),
final.model= selected_model())
final.model = selected_model()
)
z
})
@@ -368,11 +371,9 @@ server <- function(input, output) {
)
)) %>%
filter(!is.na(name)) %>%
ggplot(aes(
x = name,
ggplot(aes(x = name,
y = value,
fill = name
)) +
fill = name)) +
geom_violin() +
geom_boxplot(width = 0.1,
color = "black",
@@ -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(
@@ -751,9 +750,7 @@ server <- function(input, output) {
# File uploading Module
observeEvent(input$uploadnew, {
showModal(
modal1
)
showModal(modal1)
})
observeEvent(input$closemodal1, {
@@ -765,15 +762,16 @@ server <- function(input, output) {
# Uploading temp file and collecting info about the columns
observeEvent(input$uploadfiles, {
df.tr <- reactive({
inFile <- input$transaction
if (is.null(inFile))
return(NULL)
df <- read.csv(inFile$datapath,
df <- read.csv(
inFile$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
quote = input$quote
)
df
})
@@ -781,24 +779,56 @@ server <- function(input, output) {
inFile <- input$collaterals
if (is.null(inFile))
return(NULL)
df <- read.csv(inFile$datapath,
df <- read.csv(
inFile$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
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("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("defaultflag",
"Select default flag column",
df.tr(),
session = getDefaultReactiveDomain())
updateVarSelectInput("transid", "Select id column", df.tr(), session = getDefaultReactiveDomain())
})
@@ -806,25 +836,25 @@ server <- function(input, output) {
# Modal 2
observeEvent(input$uploadfiles, {
showModal(
modal2
)
showModal(modal2)
})
observeEvent(input$confirmupload, {
transaction.dt(
{
df <- read.csv(input$transaction$datapath,
transaction.dt({
df <- read.csv(
input$transaction$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
quote = input$quote
)
head(df)
df <-
df %>%
rename(report_date=input$reportdate,
rename(
report_date = input$reportdate,
origination_date = input$origindate,
maturity_date = input$maturitydate,
asset_type = input$assettype,
@@ -833,7 +863,8 @@ server <- function(input, output) {
balance = input$balance,
loan_status = input$status,
default_flag = input$defaultflag,
id=input$transid)
id = input$transid
)
if (input$dateformat == "ymd") {
df$report_date <- ymd(df$report_date)
@@ -857,22 +888,21 @@ server <- function(input, output) {
}
df
}
)
})
collateral.dt(
{
df <- read.csv(input$collaterals$datapath,
collateral.dt({
df <- read.csv(
input$collaterals$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
quote = input$quote
)
df <-
df %>%
rename(id = input$collateralid,
collateral = input$collateralvalue)
df
}
)
})
removeModal()
})