Created
January 1, 2019 04:45
-
-
Save renkun-ken/00c427301406f5bd09ed02d4e66765a0 to your computer and use it in GitHub Desktop.
simulation-io-demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(data.table) | |
dir <- "~/output/sim" | |
dir.create(dir, showWarnings = FALSE, recursive = TRUE) | |
params <- expand.grid( | |
n = c(100, 1000, 10000), | |
mean = c(0.1, 0.2, 0.3), | |
sd = c(0.5, 1, 1.5)) | |
sim <- function(n, mean, sd) { | |
x <- rnorm(n, 0, 1) | |
y <- rnorm(n, mean, sd) | |
t_test <- t.test(x, y) | |
w_test <- wilcox.test(x, y) | |
list(t_test = t_test, w_test = w_test) | |
} | |
res <- lapply(1:nrow(params), function(id) { | |
param <- params[id,] | |
name <- paste0(names(param), "_", param, collapse = "_") | |
outfile <- file.path(dir, sprintf("%s.rds", name)) | |
if (file.exists(outfile)) { | |
return(readRDS(outfile)) | |
} | |
cat(id, ": ", paste0(names(param), ": ", param, collapse = ", "), "\n", sep = "") | |
data <- do.call(sim, param) | |
out <- list(id = id, param = param, data = data) | |
tmpfile <- paste0(outfile, ".tmp") | |
saveRDS(out, tmpfile, compress = TRUE) | |
file.rename(tmpfile, outfile) | |
}) | |
files <- list.files(dir, "\\.rds$", full.names = TRUE) | |
output <- lapply(files, readRDS) | |
test_stats <- rbindlist(lapply(output, function(x) { | |
with(x, cbind(id, param, | |
t_stat = data$t_test$statistic, | |
w_stat = data$w_test$statistic, | |
t_pvalue = data$t_test$p.value, | |
w_pvalue = data$w_test$p.value)) | |
})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment