cjerzak commited on
Commit
b6cd751
·
verified ·
1 Parent(s): 3caf059

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +136 -115
app.R CHANGED
@@ -85,7 +85,7 @@ baseR_generate_randomizations <- function(n_units, n_treated, X, accept_prob, ra
85
  base_assign <- c(rep(1, n_treated), rep(0, n_units - n_treated))
86
 
87
  # shuffle in R
88
- # We'll store T2's in chunks to reduce memory overhead
89
  batch_count <- ceiling(max_draws / batch_size)
90
  all_assign <- list()
91
  all_T2 <- numeric(0)
@@ -230,7 +230,7 @@ ui <- dashboardPage(
230
  "Exact" = "exact"),
231
  selected = "monte_carlo"),
232
  numericInput("accept_prob", "Acceptance Probability (stringency)",
233
- value = 0.05, min = 0.0001, max = 1),
234
  conditionalPanel(
235
  condition = "input.random_type == 'monte_carlo'",
236
  numericInput("max_draws", "Max Draws (MC)", value = 1e5, min = 1e3),
@@ -241,12 +241,19 @@ ui <- dashboardPage(
241
 
242
  box(width = 8, title = "Summary of Accepted Randomizations",
243
  status = "info", solidHeader = TRUE,
 
 
 
 
 
 
 
 
244
  fluidRow(
245
- valueBoxOutput("n_accepted_box", width = 4),
246
- valueBoxOutput("balance_min_box", width = 4),
247
- valueBoxOutput("fastrerand_time_box", width = 4),
248
- valueBoxOutput("baseR_time_box", width = 4)
249
  ),
 
250
  br(),
251
  plotOutput("balance_hist", height = "250px")
252
  )
@@ -287,12 +294,19 @@ ui <- dashboardPage(
287
  ),
288
 
289
  box(width = 8, title = "Test Results", status = "info", solidHeader = TRUE,
 
 
 
 
 
 
 
 
290
  fluidRow(
291
- valueBoxOutput("pvalue_box", width = 4),
292
- valueBoxOutput("tauobs_box", width = 4),
293
- valueBoxOutput("fastrerand_test_time_box", width = 4),
294
- valueBoxOutput("baseR_test_time_box", width = 4)
295
  ),
 
296
  uiOutput("fi_text"),
297
  br(),
298
  plotOutput("test_plot", height = "280px")
@@ -361,53 +375,56 @@ server <- function(input, output, session) {
361
  "Number treated cannot exceed total units.")
362
  )
363
 
364
- # =========== 1) fastrerandomize generation timing ===========
365
- t0_fast <- Sys.time()
366
- out <- tryCatch({
367
- generate_randomizations(
368
- n_units = nrow(X_data()),
369
- n_treated = input$n_treated,
370
- X = X_data(),
371
- randomization_accept_prob= input$accept_prob,
372
- randomization_type = input$random_type,
373
- max_draws = if (input$random_type == "monte_carlo") input$max_draws else NULL,
374
- batch_size = if (input$random_type == "monte_carlo") input$batch_size else NULL,
375
- verbose = FALSE
376
- )
377
- }, error = function(e) e)
378
- t1_fast <- Sys.time()
379
-
380
- if (inherits(out, "error")) {
381
- showNotification(paste("Error generating randomizations (fastrerandomize):", out$message), type = "error")
382
- RerandResult(NULL)
383
- } else {
384
- RerandResult(out)
385
- }
386
- fastrand_time(difftime(t1_fast, t0_fast, units = "secs"))
387
-
388
- # =========== 2) base R generation timing ===========
389
- t0_base <- Sys.time()
390
- out_base <- tryCatch({
391
- baseR_generate_randomizations(
392
- n_units = nrow(X_data()),
393
- n_treated = input$n_treated,
394
- X = X_data(),
395
- accept_prob= input$accept_prob,
396
- random_type= input$random_type,
397
- max_draws = if (input$random_type == "monte_carlo") input$max_draws else NULL,
398
- batch_size = if (input$random_type == "monte_carlo") input$batch_size else NULL
399
- )
400
- }, error = function(e) e)
401
- t1_base <- Sys.time()
402
-
403
- if (inherits(out_base, "error")) {
404
- showNotification(paste("Error generating randomizations (base R):", out_base$message), type = "error")
405
- RerandResult_base(NULL)
406
- } else {
407
- RerandResult_base(out_base)
408
- }
409
- baseR_time(difftime(t1_base, t0_base, units = "secs"))
410
-
 
 
 
411
  })
412
 
413
  # Summaries of accepted randomizations
@@ -521,64 +538,68 @@ server <- function(input, output, session) {
521
  baseR_test_time <- reactiveVal(NULL)
522
 
523
  observeEvent(input$run_randtest_btn, {
524
- req(RerandResult())
525
- rr <- RerandResult()
526
- req(rr$randomizations)
527
- if (is.null(Y_data())) {
528
- showNotification("No outcome data Y found. Upload or simulate first.", type="error")
529
- return(NULL)
530
- }
531
-
532
- obsW <- rr$randomizations[1, ]
533
- obsY <- Y_data()
534
-
535
- # =========== 1) fastrerandomize randomization_test timing ===========
536
- t0_testfast <- Sys.time()
537
- outTest <- tryCatch({
538
- randomization_test(
539
- obsW = obsW,
540
- obsY = obsY,
541
- candidate_randomizations = rr$randomizations,
542
- findFI = input$findFI
543
- )
544
- }, error=function(e) e)
545
- t1_testfast <- Sys.time()
546
-
547
- if (inherits(outTest, "error")) {
548
- showNotification(paste("Error in randomization_test (fastrerandomize):", outTest$message), type="error")
549
- RandTestResult(NULL)
550
- } else {
551
- RandTestResult(outTest)
552
- }
553
- fastrand_test_time(difftime(t1_testfast, t0_testfast, units = "secs"))
554
-
555
- # =========== 2) base R randomization test timing ===========
556
- # We must also have the base R set of randomizations
557
- req(RerandResult_base())
558
- rr_base <- RerandResult_base()
559
- if (is.null(rr_base$randomizations) || nrow(rr_base$randomizations) < 1) {
560
- showNotification("No base R randomizations found. Cannot run base R test.", type = "error")
561
- RandTestResult_base(NULL)
562
- return(NULL)
563
- }
564
-
565
- t0_testbase <- Sys.time()
566
- outTestBase <- tryCatch({
567
- baseR_randomization_test(
568
- obsW = obsW,
569
- obsY = obsY,
570
- allW = rr_base$randomizations
571
- )
572
- }, error = function(e) e)
573
- t1_testbase <- Sys.time()
574
-
575
- if (inherits(outTestBase, "error")) {
576
- showNotification(paste("Error in randomization_test (base R):", outTestBase$message), type="error")
577
- RandTestResult_base(NULL)
578
- } else {
579
- RandTestResult_base(outTestBase)
580
- }
581
- baseR_test_time(difftime(t1_testbase, t0_testbase, units = "secs"))
 
 
 
 
582
  })
583
 
584
  # Display p-value and observed tau (from the fastrerandomize test)
 
85
  base_assign <- c(rep(1, n_treated), rep(0, n_units - n_treated))
86
 
87
  # shuffle in R
88
+ # We'll store T^2's in chunks to reduce memory overhead
89
  batch_count <- ceiling(max_draws / batch_size)
90
  all_assign <- list()
91
  all_T2 <- numeric(0)
 
230
  "Exact" = "exact"),
231
  selected = "monte_carlo"),
232
  numericInput("accept_prob", "Acceptance Probability (stringency)",
233
+ value = 0.01, min = 0.0001, max = 1),
234
  conditionalPanel(
235
  condition = "input.random_type == 'monte_carlo'",
236
  numericInput("max_draws", "Max Draws (MC)", value = 1e5, min = 1e3),
 
241
 
242
  box(width = 8, title = "Summary of Accepted Randomizations",
243
  status = "info", solidHeader = TRUE,
244
+
245
+ # First row of boxes: accepted randomizations and min balance measure
246
+ fluidRow(
247
+ column(width = 6, valueBoxOutput("n_accepted_box", width = 12)),
248
+ column(width = 6, valueBoxOutput("balance_min_box", width = 12))
249
+ ),
250
+
251
+ # Second row of boxes: fastrerandomize time & base R time
252
  fluidRow(
253
+ column(width = 6, valueBoxOutput("fastrerand_time_box", width = 12)),
254
+ column(width = 6, valueBoxOutput("baseR_time_box", width = 12))
 
 
255
  ),
256
+
257
  br(),
258
  plotOutput("balance_hist", height = "250px")
259
  )
 
294
  ),
295
 
296
  box(width = 8, title = "Test Results", status = "info", solidHeader = TRUE,
297
+
298
+ # First row: p-value and observed effect
299
+ fluidRow(
300
+ column(width = 6, valueBoxOutput("pvalue_box", width = 12)),
301
+ column(width = 6, valueBoxOutput("tauobs_box", width = 12))
302
+ ),
303
+
304
+ # Second row: fastrerandomize test time & base R test time
305
  fluidRow(
306
+ column(width = 6, valueBoxOutput("fastrerand_test_time_box", width = 12)),
307
+ column(width = 6, valueBoxOutput("baseR_test_time_box", width = 12))
 
 
308
  ),
309
+
310
  uiOutput("fi_text"),
311
  br(),
312
  plotOutput("test_plot", height = "280px")
 
375
  "Number treated cannot exceed total units.")
376
  )
377
 
378
+ # ------------------ COMPUTING RESULTS TOGGLE ------------------
379
+ withProgress(message = "Computing results...", value = 0, {
380
+
381
+ # =========== 1) fastrerandomize generation timing ===========
382
+ t0_fast <- Sys.time()
383
+ out <- tryCatch({
384
+ generate_randomizations(
385
+ n_units = nrow(X_data()),
386
+ n_treated = input$n_treated,
387
+ X = X_data(),
388
+ randomization_accept_prob= input$accept_prob,
389
+ randomization_type = input$random_type,
390
+ max_draws = if (input$random_type == "monte_carlo") input$max_draws else NULL,
391
+ batch_size = if (input$random_type == "monte_carlo") input$batch_size else NULL,
392
+ verbose = FALSE
393
+ )
394
+ }, error = function(e) e)
395
+ t1_fast <- Sys.time()
396
+
397
+ if (inherits(out, "error")) {
398
+ showNotification(paste("Error generating randomizations (fastrerandomize):", out$message), type = "error")
399
+ RerandResult(NULL)
400
+ } else {
401
+ RerandResult(out)
402
+ }
403
+ fastrand_time(difftime(t1_fast, t0_fast, units = "secs"))
404
+
405
+ # =========== 2) base R generation timing ===========
406
+ t0_base <- Sys.time()
407
+ out_base <- tryCatch({
408
+ baseR_generate_randomizations(
409
+ n_units = nrow(X_data()),
410
+ n_treated = input$n_treated,
411
+ X = X_data(),
412
+ accept_prob= input$accept_prob,
413
+ random_type= input$random_type,
414
+ max_draws = if (input$random_type == "monte_carlo") input$max_draws else NULL,
415
+ batch_size = if (input$random_type == "monte_carlo") input$batch_size else NULL
416
+ )
417
+ }, error = function(e) e)
418
+ t1_base <- Sys.time()
419
+
420
+ if (inherits(out_base, "error")) {
421
+ showNotification(paste("Error generating randomizations (base R):", out_base$message), type = "error")
422
+ RerandResult_base(NULL)
423
+ } else {
424
+ RerandResult_base(out_base)
425
+ }
426
+ baseR_time(difftime(t1_base, t0_base, units = "secs"))
427
+ })
428
  })
429
 
430
  # Summaries of accepted randomizations
 
538
  baseR_test_time <- reactiveVal(NULL)
539
 
540
  observeEvent(input$run_randtest_btn, {
541
+ # ------------------ COMPUTING RESULTS TOGGLE ------------------
542
+ withProgress(message = "Computing results...", value = 0, {
543
+
544
+ req(RerandResult())
545
+ rr <- RerandResult()
546
+ req(rr$randomizations)
547
+ if (is.null(Y_data())) {
548
+ showNotification("No outcome data Y found. Upload or simulate first.", type="error")
549
+ return(NULL)
550
+ }
551
+
552
+ obsW <- rr$randomizations[1, ]
553
+ obsY <- Y_data()
554
+
555
+ # =========== 1) fastrerandomize randomization_test timing ===========
556
+ t0_testfast <- Sys.time()
557
+ outTest <- tryCatch({
558
+ randomization_test(
559
+ obsW = obsW,
560
+ obsY = obsY,
561
+ candidate_randomizations = rr$randomizations,
562
+ findFI = input$findFI
563
+ )
564
+ }, error=function(e) e)
565
+ t1_testfast <- Sys.time()
566
+
567
+ if (inherits(outTest, "error")) {
568
+ showNotification(paste("Error in randomization_test (fastrerandomize):", outTest$message), type="error")
569
+ RandTestResult(NULL)
570
+ } else {
571
+ RandTestResult(outTest)
572
+ }
573
+ fastrand_test_time(difftime(t1_testfast, t0_testfast, units = "secs"))
574
+
575
+ # =========== 2) base R randomization test timing ===========
576
+ # We must also have the base R set of randomizations
577
+ req(RerandResult_base())
578
+ rr_base <- RerandResult_base()
579
+ if (is.null(rr_base$randomizations) || nrow(rr_base$randomizations) < 1) {
580
+ showNotification("No base R randomizations found. Cannot run base R test.", type = "error")
581
+ RandTestResult_base(NULL)
582
+ return(NULL)
583
+ }
584
+
585
+ t0_testbase <- Sys.time()
586
+ outTestBase <- tryCatch({
587
+ baseR_randomization_test(
588
+ obsW = obsW,
589
+ obsY = obsY,
590
+ allW = rr_base$randomizations
591
+ )
592
+ }, error = function(e) e)
593
+ t1_testbase <- Sys.time()
594
+
595
+ if (inherits(outTestBase, "error")) {
596
+ showNotification(paste("Error in randomization_test (base R):", outTestBase$message), type="error")
597
+ RandTestResult_base(NULL)
598
+ } else {
599
+ RandTestResult_base(outTestBase)
600
+ }
601
+ baseR_test_time(difftime(t1_testbase, t0_testbase, units = "secs"))
602
+ })
603
  })
604
 
605
  # Display p-value and observed tau (from the fastrerandomize test)