Spaces:
Running
Running
Update app.R
Browse files
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
|
| 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.
|
| 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("
|
| 246 |
-
valueBoxOutput("
|
| 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("
|
| 292 |
-
valueBoxOutput("
|
| 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 |
-
#
|
| 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 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 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 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
randomization_test
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|