-
Notifications
You must be signed in to change notification settings - Fork 1
/
tool.R
226 lines (197 loc) · 5.39 KB
/
tool.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
options(scipen=999)
# Load libraries
library('shiny')
library('InformationValue')
library('xgboost')
library('caret')
library('plotly')
library('xgboost')
library('dplyr')
library('arules')
library('smbinning')
library('lubridate')
library('data.table')
# Seed for model comparisons
set.seed(1)
# Function to set home directory
defaultDir = '/home/user/cpls'
csf <- function() {
cmdArgs = commandArgs(trailingOnly = FALSE)
needle = "--file="
match = grep(needle, cmdArgs)
if (length(match) > 0) {
# Rscript via command line
return(normalizePath(sub(needle, "", cmdArgs[match])))
} else {
ls_vars = ls(sys.frames()[[1]])
if ("fileName" %in% ls_vars) {
# Source'd via RStudio
return(normalizePath(sys.frames()[[1]]$fileName))
} else {
if (!is.null(sys.frames()[[1]]$ofile)) {
# Source'd via R console
return(normalizePath(sys.frames()[[1]]$ofile))
} else {
# RStudio Run Selection
return(normalizePath(rstudioapi::getActiveDocumentContext()$path))
}
}
}
}
dir <- tryCatch(dirname(csf()),
error = function(e) {
defaultDir
}
)
if (is.null(dir) | length(dir) == 0) {
dir <- defaultDir
}
if(!dir.exists(dir)) {
err('Unable to determine home directory')
} else {
setwd(dir)
}
# Load helper functions
source('scripts/funcs.R')
server <- function(input, output, session) {
# Load init scripts
output$loadInit = reactive({
# Initialize
source('scripts/initTool.R')
return(1)
})
outputOptions(output, 'loadInit', suspendWhenHidden=FALSE)
base <- reactive ({
if (input$base != '') {
stats[eval(parse(text=input$base))]
} else {
stats
}
})
# Filter test notes using filter text given
data <- reactive ({
# User filter
if (input$filter != '') {
base()[eval(parse(text=input$filter))]
} else {
base()
}
})
totalNotes <- reactive ({
nrow(base())
})
cash <- reactive ({
merge(x = ph, y = data()[,.(id)], by = "id")
})
xirr <- reactive ({
cashOut <- cash()[, .(Month=min(Month),Payment=-max(Principal)), keyby = id]
cashIn <- cash()[,.(Month,Payment=Payment*.99)]
cashFlow <- rbind(cashIn,cashOut[,.(Month,Payment)])
xIRR(cashFlow[, .(Payment=sum(Payment)), keyby = Month]) * 100
})
output$summary <-renderUI({
filteredNotes <- nrow(data())
pct <- round(filteredNotes/totalNotes()*100,2)
co <- round(prop.table(table(data()$loan_status))[1],2)*100
age <- round(mean(data()$aol),2)
rate <- round(mean(data()$intRate),2)
fluidRow(
wellPanel(
fluidRow(
column(4,paste('Filtered Notes:',printNumber(filteredNotes))),
column(4,paste('Total Notes:',printNumber(totalNotes()))),
column(4,paste('Filtered Percent: ',pct,'%',sep=''))
),
fluidRow(
column(4,paste('XIRR: ',xirr(),'%',sep='')),
column(4,paste('Average Rate: ',rate,'%',sep='')),
column(4,paste('Age:',age,' Months'))
),
fluidRow(
column(4,paste('Charged Off: ',co,'%',sep='')),
column(4,paste('Late 16: ','l16','%',sep='')),
column(4,paste('Late 31: ','l31','%',sep=''))
)
)
)
})
# output$noteCount <- renderText({
# nrow(data())
# })
# output$plotROC <- renderPlot({
# plotROC(actuals=data()$label,predictedScores=data()$model)
# })
output$cm <- renderPrint({
caret::confusionMatrix(data()$class,data()$loan_status,'Fully Paid')
})
}
textareaInput <- function(inputId, label, value="", placeholder="", rows=2){
tagList(
div(strong(label), style="margin-top: 5px;"),
tags$style(type="text/css", "textarea {width:100%; margin-top: 5px;}"),
tags$textarea(id = inputId, placeholder = placeholder, rows = rows, value))
}
ui <- (
fluidPage(
tags$head(
tags$style(type="text/css","
.row {
margin-left: 10px;
margin-right: 10px;
margin-top: 10px;
}
.inline {
display: inline-block;
}
.alert {
margin-left: 20px;
}
.col-sm-6 {
padding-left: 0px;
padding-right: 10px;
}
textarea {
width: 100%;
margin-top: 5px;
border-radius: 4px;
border: 1px solid #cccccc;
outline: none;
resize: none;
}
")
),
verticalLayout(
fluidRow(
wellPanel(
conditionalPanel(
condition = "output.loadInit != 1",
h5('Loading data. Please be patient...')
),
fluidRow(
column(6,textareaInput("base", "Base", rows=4)),
column(6,textareaInput("filter", "Filter", rows=4))
),
fluidRow(
submitButton("Submit")
)
)
),
mainPanel(width=12,
tabsetPanel(type = "tabs",
tabPanel("Performance",
uiOutput('summary'),
fluidRow(
verbatimTextOutput ("cm")
)
# fluidRow(
# plotOutput("plotROC")
# )
),
tabPanel("Filter Builder", verbatimTextOutput("tesasdft")),
tabPanel("Loan Statistics", verbatimTextOutput("tesadfsdft"))
)
)
)
)
)
shinyApp(ui = ui, server = server)