PtteMDataScience commited on
Commit
f734497
·
verified ·
1 Parent(s): 8f391dc

Upload app.R

Browse files
Files changed (1) hide show
  1. app.R +763 -0
app.R ADDED
@@ -0,0 +1,763 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ library(shiny)
2
+ library(shinyjs)
3
+ library(bslib)
4
+ library(dplyr)
5
+ library(ggplot2)
6
+ library(tm)
7
+ library(SnowballC)
8
+ library(plotly)
9
+ library(text2vec)
10
+ library(tokenizers)
11
+ library(dplyr)
12
+ library(tidyr)
13
+ library(igraph)
14
+ library(ggraph)
15
+ library(topicmodels)
16
+ library(wordcloud)
17
+ library(wordcloud2)
18
+ library(reshape2)
19
+ library(SnowballC)
20
+ library(RColorBrewer)
21
+ library(syuzhet)
22
+ library(cluster)
23
+ library(tidytext)
24
+ library(word2vec)
25
+ library(Rtsne)
26
+ library(umap)
27
+ library(MASS)
28
+ library(koRpus)
29
+ library(openxlsx)
30
+ library(tools)
31
+ library(shinyWidgets)
32
+ library(reticulate)
33
+ library(keras)
34
+ library(tensorflow)
35
+ library(neuralnet)
36
+ library(rsample)
37
+ library(tfdatasets)
38
+ library(statnet)
39
+ library(UserNetR)
40
+ library(visNetwork)
41
+ library(networkD3)
42
+ library(ergm)
43
+ library(ergm.count)
44
+ library(network)
45
+ library(tidyverse)
46
+ options(width = 150)
47
+ options(digits = 4, scipen = 1000000000)
48
+ options(shiny.maxRequestSize=30*1024^2)
49
+
50
+
51
+
52
+ ui <- fluidPage(
53
+ theme = bs_theme(version = 5, bootswatch = "spacelab"),
54
+ useShinyjs(), # Initialize shinyjs
55
+ titlePanel("PtteM Data Science"),
56
+ tags$head(tags$link(rel = "stylesheet", href="https://fonts.googleapis.com/css?family=Montserrat:100,300,400,700&display=swap"),
57
+ tags$style(HTML("
58
+ body, h1, h2, h3, h4, h5, h6, .nav, p, a, .shiny-input-container {
59
+ font-family: 'Montserrat'; /* Font type for the title attribute */
60
+ font-weight: 385;
61
+ color: #007c9e !important;
62
+ }
63
+ * {
64
+ font-family: 'Montserrat', sans-serif;
65
+ font-weight: 385;
66
+ color: #195576; /* Blue color */
67
+ }
68
+ body {
69
+ background-color: #f7f7f7; /* Light gray background */
70
+ }
71
+ .icon-btn {
72
+ border: 1px solid #0d6efd; /* Example border: solid, 2 pixels, #555 color */
73
+ border-radius: 15%; /* Circular border */
74
+ color: #00969e; /* Icon color */
75
+ font-family: 'Montserrat'; /* Font type for the title attribute */
76
+ font-weight: 385;
77
+ background-color: #f7f7f7;
78
+ padding: 125px; /* Space around the icon */
79
+ margin: 25px; /* Space around the button */
80
+ font-size: 24px; /* Icon size */
81
+ box-shadow: 0 2px 4px rgba(0,0,0,0.2);
82
+ }
83
+ .icon-btn:hover {
84
+ color: #00969e; /* Icon color on hover */
85
+ border-color: #007c9e;
86
+ background-color: #ebfbfd;/* Border color on hover */
87
+ }
88
+ /* Add custom styles here */
89
+ .shiny-input-container {
90
+ margin-bottom: 15px;
91
+ }
92
+ .box {
93
+ border: 1px solid #ddd;
94
+ padding: 20px;
95
+ border-radius: 50px;
96
+ margin-bottom: 200px;
97
+ gap: 200px;
98
+ align-items: center;
99
+ }
100
+ #statsTable_wrapper {
101
+ margin: 0 auto;
102
+ }
103
+ .shiny-output-error {
104
+ border: 1px solid #FF0000; /* Red border on error */
105
+ }
106
+ /* If you want to change the font size of the tooltip, you can add custom CSS for the 'title' attribute's default styling. */
107
+ "))),
108
+ tags$head(
109
+ # Include JavaScript to reload the page
110
+ tags$script(HTML("
111
+ document.addEventListener('DOMContentLoaded', function() {
112
+ document.getElementById('myElement').style.color = '#0d6efd'; // Change to your desired color
113
+ });
114
+ "))
115
+ ),
116
+ tags$head(
117
+ tags$script(HTML("
118
+ function reloadPage() {
119
+ window.location.reload();
120
+ }
121
+ "))
122
+ ),
123
+ # Refresh button that calls the JavaScript function
124
+ actionButton("refresh", "Refresh Analysis", onclick = "reloadPage();"),
125
+ # Help Text or Information for the user
126
+ helpText("Bu uygulama ile pekiştirmeli öğrenme başlığı altındaki veri bilimi fonksiyonlarına erişebilirsiniz."),
127
+ #Reinforcement Learning
128
+ h2("Reinforcement Learning Section"),
129
+ tabsetPanel(
130
+ tabPanel("Multi-Armed Bandit (MAB) Algorithms",
131
+ tabsetPanel(
132
+ tabPanel("Epsilon-Greedy",
133
+ sidebarLayout(
134
+ sidebarPanel(
135
+ fileInput('epsgreefile', 'Upload Data File', accept = c(".csv", ".xlsx", ".xls")),
136
+ uiOutput('selectActionColumn'),
137
+ uiOutput('selectRewardColumn'),
138
+ numericInput("epsilon", "Epsilon (Exploration Rate)", value = 0.1, min = 0, max = 1, step = 0.01),
139
+ actionButton("runMAB", "Run MAB Decision"),
140
+ ),
141
+ mainPanel(
142
+ tableOutput("actionResults"),
143
+ textOutput("selectedAction"),
144
+ )
145
+ )
146
+ ),
147
+ tabPanel("Upper Confidence Bound (UCB)",
148
+ sidebarLayout(
149
+ sidebarPanel(
150
+ fileInput("ucbfile", "Upload Data File", accept = c(".csv", ".xlsx", ".xls")),
151
+ uiOutput("selectStrategyColucb"),
152
+ uiOutput("selectRewardColucb"),
153
+ numericInput("epsilon", "Epsilon (Exploration Rate)", value = 0.1, min = 0, max = 1, step = 0.01),
154
+ actionButton("runMABucb", "Run MAB Decision")
155
+ ),
156
+ mainPanel(
157
+ tabsetPanel(
158
+ tabPanel("Results", tableOutput("actionResultsucb")),
159
+ tabPanel("Reward History", plotlyOutput("rewardHistoryucb", width = "100%", height = "750px"))
160
+ )
161
+ )
162
+ )
163
+ ),
164
+ tabPanel("Thompson Sampling",
165
+ sidebarLayout(
166
+ sidebarPanel(
167
+ fileInput("tsfile", "Upload Data File", accept = c(".csv", ".xlsx", ".xls")),
168
+ uiOutput("selectStrategyColumnTS"), # This should be in the UI part
169
+ uiOutput("selectRewardColumnTS"),
170
+ textInput("successConditionsTS", "Enter success conditions (comma separated)"),
171
+ actionButton("runMABTS", "Run MAB Thompson Sampling")
172
+ ),
173
+ mainPanel(
174
+ tabsetPanel(
175
+ tabPanel("Best Display", verbatimTextOutput("bestStrategyDisplayTS")),
176
+ tabPanel("Reward History TS", plotlyOutput("rewardHistoryTS", width = "100%", height = "750px")),
177
+ tabPanel("Strategy Frequency", plotlyOutput("strFrePlotTS", width = "100%", height = "750px"))
178
+ )
179
+ )
180
+ )
181
+ ),
182
+
183
+
184
+ )
185
+ ),
186
+ tabPanel("Contextual Bandits",
187
+ tabsetPanel(
188
+ tabPanel("Linear Regression UCB",
189
+ sidebarLayout(
190
+ sidebarPanel(
191
+ fileInput("lrucbfile", "Upload your dataset", accept = c(".csv", ".xlsx", ".xls")),
192
+ actionButton("loadlrucb", "Load Data"),
193
+ selectInput("targetlrucb", "Select Target Column", choices = NULL),
194
+ selectizeInput("independentVarlrucb", "Select Independent Variable", choices = NULL, multiple = FALSE),
195
+ actionButton("runModellrucb", "Run Linear Regression UCB")
196
+ ),
197
+ mainPanel(
198
+ tabsetPanel(
199
+ tabPanel("Model Summary", verbatimTextOutput("modelSummarylrucb")),
200
+ tabPanel("Diagnostics",
201
+ plotlyOutput("resFitlrucbPlot"),
202
+ plotlyOutput("qqPlotlrucb"),
203
+ plotlyOutput("scaleLoclrucbPlot"),
204
+ plotlyOutput("resLevlrucbPlot")
205
+ ),
206
+ tabPanel("Regression Plot", plotlyOutput("regressionPlot", width = "100%", height = "750px"))
207
+ )
208
+ )
209
+ )
210
+ )
211
+
212
+
213
+ )
214
+ )
215
+ )
216
+
217
+
218
+
219
+ )
220
+
221
+ server <- function(input, output, session) {
222
+ ##Reinforcement Learning
223
+ ###Multi-Armed Bandit (MAB) Algorithms
224
+ ####Epsilon-Greedy
225
+ # Reactive to store uploaded data
226
+ uploadedepsgree <- reactive({
227
+ req(input$epsgreefile)
228
+ ext <- tools::file_ext(input$epsgreefile$name)
229
+ switch(ext,
230
+ csv = read.csv(input$epsgreefile$datapath, stringsAsFactors = FALSE),
231
+ xlsx = readxl::read_excel(input$epsgreefile$datapath),
232
+ stop("Unsupported file type"))
233
+ })
234
+
235
+ # Dynamic UI to select the action and reward columns
236
+ output$selectActionColumn <- renderUI({
237
+ df <- uploadedepsgree()
238
+ req(df)
239
+ selectInput("actionColumn", "Select Action Column", choices = names(df))
240
+ })
241
+
242
+ output$selectRewardColumn <- renderUI({
243
+ df <- uploadedepsgree()
244
+ req(df)
245
+ selectInput("rewardColumn", "Select Reward Column", choices = names(df))
246
+ })
247
+
248
+ # Reactive values to store actions and performance metrics
249
+ actionRewards <- reactiveValues(actions = NULL, rewards = NULL, counts = NULL)
250
+
251
+ observeEvent(input$runMAB, {
252
+ req(input$epsilon, input$actionColumn, input$rewardColumn)
253
+ df <- uploadedepsgree()
254
+ df <- df %>% dplyr::select(all_of(input$actionColumn), all_of(input$rewardColumn)) %>% na.omit()
255
+ uniqueActions <- unique(df[[input$actionColumn]])
256
+ summedRewards <- tapply(df[[input$rewardColumn]], df[[input$actionColumn]], sum, na.rm = TRUE)
257
+
258
+ # Store processed data in actionRewards
259
+ actionRewards$actions <- uniqueActions
260
+ actionRewards$rewards <- summedRewards
261
+ actionRewards$counts <- rep(10, length(uniqueActions)) # Initialize with some counts
262
+
263
+ # Run epsilon-greedy algorithm
264
+ if (runif(1) < input$epsilon) {
265
+ # Exploration
266
+ selectedActionIndex <- sample(length(uniqueActions), 1)
267
+ } else {
268
+ # Exploitation
269
+ averages <- summedRewards / actionRewards$counts
270
+ selectedActionIndex <- which.max(averages)
271
+ }
272
+
273
+ selectedAction <- uniqueActions[selectedActionIndex]
274
+ simulatedReward <- runif(1, min = 0, max = max(summedRewards)) # Simulate a reward
275
+ actionRewards$rewards[selectedActionIndex] <- actionRewards$rewards[selectedActionIndex] + simulatedReward
276
+ actionRewards$counts[selectedActionIndex] <- actionRewards$counts[selectedActionIndex] + 1
277
+
278
+ # Update UI
279
+ output$selectedAction <- renderText({ selectedAction })
280
+ output$actionResults <- renderTable({
281
+ data.frame(Action = actionRewards$actions, Reward = actionRewards$rewards, Count = actionRewards$counts)
282
+ })
283
+ })
284
+
285
+ ####Upper Confidence Bound (UCB)
286
+ # Reactive values to store actions and performance metrics
287
+ actionData <- reactiveValues(
288
+ actions = NULL,
289
+ counts = NULL,
290
+ rewards = NULL,
291
+ history = list() # Initialize the history list here
292
+ )
293
+
294
+ uploadeducb <- reactive({
295
+ req(input$ucbfile)
296
+ ext <- tools::file_ext(input$ucbfile$name)
297
+ switch(ext,
298
+ csv = read.csv(input$ucbfile$datapath, stringsAsFactors = FALSE),
299
+ xlsx = readxl::read_excel(input$ucbfile$datapath),
300
+ xls = readxl::read_excel(input$ucbfile$datapath),
301
+ stop("Unsupported file type")
302
+ )
303
+ })
304
+
305
+ output$selectStrategyColucb <- renderUI({
306
+ req(input$ucbfile)
307
+ data <- uploadeducb()
308
+ selectInput("strategyColumn", "Select Strategy Column",
309
+ choices = names(data), selected = names(data)[1])
310
+ })
311
+
312
+ output$selectRewardColucb <- renderUI({
313
+ req(input$ucbfile)
314
+ data <- uploadeducb()
315
+ selectInput("rewardColumn", "Select Reward Column",
316
+ choices = names(data), selected = names(data)[2])
317
+ })
318
+
319
+
320
+
321
+ observeEvent(input$runMABucb, {
322
+
323
+ data <- uploadeducb()
324
+ strategyColumn <- input$strategyColumn
325
+ rewardColumn <- input$rewardColumn
326
+
327
+ # Ensure numerical data for computation
328
+ data[[rewardColumn]] <- as.numeric(as.character(data[[rewardColumn]]))
329
+
330
+ # Unique strategies
331
+ strategies <- unique(data[[strategyColumn]])
332
+ n <- nrow(data) # Total number of events
333
+
334
+ if (is.null(actionData$actions)) {
335
+ actionData$actions <- strategies
336
+ actionData$counts <- rep(0, length(strategies))
337
+ actionData$rewards <- rep(0, length(strategies))
338
+ # Initialize history for each strategy
339
+ actionData$history <- setNames(vector("list", length(strategies)), strategies)
340
+ for(strategy in strategies) {
341
+ actionData$history[[strategy]] <- data.frame(Time = integer(), Reward = numeric())
342
+ }
343
+ }
344
+
345
+ # Loop over strategies to compute UCB and update history
346
+ for (strategy in strategies) {
347
+ strategy_data <- data[data[[strategyColumn]] == strategy, ]
348
+ ni <- nrow(strategy_data) # Number of times this strategy was tried
349
+ avg_reward <- mean(strategy_data[[rewardColumn]], na.rm = TRUE)
350
+
351
+ if (ni > 0) {
352
+ ucb_value <- avg_reward + sqrt((2 * log(n)) / ni)
353
+ } else {
354
+ ucb_value <- Inf # Encourage exploration of untried strategies
355
+ }
356
+ # Define strategy_index here, inside the loop, before you use it
357
+ strategy_index <- which(actionData$actions == strategy)
358
+
359
+ actionData$counts[strategy_index] <- actionData$counts[strategy_index] + ni
360
+ actionData$rewards[strategy_index] <- ucb_value
361
+ actionData$history[[strategy]] <- rbind(
362
+ actionData$history[[strategy]],
363
+ data.frame(Time = as.integer(actionData$counts[strategy_index]), Reward = ucb_value)
364
+ )
365
+ }
366
+ # After the loop, the history should be updated
367
+ })
368
+
369
+ output$actionResultsucb <- renderTable({
370
+ req(actionData$actions) # Ensure the action data is not NULL
371
+ data.frame(
372
+ Strategy = actionData$actions,
373
+ Counts = actionData$counts,
374
+ Rewards = actionData$rewards
375
+ )
376
+ })
377
+
378
+
379
+
380
+ # Create the plot in a separate reactive context, responding to changes in actionData
381
+ output$rewardHistoryucb <- renderPlotly({
382
+ req(actionData$history) # Ensure that the history is not NULL or empty
383
+
384
+ # Create plot_data for plotting
385
+ plot_data <- do.call(rbind, Filter(Negate(is.null), lapply(names(actionData$history), function(strategy) {
386
+ history_data <- actionData$history[[strategy]]
387
+ if(nrow(history_data) > 0) {
388
+ return(data.frame(Strategy = strategy, Time = history_data$Time, Reward = history_data$Reward))
389
+ }
390
+ })))
391
+
392
+ # Only attempt to create a plot if plot_data is a data frame and not NULL
393
+ if(!is.null(plot_data) && is.data.frame(plot_data)) {
394
+ # Create a custom color palette with as many colors as there are strategies
395
+ colors <- grDevices::colorRampPalette(RColorBrewer::brewer.pal(8, "Set2"))(length(unique(plot_data$Strategy)))
396
+
397
+ plot_ly(plot_data, x = ~Time, y = ~Reward, color = ~Strategy, colors = colors, type = 'scatter', mode = 'lines+markers') %>%
398
+ layout(title = "Reward History Over Time",
399
+ xaxis = list(title = "Time"),
400
+ yaxis = list(title = "Reward"))
401
+ } else {
402
+ plot_ly() %>%
403
+ add_annotations(text = "No data available for plot", x = 0.5, y = 0.5, showarrow = FALSE, xref = "paper", yref = "paper")
404
+ }
405
+ })
406
+
407
+ ####Thompson Sampling
408
+ uploadedTS <- reactive({
409
+ req(input$tsfile)
410
+ ext <- tools::file_ext(input$tsfile$name)
411
+ switch(ext,
412
+ csv = read.csv(input$tsfile$datapath, stringsAsFactors = FALSE),
413
+ xlsx = readxl::read_excel(input$tsfile$datapath),
414
+ xls = readxl::read_excel(input$tsfile$datapath),
415
+ stop("Unsupported file type: ", ext)
416
+ )
417
+ })
418
+
419
+ # Initialize actionDataTS similar to actionData for UCB
420
+ actionDataTS <- reactiveValues(
421
+ actions = NULL,
422
+ successes = NULL,
423
+ failures = NULL,
424
+ theta = NULL
425
+ )
426
+
427
+
428
+ output$selectStrategyColumnTS <- renderUI({
429
+ req(input$tsfile)
430
+ data <- uploadedTS()
431
+ selectInput("selectStrategyColumnTS", "Select Strategy Column",
432
+ choices = names(data), selected = names(data)[1])
433
+ })
434
+
435
+ output$selectRewardColumnTS <- renderUI({
436
+ req(input$tsfile)
437
+ data <- uploadedTS()
438
+ selectInput("selectRewardColumnTS", "Select Reward Column",
439
+ choices = names(data), selected = names(data)[2])
440
+ })
441
+
442
+
443
+ observeEvent(input$runMABTS, {
444
+ req(input$tsfile)
445
+ data <- uploadedTS()
446
+
447
+ strategyColumn <- input$selectStrategyColumnTS
448
+ rewardColumn <- input$selectRewardColumnTS
449
+ success_conditions <- unlist(strsplit(input$successConditionsTS, ",")) # split the input string into a vector
450
+
451
+ if (is.null(actionDataTS$actions)) {
452
+ actionDataTS$actions <- unique(data[[strategyColumn]])
453
+ actionDataTS$successes <- rep(1, length(actionDataTS$actions)) # Start with 1 success for beta distribution
454
+ actionDataTS$failures <- rep(1, length(actionDataTS$actions)) # Start with 1 failure for beta distribution
455
+ actionDataTS$theta <- rep(0, length(actionDataTS$actions))
456
+ actionDataTS$history <- list() # Initialize history for plotting
457
+ actionDataTS$selectionCount <- rep(0, length(actionDataTS$actions)) # Initialize with zeros
458
+ }
459
+
460
+ for (strategy in actionDataTS$actions) {
461
+ strategy_index <- which(actionDataTS$actions == strategy)
462
+
463
+ # Define strategy_data and num_successes/num_failures after defining strategy_index
464
+ strategy_data <- data[data[[strategyColumn]] == strategy, ]
465
+ strategy_data[[rewardColumn]] <- as.numeric(strategy_data[[rewardColumn]] %in% success_conditions) # Convert to binary
466
+
467
+ num_successes <- sum(strategy_data[[rewardColumn]], na.rm = TRUE)
468
+ num_failures <- nrow(strategy_data) - num_successes
469
+
470
+ actionDataTS$successes[strategy_index] <- actionDataTS$successes[strategy_index] + num_successes
471
+ actionDataTS$failures[strategy_index] <- actionDataTS$failures[strategy_index] + num_failures
472
+ actionDataTS$theta[strategy_index] <- rbeta(1, actionDataTS$successes[strategy_index], actionDataTS$failures[strategy_index])
473
+ actionDataTS$selectionCount[strategy_index] <- actionDataTS$selectionCount[strategy_index] + 1
474
+
475
+ # Update the history with the new data for plotting
476
+ if (is.null(actionDataTS$history[[strategy]])) {
477
+ actionDataTS$history[[strategy]] <- data.frame(Time = seq_along(strategy_data[[rewardColumn]]), Reward = strategy_data[[rewardColumn]])
478
+ } else {
479
+ actionDataTS$history[[strategy]] <- rbind(actionDataTS$history[[strategy]], data.frame(Time = seq_along(strategy_data[[rewardColumn]]) + nrow(actionDataTS$history[[strategy]]), Reward = strategy_data[[rewardColumn]]))
480
+ }
481
+ }
482
+ })
483
+
484
+ output$bestStrategyDisplayTS <- renderPrint({
485
+ req(actionDataTS$theta) # Ensure theta values have been calculated
486
+ best_strategy_index <- which.max(actionDataTS$theta)
487
+ best_strategy <- actionDataTS$actions[best_strategy_index] # Ensure this variable matches throughout your code
488
+ theta_value <- actionDataTS$theta[best_strategy_index]
489
+
490
+ cat("The best strategy is:", best_strategy, "\n",
491
+ "Estimated success probability (theta):", theta_value)
492
+ })
493
+
494
+
495
+ createPlotData <- function(history) {
496
+ if (is.list(history) && length(history) > 0 && all(sapply(history, is.data.frame))) {
497
+ # Combine all strategy data frames into one, adding a 'Strategy' column
498
+ plot_data <- do.call(rbind, lapply(names(history), function(strategy) {
499
+ strategy_data <- history[[strategy]]
500
+ if ("Time" %in% names(strategy_data) && "Reward" %in% names(strategy_data)) {
501
+ strategy_data$Strategy <- strategy # Add the 'Strategy' column
502
+ return(strategy_data)
503
+ } else {
504
+ # Return a data frame with missing values if required columns are not present
505
+ return(data.frame(Time = NA, Reward = NA, Strategy = strategy))
506
+ }
507
+ }))
508
+ # Remove rows with missing values
509
+ plot_data <- na.omit(plot_data)
510
+ return(plot_data)
511
+ } else {
512
+ # If history is not as expected, return an empty data frame with the correct columns
513
+ return(data.frame(Time = integer(0), Reward = numeric(0), Strategy = factor()))
514
+ }
515
+ }
516
+
517
+
518
+ output$rewardHistoryTS <- renderPlotly({
519
+ req(actionDataTS$history) # Ensure that the history is not NULL or empty
520
+
521
+ # Create the required plot_data using the createPlotData function
522
+ plot_data <- createPlotData(actionDataTS$history)
523
+
524
+ # Check if the plot_data has rows to plot
525
+ if (!is.null(plot_data) && nrow(plot_data) > 0) {
526
+ # Create the plot with plotly
527
+ plot_ly(
528
+ data = plot_data,
529
+ x = ~Time,
530
+ y = ~Reward,
531
+ color = ~Strategy,
532
+ type = 'scatter', # Use 'scatter' for line and point plots
533
+ mode = 'markers', # Combine line and marker styles
534
+ marker = list(color = 'rgba(74, 93, 191, 0.7))', line = list(color = 'rgba(55, 128, 191, 1.0)', width = 2)) # Optionally, adjust marker size
535
+ ) %>%
536
+ layout(
537
+ title = "Reward History Over Time",
538
+ xaxis = list(title = "Time"),
539
+ yaxis = list(title = "Reward"),
540
+ hovermode = 'closest' # Improves tooltip behavior
541
+ )
542
+ } else {
543
+ # Provide a message or an empty plot if plot_data is empty
544
+ return(plotly_empty())
545
+ }
546
+ })
547
+ output$strFrePlotTS <- renderPlotly({
548
+ req(input$tsfile) # Make sure a file is uploaded
549
+ data <- uploadedTS() # Get the uploaded data
550
+
551
+ # Calculate the frequency of each strategy
552
+ strategy_freq <- table(data[[input$selectStrategyColumnTS]])
553
+
554
+ # Convert to a data frame for plotting
555
+ strategy_freq_df <- as.data.frame(strategy_freq)
556
+
557
+ # Create the plot with plotly
558
+ plot_ly(
559
+ data = strategy_freq_df,
560
+ x = ~Var1, # The strategy names
561
+ y = ~Freq, # The frequency counts
562
+ type = 'scatter', # Use a scatter chart
563
+ mode = 'markers',
564
+ marker = list(color = 'rgba(74, 93, 191, 0.7)', line = list(color = 'rgba(55, 128, 191, 1.0)', width = 2))
565
+ ) %>%
566
+ layout(
567
+ title = "Strategy Frequency",
568
+ xaxis = list(title = "Strategy"),
569
+ yaxis = list(title = "Frequency"),
570
+ hovermode = 'closest'
571
+ )
572
+ })
573
+
574
+ ####Linear Regression UCB
575
+ # Reactive value to store preprocessed data
576
+ datalrucb <- reactiveVal(NULL)
577
+ modelsList <- reactiveVal(list())
578
+ # Function to read and clean data
579
+ read_data <- function(filepath) {
580
+ ext <- tools::file_ext(filepath)
581
+ if (ext == "csv") {
582
+ read.csv(filepath, stringsAsFactors = FALSE)
583
+ } else if (ext == "xlsx") {
584
+ readxl::read_excel(filepath)
585
+ } else {
586
+ stop("Invalid file format. Please select a CSV or XLSX file.")
587
+ }
588
+ }
589
+
590
+ clean_column_names <- function(dataframe) {
591
+ colnames(dataframe) <- gsub("[^[:alnum:]_]", "", make.names(colnames(dataframe), unique = TRUE))
592
+ return(dataframe)
593
+ }
594
+
595
+ # Observe load data button click
596
+ observeEvent(input$loadlrucb, {
597
+ file <- input$lrucbfile
598
+ req(file)
599
+ data_lrucb <- read_data(file$datapath)
600
+ data_lrucb <- clean_column_names(data_lrucb)
601
+ datalrucb(data_lrucb) # Store the data
602
+ updateSelectInput(session, "targetlrucb", choices = colnames(data_lrucb))
603
+ updateSelectizeInput(session, "independentVarlrucb", choices = setdiff(colnames(data_lrucb), input$targetlrucb))
604
+ })
605
+
606
+ # Function to perform safe regression
607
+ safe_regression <- function(data, responseVar, var) {
608
+ tryCatch({
609
+ # Print types to debug
610
+ #print(paste("Type of response variable:", class(data[[responseVar]])))
611
+ #print(paste("Type of independent variable:", class(data[[var]])))
612
+
613
+ #if(!is.numeric(data[[var]])) {
614
+ #stop(paste(var, "is not numeric and will be skipped."))
615
+ #}
616
+ #if(!is.numeric(data[[responseVar]])) {
617
+ #stop(paste(responseVar, "is not numeric and will be skipped."))
618
+ #}
619
+
620
+ # Check for missing or infinite values
621
+ #if(any(!is.finite(data[[var]])) || any(!is.finite(data[[responseVar]]))) {
622
+ #stop("Non-finite values detected in variables.")
623
+ #}
624
+ # Assuming 'data' is your data frame and 'responseVar' and 'var' are column names
625
+ data <- na.omit(data[c(responseVar, var)])
626
+ formula <- as.formula(paste(responseVar, "~", var))
627
+ model <- lm(formula, data = data)
628
+ summary_model <- summary(model)
629
+ ucb_estimate <- summary_model$coefficients[var, "Estimate"]
630
+ # Store the entire model object along with variable names
631
+ return(list(variable = var, model = model, ucb_estimate = ucb_estimate))
632
+ }, error = function(e) {
633
+ message("Error in regression with variable: ", var, "; Error: ", e$message)
634
+ return(NULL) # Skip this variable
635
+ })
636
+ }
637
+
638
+
639
+ # Perform regression on Run Model button click
640
+ observeEvent(input$runModellrucb, {
641
+ data <- req(datalrucb())
642
+ responseVar <- req(input$targetlrucb)
643
+ var <- req(input$independentVarlrucb)
644
+
645
+ if(!responseVar %in% names(data)) {
646
+ stop("Selected response variable is not in the dataset.")
647
+ }
648
+
649
+ results <- safe_regression(data, responseVar, var)
650
+ modelsList(list(results)) # Append new result to the list
651
+ output$modelSummarylrucb <- renderPrint({
652
+ req(results)
653
+ if (is.null(results$ucb_estimate)) {
654
+ paste("Regression could not be performed for variable:", var)
655
+ } else {
656
+ paste("UCB estimate for variable", var, "is", results$ucb_estimate)
657
+ }
658
+ })
659
+ })
660
+
661
+ # Server function to create diagnostic plots
662
+ output$resFitlrucbPlot <- renderPlotly({
663
+ req(modelsList(), datalrucb())
664
+ models <- req(modelsList())
665
+
666
+ # Use the first model for the plot as an example
667
+ if (length(models) >= 1 && !is.null(models[[1]]$model)) {
668
+ fitted_model <- models[[1]]$model
669
+ p <- ggplot(fitted_model, aes(.fitted, .resid)) +
670
+ geom_point(color = "darkorange") +
671
+ geom_smooth(method = "lm", se = FALSE, color = "dodgerblue") +
672
+ labs(title = "Residuals vs Fitted", x = "Fitted Values", y = "Residuals")
673
+
674
+ ggplotly(p)
675
+ } else {
676
+ return(NULL) # No models to plot
677
+ }
678
+ })
679
+
680
+
681
+ output$qqPlotlrucb <- renderPlotly({
682
+ req(modelsList(), datalrucb())
683
+ models <- req(modelsList())
684
+
685
+ # Use the first model for the plot as an example
686
+ if (length(models) >= 1 && !is.null(models[[1]]$model)) {
687
+ fitted_model <- models[[1]]$model
688
+ p <- ggplot(fitted_model, aes(sample = .stdresid)) +
689
+ stat_qq(color = "darkorange") +
690
+ stat_qq_line(color = "dodgerblue") +
691
+ labs(title = "Normal Q-Q")
692
+
693
+ ggplotly(p)
694
+ } else {
695
+ return(NULL) # No models to plot
696
+ }
697
+ })
698
+
699
+ output$scaleLoclrucbPlot <- renderPlotly({
700
+ req(modelsList(), datalrucb())
701
+ models <- req(modelsList())
702
+
703
+ # Use the first model for the plot as an example
704
+ if (length(models) >= 1 && !is.null(models[[1]]$model)) {
705
+ fitted_model <- models[[1]]$model
706
+ p <- ggplot(fitted_model, aes(.fitted, sqrt(abs(.resid)))) +
707
+ geom_point(color = "darkorange") +
708
+ geom_smooth(method = "lm", se = FALSE, color = "dodgerblue") +
709
+ labs(title = "Scale-Location", x = "Fitted Values", y = "Sqrt(|Residuals|)")
710
+
711
+ ggplotly(p)
712
+ } else {
713
+ return(NULL) # No models to plot
714
+ }
715
+ })
716
+
717
+ output$resLevlrucbPlot <- renderPlotly({
718
+ req(modelsList(), datalrucb())
719
+ models <- req(modelsList())
720
+
721
+ # Use the first model for the plot as an example
722
+ if (length(models) >= 1 && !is.null(models[[1]]$model)) {
723
+ fitted_model <- models[[1]]$model
724
+ p <- ggplot(fitted_model, aes(.hat, .stdresid)) +
725
+ geom_point(aes(size = .cooksd), shape = 1, color = "darkorange") +
726
+ geom_smooth(method = "lm", se = FALSE, color = "dodgerblue") +
727
+ labs(title = "Residuals vs Leverage", x = "Leverage", y = "Standardized Residuals")
728
+
729
+ ggplotly(p)
730
+ } else {
731
+ return(NULL) # No models to plot
732
+ }
733
+ })
734
+
735
+ output$regressionPlot <- renderPlotly({
736
+ req(modelsList(), datalrucb())
737
+ fitted_model <- modelsList()
738
+ data_for_plot <- datalrucb()
739
+
740
+ # Ensure the target and independent variables are provided
741
+ target_col <- input$targetlrucb
742
+ independent_var <- input$independentVarlrucb
743
+ if (is.null(data_for_plot[[target_col]]) || is.null(data_for_plot[[independent_var]])) {
744
+ return("Target or independent variable not found in the data.")
745
+ }
746
+
747
+ # Creating the plot with added color
748
+ p <- ggplot(data_for_plot, aes_string(x = independent_var, y = target_col)) +
749
+ geom_point(color = "darkorange") + # Change color of points
750
+ geom_smooth(method = "lm", se = FALSE, color = "dodgerblue") + # Change color of regression line
751
+ ggtitle("Regression Line Plot") +
752
+ xlab(independent_var) +
753
+ ylab(target_col) +
754
+ theme_minimal() + # Adding a minimal theme for a cleaner look
755
+ theme(legend.position = "none") # Remove legend if not needed
756
+
757
+ # Convert ggplot object to Plotly for an interactive plot
758
+ ggplotly(p)
759
+ })
760
+
761
+ }
762
+
763
+ shinyApp(ui, server)