Spaces:
Running
Running
Update app.R
Browse files
app.R
CHANGED
|
@@ -18,6 +18,7 @@ library(shinydashboard)
|
|
| 18 |
library(DT) # For data tables
|
| 19 |
library(ggplot2) # For basic plotting
|
| 20 |
library(fastrerandomize) # Our rerandomization package
|
|
|
|
| 21 |
|
| 22 |
# For production apps, ensure fastrerandomize is installed:
|
| 23 |
# install.packages("devtools")
|
|
@@ -55,9 +56,22 @@ baseR_hotellingT2 <- function(X, W) {
|
|
| 55 |
baseR_generate_randomizations <- function(n_units, n_treated, X, accept_prob, random_type,
|
| 56 |
max_draws, batch_size) {
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
if (random_type == "exact") {
|
| 59 |
# -------------- EXACT RANDOMIZATIONS --------------
|
| 60 |
-
# All combinations
|
| 61 |
cidx <- combn(n_units, n_treated)
|
| 62 |
# Build assignment matrix
|
| 63 |
n_comb <- ncol(cidx)
|
|
@@ -81,10 +95,8 @@ baseR_generate_randomizations <- function(n_units, n_treated, X, accept_prob, ra
|
|
| 81 |
} else {
|
| 82 |
# -------------- MONTE CARLO RANDOMIZATIONS --------------
|
| 83 |
# We'll sample max_draws permutations
|
| 84 |
-
# Start with a base assignment vector
|
| 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()
|
|
@@ -92,7 +104,6 @@ baseR_generate_randomizations <- function(n_units, n_treated, X, accept_prob, ra
|
|
| 92 |
|
| 93 |
cur_draw <- 0
|
| 94 |
for (b in seq_len(batch_count)) {
|
| 95 |
-
# how many draws in this batch
|
| 96 |
ndraws_here <- min(batch_size, max_draws - cur_draw)
|
| 97 |
cur_draw <- cur_draw + ndraws_here
|
| 98 |
|
|
@@ -255,7 +266,11 @@ ui <- dashboardPage(
|
|
| 255 |
),
|
| 256 |
|
| 257 |
br(),
|
| 258 |
-
plotOutput("balance_hist", height = "250px")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
)
|
| 260 |
)
|
| 261 |
),
|
|
@@ -482,6 +497,17 @@ server <- function(input, output, session) {
|
|
| 482 |
theme_minimal(base_size = 14)
|
| 483 |
})
|
| 484 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 485 |
# -------------------------------------------------------
|
| 486 |
# 3. Randomization Test
|
| 487 |
# -------------------------------------------------------
|
|
@@ -658,8 +684,8 @@ server <- function(input, output, session) {
|
|
| 658 |
})
|
| 659 |
|
| 660 |
# A simple plot for the randomization distribution (for demonstration).
|
| 661 |
-
# In
|
| 662 |
-
# so we simply show the observed effect
|
| 663 |
output$test_plot <- renderPlot({
|
| 664 |
rt <- RandTestResult()
|
| 665 |
if (is.null(rt)) {
|
|
@@ -685,4 +711,3 @@ server <- function(input, output, session) {
|
|
| 685 |
# Run the Application
|
| 686 |
# ---------------------------------------------------------
|
| 687 |
shinyApp(ui = ui, server = server)
|
| 688 |
-
|
|
|
|
| 18 |
library(DT) # For data tables
|
| 19 |
library(ggplot2) # For basic plotting
|
| 20 |
library(fastrerandomize) # Our rerandomization package
|
| 21 |
+
library(parallel) # For detecting CPU cores
|
| 22 |
|
| 23 |
# For production apps, ensure fastrerandomize is installed:
|
| 24 |
# install.packages("devtools")
|
|
|
|
| 56 |
baseR_generate_randomizations <- function(n_units, n_treated, X, accept_prob, random_type,
|
| 57 |
max_draws, batch_size) {
|
| 58 |
|
| 59 |
+
# For safety, check if exact enumerations will explode:
|
| 60 |
+
# If random_type == "exact", we do combn(n_units, n_treated), which might be huge
|
| 61 |
+
if (random_type == "exact") {
|
| 62 |
+
n_comb_total <- choose(n_units, n_treated)
|
| 63 |
+
if (n_comb_total > 1e6) {
|
| 64 |
+
warning(
|
| 65 |
+
sprintf("Exact randomization is requested, but that is %s combinations.
|
| 66 |
+
This may be infeasible in terms of memory/time.
|
| 67 |
+
Consider Monte Carlo instead.", format(n_comb_total, big.mark=",")),
|
| 68 |
+
immediate. = TRUE
|
| 69 |
+
)
|
| 70 |
+
}
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
if (random_type == "exact") {
|
| 74 |
# -------------- EXACT RANDOMIZATIONS --------------
|
|
|
|
| 75 |
cidx <- combn(n_units, n_treated)
|
| 76 |
# Build assignment matrix
|
| 77 |
n_comb <- ncol(cidx)
|
|
|
|
| 95 |
} else {
|
| 96 |
# -------------- MONTE CARLO RANDOMIZATIONS --------------
|
| 97 |
# We'll sample max_draws permutations
|
|
|
|
| 98 |
base_assign <- c(rep(1, n_treated), rep(0, n_units - n_treated))
|
| 99 |
|
|
|
|
| 100 |
# We'll store T^2's in chunks to reduce memory overhead
|
| 101 |
batch_count <- ceiling(max_draws / batch_size)
|
| 102 |
all_assign <- list()
|
|
|
|
| 104 |
|
| 105 |
cur_draw <- 0
|
| 106 |
for (b in seq_len(batch_count)) {
|
|
|
|
| 107 |
ndraws_here <- min(batch_size, max_draws - cur_draw)
|
| 108 |
cur_draw <- cur_draw + ndraws_here
|
| 109 |
|
|
|
|
| 266 |
),
|
| 267 |
|
| 268 |
br(),
|
| 269 |
+
plotOutput("balance_hist", height = "250px"),
|
| 270 |
+
|
| 271 |
+
# Hardware info note
|
| 272 |
+
br(),
|
| 273 |
+
uiOutput("hardware_info")
|
| 274 |
)
|
| 275 |
)
|
| 276 |
),
|
|
|
|
| 497 |
theme_minimal(base_size = 14)
|
| 498 |
})
|
| 499 |
|
| 500 |
+
# Hardware info (CPU cores, GPU note)
|
| 501 |
+
output$hardware_info <- renderUI({
|
| 502 |
+
num_cores <- detectCores(logical = TRUE)
|
| 503 |
+
# Basic note about GPU (this can be expanded if you have specialized checks)
|
| 504 |
+
HTML(paste(
|
| 505 |
+
"<strong>System Hardware Info:</strong><br/>",
|
| 506 |
+
"Number of CPU cores detected:", num_cores, "<br/>",
|
| 507 |
+
"With additional CPU or GPU, greater speedups can be expected."
|
| 508 |
+
))
|
| 509 |
+
})
|
| 510 |
+
|
| 511 |
# -------------------------------------------------------
|
| 512 |
# 3. Randomization Test
|
| 513 |
# -------------------------------------------------------
|
|
|
|
| 684 |
})
|
| 685 |
|
| 686 |
# A simple plot for the randomization distribution (for demonstration).
|
| 687 |
+
# In this minimal example, we do not store the entire distribution in 'randomization_test',
|
| 688 |
+
# so we simply show the observed effect as a point.
|
| 689 |
output$test_plot <- renderPlot({
|
| 690 |
rt <- RandTestResult()
|
| 691 |
if (is.null(rt)) {
|
|
|
|
| 711 |
# Run the Application
|
| 712 |
# ---------------------------------------------------------
|
| 713 |
shinyApp(ui = ui, server = server)
|
|
|