Repository navigation
Expand file tree
/
Copy pathserver.R
More file actions
386 lines (355 loc) · 12.5 KB
/
Copy pathserver.R
File metadata and controls
386 lines (355 loc) · 12.5 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# some libs for shinyapps to deploy correctly
library(shiny)
library(curl)
library(e1071)
library(data.table)
library(caret)
# models for caret, need here explicitly for shinyapps deployment
library(randomForest) # for rf
library(gbm) # for gbm
library(mboost) # for glmboost
library(klaR) # for nb
library(plyr)
library(dplyr)
library(psych)
library(purrr)
library(pROC)
library(yardstick)
# Disable shiny widget, from:
# https://groups.google.com/forum/#!topic/shiny-discuss/uSetp4TtW-s
disable <- function(x) {
if (inherits(x, 'shiny.tag')) {
if (x$name %in% c('input', 'select'))
x$attribs$disabled <- 'disabled'
x$children <- disable(x$children)
}
else if (is.list(x) && length(x) > 0) {
for (i in 1:length(x))
x[[i]] <- disable(x[[i]])
}
x
}
shinyServer(
function(input, output) {
## Data Upload
dataInput <- reactive({
req(input$upload)
ext <- tools::file_ext(input$upload$name)
switch(ext,
csv = vroom::vroom(input$upload$datapath, delim = ","),
validate("Invalid file; Please upload a .csv file")
)
})
output$target <- renderUI({
selectInput('target', 'Select the Outcome/Target Variable',
choices=as.list(colnames(dataInput())))
})
output$files <- renderTable(input$upload)
output$head <- DT::renderDT(
dataInput(), extensions = 'Buttons', filter = "top", editable=T, rownames=F,
options = list(
dom = 'Blfrtip',
buttons = c('copy', 'csv', 'excel', 'pdf', 'print'),
lengthMenu = list(c(10, 50, 100, -1), c(10, 50, 100, "All"))
)
)
encode_ordinal <- function(x, order = unique(x)) {
x <- as.numeric(factor(x, levels = order, exclude = NULL))
x
}
data <- reactiveVal()
observeEvent(input$upload, ignoreNULL = T, ignoreInit = T, {
file1 <- input$upload
if (is.null(file1)){return()}
data(read.csv(file=file1$datapath))
})
observeEvent(input$drop, {
claim <- data()
drop <- c(input$dropCols)
claim <- claim[, !names(claim) %in% drop]
data(claim)
})
observeEvent(input$preprocess, {
claim <- data()
claim <- claim[complete.cases(claim),]
colTypes <- map(claim, class)
for (col in colnames(claim)) {
if (colTypes[col] == 'character') {
claim[[col]] <- encode_ordinal(claim[[col]])
}
}
data(claim)
})
## Data Summary
output$dropSelected <- renderUI({
selectInput("dropCols", "Select the Columns you want to drop:",
choices=as.list(colnames(data())), multiple = T)
})
output$PredictorsSummaryOut <- renderTable({
describe(data()[, !(names(data()) %in% c(input$target))])
}, rownames = T)
output$OutcomeSummaryOut <- renderTable({
describe(data()[input$target])
}, rownames = T)
## Explore Data
# pairs plot - always
output$expPairsPlot <- renderPlot({
featurePlot(x=data(),
y=data()[input$target],
plot='pairs', auto.key=list(columns=2))
})
# generate variable selectors for individual plots
# ideas from https://gist.github.com/jcheng5/3239667
output$expXaxisVarSelector <- renderUI({
selectInput('expXaxisVar', 'Variable on x-axis',
choices=as.list(colnames(data())), selected=colnames(data())[1])
})
# generate variable selectors for individual plots
getYaxisVarSelector <- function(geom) {
# wy = wtih y, wo = without y (or disable)
widget <- selectInput('expYaxisVar', 'Variable on y-axis',
choices=as.list(colnames(data())), selected=colnames(data())[2])
wy <- widget
woy <- disable(widget)
switch(geom,
point = wy,
boxplot = wy,
histogram = woy,
density = woy,
jitter = wy
)
}
output$expYaxisVarSelector <- renderUI({
getYaxisVarSelector(input$singlePlotGeom)
})
output$expColorVarSelector <- renderUI({
selectInput('expColorVar', 'Variable to color by',
choices=as.list(c('None', colnames(data()))),
selected=input$target)
})
# create ggplot statement based on geom
add_ggplot <- function(geom) {
gx <- ggplot(data(), aes_string(x=input$expXaxisVar))
gxy <- ggplot(data(), aes_string(x=input$expXaxisVar, y=input$expYaxisVar))
switch(geom,
point = gxy,
boxplot = gxy,
histogram = gx,
density = gx,
jitter = gxy
)
}
# create ggplot geom
add_geom <- function(geom) {
switch(geom,
point = geom_point(aes_string(color=input$expColorVar)),
boxplot = geom_boxplot(aes_string(color=input$expColorVar)),
histogram = geom_histogram(aes_string(color=input$expColorVar)),
density = geom_density(aes_string(color=input$expColorVar)),
jitter = geom_jitter(aes_string(color=input$expColorVar))
)
}
output$expSinglePlot <- renderPlot({
g <- add_ggplot(input$singlePlotGeom) + add_geom(input$singlePlotGeom)
print(g)
})
## Prediction Model
f <- reactive({
as.formula(paste(input$target, "~."))
})
# create feature selection
output$featureSelectInput <- renderUI({
selectInput('featureSelect', 'Select features to generate model',
choices=as.list(colnames(data()[, !(names(data()) %in% c(input$target))])),
multiple = TRUE, selected=c(colnames(data())[1], colnames(data())[2], colnames(data())[3]))
})
output$machAlgorithm <- renderUI({
selectInput('machLearnAlgorithm',
'Select the model or machine learning algorithm',
choices= c('K-Nearest Neighbors' = 'knn',
'Generalized Linear Model (logit)' = 'glm',
'Random Forests (may take a few minutes)' = 'rf',
'Gradient Boosting' = 'gbm',
'Boosted Generalized Linear Model' = 'glmboost',
'Linear Discriminant Analysis' = 'lda',
'Naive Bayes' = 'nb'),
selected='knn')
})
#split the data into train and test
splitSlider <- reactive({
input$fracTrain / 100
})
trainRowIndex <- reactive({
sample(1:nrow(data()), splitSlider() * nrow(data()))
})
trainData <- reactive({
train_dt <- data()
train_dt <- train_dt[trainRowIndex(),]
})
testData <- reactive({
test_dt <- data()
test_dt <- test_dt[-trainRowIndex(),]
})
output$cntTrain <- renderText({
paste0("Training set: ", nrow(trainData()), " records")
})
output$cntTest <- renderText({
paste0("Test set: ", nrow(testData()), " records")
})
# apply model to training set
applyModel <- function(modelType, features) {
df <- trainData()
if (input$mltype == "clf") {
# df$input$target <- as.factor(df$input$target)
# Convert target variable from numeric to factor
df[[input$target]] <- as.factor(df[[input$target]])
if (modelType == 'gbm')
train(f(),
data=select(df, one_of(c(input$target, features))),
method=modelType, preProcess=input$preProcessMethods, verbose=F, metric='Accuracy')
else
train(f(),
data=select(df, one_of(c(input$target, features))),
method=modelType, preProcess=input$preProcessMethods, metric='Accuracy')
}
else {
if (modelType == 'gbm')
train(f(),
data=select(df, one_of(c(input$target, features))),
method=modelType, preProcess=input$preProcessMethods, verbose=F, metric='RMSE')
else
train(f(),
data=select(df, one_of(c(input$target, features))),
method=modelType, preProcess=input$preProcessMethods, metric='RMSE')
}
}
# reactive functions to run and evaluate model
runModel <- reactive({
applyModel(input$machLearnAlgorithm, input$featureSelect)
})
# if summary(fit) has names, use it, if not, do not
output$summaryModel <- renderPrint({
if (!is.null(names(summary(runModel()))))
print(summary(runModel()))
else
'Same as Final Model Fit above'
}, width = 10000)
# summary of final model
output$finalModel <- renderPrint({
print(runModel())
}, width = 10000)
## Prediction Model Evaluation
evalModel <- function(testData, features) {
predictions <- predict(runModel(), select(testData, one_of(features)))
truthes <- testData[, input$target]
# generate confusion matrix
if (input$mltype == "clf") {
print(confusionMatrix(predictions, as.factor(truthes)))
}
else {
# Convert target variable from factor to numeric
truthes <- as.numeric(truthes)
# calculate RMSE
rmse <- sqrt(mean((truthes - predictions)^2))
# calculate R^2
r2 <- cor(truthes, predictions)^2
# calculate MAE
mae <- mean(abs(truthes - predictions))
# calculate MASE
mase <- mean(abs(truthes - predictions))/mean(abs(diff(truthes)))
# calculate adjusted R^2
adjr2 <- 1 - (1 - r2)*(nrow(testData) - 1)/(nrow(testData) - length(features) - 1)
# return a data frame
print(data.frame("RMSE"=rmse, "R2"=r2, "MAE"=mae, "MASE"=mase, "AdjR2"=adjr2))
}
}
#training data accuracy
output$inSampleAccuracy <- renderPrint({
df <- trainData()
df <- if (input$mltype == "clf") {
# Convert target variable from numeric to factor
df[, input$target] <- as.factor(df[, input$target])
df
}
else
df
evalModel(df, input$featureSelect)
}, width = 10000)
output$inSamplePlot <- renderPlot({
df <- trainData()
df <- if (input$mltype == "clf") {
# Convert target variable from numeric to factor
df[, input$target] <- as.factor(df[, input$target])
df
}
else
df
predictions <- predict(runModel(), select(df, one_of(input$featureSelect)))
truthes <- df[, input$target]
truth_predicted <- data.frame(
obs = truthes,
pred = predictions
)
if (input$mltype == "clf") {
# plot confusion matrix
cm <- conf_mat(truth_predicted, obs, pred)
autoplot(cm, type='heatmap', scale_fill_gradient(low="#D6EAF8",high = "#2E86C1")) +
ggtitle("Confusion Matrix (Train)") +
theme(text = element_text(size=20), plot.title = element_text(hjust = 0.5))
}
else {
# Plot residuals vs fitted values using carat
ggplot(truth_predicted, aes(x=pred, y=obs)) +
geom_point() +
geom_abline(intercept=0, slope=1, color="red") +
labs(x="Predicted", y="Observed") +
ggtitle("Residuals vs Fitted Values (Train)") +
theme(text = element_text(size=20), plot.title = element_text(hjust = 0.5))
}
})
#testing data accuracy
output$outOfSampleAccuracy <- renderPrint({
df <- testData()
df <- if (input$mltype == "clf") {
# Convert target variable from numeric to factor
df[, input$target] <- as.factor(df[, input$target])
df
}
else
df
evalModel(df, input$featureSelect)
}, width = 10000)
output$outOfSamplePlot <- renderPlot({
df <- testData()
df <- if (input$mltype == "clf") {
# Convert target variable from numeric to factor
df[, input$target] <- as.factor(df[, input$target])
df
}
else
df
predictions <- predict(runModel(), select(df, one_of(input$featureSelect)))
truthes <- df[, input$target]
truth_predicted <- data.frame(
obs = truthes,
pred = predictions
)
if (input$mltype == "clf") {
# plot confusion matrix
cm <- conf_mat(truth_predicted, obs, pred)
autoplot(cm, type='heatmap', scale_fill_gradient(low="#D6EAF8",high = "#2E86C1")) +
ggtitle("Confusion Matrix (Test)") +
theme(text = element_text(size=20), plot.title = element_text(hjust = 0.5))
}
else {
# Plot residuals vs fitted values using carat
ggplot(truth_predicted, aes(x=pred, y=obs)) +
geom_point() +
geom_abline(intercept=0, slope=1, color="red") +
labs(x="Predicted", y="Observed") +
ggtitle("Residuals vs Fitted Values (Test)") +
theme(text = element_text(size=20), plot.title = element_text(hjust = 0.5))
}
})
}
)