-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathserver model.R
More file actions
403 lines (353 loc) · 13.9 KB
/
Copy pathserver model.R
File metadata and controls
403 lines (353 loc) · 13.9 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
models_df <- reactiveValues(df = NULL, # Load the selected data frame
var_list = NULL, # Create a variable list
independent_var = NULL, # Create the independent variables list
var_dep_class = NULL # The class of the dependent variable
)
# Select the dataset
observeEvent({
input$var_modify
input_df$names_list
},{
if(length(input_df$names_list[which(input_df$df_class == "Data Frame")]) == 0){
output$models1_df_list <- renderUI({
output$model_tab_input <- reactive("0")
outputOptions(output, "model_tab_input", suspendWhenHidden = FALSE)
models_df$var_list <- models_df$df <- NULL
showModal(modalDialog(
title = "Warning - No Available Data Frame",
HTML(paste("No available data frame in the platform",
"Use the Data tab to load data",
sep = "<br/>")
), size = "s"
))
output$models1_df_list <- renderUI({
selectInput("models1_select_df", "Select Dataset",
choices = "NA"
)
})
})
} else if(length(input_df$names_list[which(input_df$df_class == "Data Frame")]) > 0){
output$model_tab_input <- reactive("1")
outputOptions(output, "model_tab_input", suspendWhenHidden = FALSE)
output$models1_df_list <- renderUI({
selectInput("models1_select_df", "Select Dataset",
choices = input_df$names_list[which(input_df$df_class == "Data Frame")]
)
})
}
})
# Update the dataset selection
observeEvent({
input$var_modify
input$models1_select_df
},{
if(length(input_df$names_list[which(input_df$df_class == "Data Frame")]) > 0){
output$model_tab_input <- reactive("1")
outputOptions(output, "model_tab_input", suspendWhenHidden = FALSE)
models_df$df <- input_df$df_list[[which(input_df$names_list == input$models1_select_df)]]
} else {
output$model_tab_input <- reactive("0")
outputOptions(output, "model_tab_input", suspendWhenHidden = FALSE)
models_df$df <- NULL
}
})
# Dependent variable
observeEvent({
input$var_modify
input$models1_select_df
}, {
if(!is.null(models_df$df)){
models_df$var_list <- names(models_df$df)
output$model_tab_ind <- reactive("1")
outputOptions(output, "model_tab_ind", suspendWhenHidden = FALSE)
output$models1_var_list <- renderUI({
selectInput("models1_select_var", "Select the Dependent Variable",
choices = c("Select Variable",models_df$var_list)
)
})
} else if(is.null(models_df$df)){
models_df$var_list <- NULL
output$model_tab_ind <- reactive("0")
outputOptions(output, "model_tab_ind", suspendWhenHidden = FALSE)
}
})
# Independent variable
observeEvent(input$models1_select_var, {
if(input$models1_select_var != "Select Variable"){
models_df$var_dep_class <- class(models_df$df[,which(names(models_df$df) == input$models1_select_var)])
models_df$independent_var <- setdiff(names(models_df$df), c(input$models1_select_var, "name"))
output$model_tab_ind <- reactive("1")
outputOptions(output, "model_tab_ind", suspendWhenHidden = FALSE)
output$models1_independent_list <- renderUI({
pickerInput(inputId = "models1_independent",
label = "Select the Independent Variable",
choices = models_df$independent_var, options = list(`actions-box` = TRUE),
multiple = TRUE,
selected = models_df$independent_var)
})
} else if(input$models1_select_var == "Select Variable"){
models_df$independent_var <- NULL
models_df$var_dep_class <- NULL
output$model_tab_ind <- reactive("0")
outputOptions(output, "model_tab_ind", suspendWhenHidden = FALSE)
}
})
observeEvent(models_df$var_dep_class,{
if(models_df$var_dep_class == "factor"){
if(levels(models_df$df[,which(names(models_df$df) == input$models1_select_var)]) == 2){
output$model_binomial <- reactive("1") # set condition for binomial model
outputOptions(output, "model_binomial", suspendWhenHidden = FALSE)
} else if(levels(models_df$df[,which(names(models_df$df) == input$models1_select_var)]) > 2){
output$model_binomial <- reactive("2") # set condition for multinomial model
outputOptions(output, "model_binomial", suspendWhenHidden = FALSE)
} else {
output$model_binomial <- reactive("0") # not engough levels for binomial/multinomial
outputOptions(output, "model_binomial", suspendWhenHidden = FALSE)
}
output$dep_var_class <- reactive("1") # flag for factor variable
outputOptions(output, "dep_var_class", suspendWhenHidden = FALSE)
} else if (models_df$var_dep_class == "numeric" |
models_df$var_dep_class == "integer") {
output$dep_var_class <- reactive("2") # flag for numeric/integer variable
outputOptions(output, "dep_var_class", suspendWhenHidden = FALSE)
#
output$model_binomial <- reactive("0") # reseting the binomial flag
outputOptions(output, "model_binomial", suspendWhenHidden = FALSE)
}
})
#------------------------------ Setting the H2o Package -------------------------------------
h2o_df <- reactiveValues(status = FALSE,
free_mem = NULL,
num_cpus = NULL,
df = NULL,
class = NULL,
var_factor = NULL,
var_numeric = NULL,
df.h2o = NULL,
train = NULL,
test = NULL,
valud = NULL,
x = NULL,
y = NULL)
observeEvent({
input$model_package
},{
if("H2O" %in% input$model_package){
if(!"h2o" %in% installed.packages()){
showModal(modalDialog(
title = "Warning - H2O is not Available",
HTML(paste("The H2O package is not installed.",
"Please install the package to continue.",
sep = "<br/>")
), size = "s"
))
output$h2o_flag <- reactive("0")
outputOptions(output, "h2o_flag", suspendWhenHidden = FALSE)
} else if("h2o" %in% installed.packages() & !"package:h2o" %in% search()){
output$h2o_flag <- reactive("1")
print("Need to Load")
outputOptions(output, "h2o_flag", suspendWhenHidden = FALSE)
} else if("h2o" %in% installed.packages() & "package:h2o" %in% search()){
output$h2o_flag <- reactive("2")
outputOptions(output, "h2o_flag", suspendWhenHidden = FALSE)
print("No Need to Load")
}
} else {
print("YYY")
}
})
# Load H2O
observeEvent(input$load_h2o,{
require(h2o)
if ("h2o" %in% installed.packages() & "package:h2o" %in% search()){
output$h2o_flag <- reactive("2")
outputOptions(output, "h2o_flag", suspendWhenHidden = FALSE)
} else if("h2o" %in% installed.packages() & !"package:h2o" %in% search()){
showModal(modalDialog(
title = "Warning - Failed to Load H2O",
HTML(paste("Could not load H2O, please check if the package was installed currectly",
"For further information, please check H2O User Documentation:",
"https://h2o-release.s3.amazonaws.com/h2o/rel-weierstrass/3/index.html",
sep = "<br/>")
), size = "s"
))
}
})
# Install H2O
observeEvent(input$install_h2o,{
# The following two commands remove any previously installed H2O packages for R.
if ("package:h2o" %in% search()) { detach("package:h2o", unload=TRUE) }
if ("h2o" %in% rownames(installed.packages())) { remove.packages("h2o") }
# Next, we download packages that H2O depends on.
pkgs <- c("statmod","RCurl","jsonlite")
for (pkg in pkgs) {
if (! (pkg %in% rownames(installed.packages()))) { install.packages(pkg) }
}
# Now we download, install and initialize the H2O package for R.
install.packages("h2o", type="source", repos="https://h2o-release.s3.amazonaws.com/h2o/rel-weierstrass/3/R")
if ("h2o" %in% installed.packages() & !"package:h2o" %in% search()){
output$h2o_flag <- reactive("1")
outputOptions(output, "h2o_flag", suspendWhenHidden = FALSE)
} else if(!"h2o" %in% installed.packages()){
install.packages("h2o")
if ("h2o" %in% installed.packages() & !"package:h2o" %in% search()){
output$h2o_flag <- reactive("1")
outputOptions(output, "h2o_flag", suspendWhenHidden = FALSE)
}
}else if(!"h2o" %in% installed.packages()){
showModal(modalDialog(
title = "Warning - Installation Failed",
HTML(paste("There was a problem to installed H2O.",
"For further information, please check H2O User Documentation:",
"https://h2o-release.s3.amazonaws.com/h2o/rel-weierstrass/3/index.html",
sep = "<br/>")
), size = "s"
))
}
})
output$h2o_into_ram <- renderUI({
sliderInput("max_mem", "Set the Max Memory Size:",
min = 1, max = ceiling(get_free_ram() / 1024 ^ 2),
value = ceiling(get_free_ram() / 1024 ^ 2))
})
output$available_memory <- renderValueBox({
valueBox(
paste(round(get_free_ram() / 1024 ^ 2,2), "GB", sep = ""), "Free Physical Memory",
icon = icon("microchip"),
color = "purple"
)
})
observeEvent(input$h2o_start, {
if(input$h2o_start){
mem <- NULL
print(input$max_mem)
if(is.null(mem)){mem <- 1}
h2o.init(nthreads=-1 , max_mem_size = paste(mem, "g", sep = ""))
h2o.removeAll()
output$h2o_flag <- reactive("3")
outputOptions(output, "h2o_flag", suspendWhenHidden = FALSE)
h2o_df$status <- h2o.clusterIsUp()
cluster_status <- h2o.clusterStatus()
h2o_df$free_mem <- as.numeric(cluster_status$free_mem)
h2o_df$num_cpus <- as.numeric(cluster_status$num_cpus)
}
})
output$h2o_status_box <- renderValueBox({
valueBox(
ifelse(h2o_df$status, "Connected","Disconnected" ), "H2O Status", icon = icon("signal"),
color = ifelse(h2o_df$status, "green","red" )
)
})
output$h2o_cluster_mem <- renderValueBox({
valueBox(
paste(round((h2o_df$free_mem / 1024^3), 2), "GB", sep = ""),
"H2O Cluster Total Memory", icon = icon("microchip"),
color = "maroon"
)
})
output$h2o_cpu <- renderValueBox({
valueBox(
h2o_df$num_cpus,
"Number of CPUs in Use", icon = icon("signal"),
color = "light-blue"
)
})
output$h2o_load_data <- renderUI({
if(is.null(h2o_df$df.h2o)){
actionButton("h2o_df_load", "Load to H2O")
} else{
actionButton("h2o_df_load", "Reload to H2O")
}
})
observeEvent(input$h2o_df_load,{
h2o.removeAll()
h2o_df$df.h2o <- as.h2o(models_df$df)
h2o_df$y <- h2o_df$x <- h2o_df$train <- h2o_df$test <- h2o_df$valid <- NULL
h2o_df$y <- match(input$models1_select_var, names(h2o_df$df.h2o))
h2o_df$x <- match(input$models1_independent, names(h2o_df$df.h2o))
print(h2o_df$y)
print(h2o_df$x)
if(input$h2o_validation){
splits <- h2o.splitFrame(
data = h2o_df$df.h2o,
ratios = c(input$h2o_split_v[1],(input$h2o_split_v[2] - input$h2o_split_v[1])),
destination_frames = c("train", "valid", "test"), seed = 1234
)
h2o_df$train <- splits[[1]]
h2o_df$valid <- splits[[2]]
h2o_df$test <- splits[[3]]
} else {
splits <- h2o.splitFrame(
data = h2o_df$df.h2o,
ratios = c(input$h2o_split),
destination_frames = c("train", "test"), seed = 1234
)
h2o_df$train <- splits[[1]]
h2o_df$test <- splits[[2]]
}
})
observeEvent({
h2o_df$df.h2o
},{
if(!is.null(h2o_df$df.h2o)){
output$h2o_df_loaded <- reactive("1")
outputOptions(output, "h2o_df_loaded", suspendWhenHidden = FALSE)
} else{
output$h2o_df_loaded <- reactive("0")
outputOptions(output, "h2o_df_loaded", suspendWhenHidden = FALSE)
}
})
#------------------------------ Setting the H2o Package -------------------------------------
h2o_rf <- reactiveValues(model = NULL)
observeEvent(input$rf_h2o_run,{
h2o_rf$model <- NULL
if(input$h2o_validation){
h2o_rf$model <- h2o.randomForest(
training_frame = h2o_df$train,
validation_frame = h2o_df$valid,
x = h2o_df$x,
y = h2o_df$y,
ntrees = input$h2o_rf_ntree,
max_depth = input$h2o_rf_max_depth
)
} else if(!input$h2o_validation){
h2o_rf$model <- h2o.randomForest(
training_frame = h2o_df$train,
x = h2o_df$x,
y = h2o_df$y,
ntrees = input$h2o_rf_ntree,
max_depth = input$h2o_rf_max_depth
)
}
sh <- h2o.scoreHistory(h2o_rf$model)
output$error_plot <- renderPlotly({
plot_ly(data = sh, x = ~number_of_trees, y = ~ training_rmse,
type = "scatter", mode = "lines+markers", name = "Training", visible = TRUE) %>%
add_trace(x = ~number_of_trees, y = ~ validation_rmse,
type = "scatter", mode = "lines+markers", name = "Validation", visible = TRUE) %>%
add_trace(x = ~number_of_trees, ~ training_classification_error,
type = "scatter", mode = "lines+markers",
name = "Training", visible = FALSE) %>%
add_trace(x = ~number_of_trees, y = ~ validation_classification_error,
type = "scatter", mode = "lines+markers",
name = "Validation", visible = FALSE) %>%
layout(
title = "Random Forest - Error Score History",
yaxis = list(title = "Error"),
xaxis = list(title = "Number of Trees"),
updatemenus = list(
list(
y = 0.95, x= - 0.1,
buttons = list(
# RMSE
list(method = "restyle",
args = list("visible", list(TRUE, TRUE,FALSE, FALSE)
),
label = "RMSE"),
list(method = "restyle",
args = list("visible", list(FALSE, FALSE, TRUE, TRUE)
),
label = "Classification")
))))
})
})