# Physician-cluster bootstrap check of the model-based SEs (D28, reduced in D53). # Usage: Rscript simulations/05_bootstrap.R [reps] [B] [cores] # Writes results/bootstrap/__rep.csv: the estimate and the # bootstrap SD for each design, contrast and outcome (V1 point estimates). pkgload::load_all("pvisim", quiet = TRUE) suppressPackageStartupMessages(library(data.table)) a <- commandArgs(TRUE) reps <- if (length(a) >= 1) as.integer(a[1]) else 20L B <- if (length(a) >= 2) as.integer(a[2]) else 50L cores <- if (length(a) >= 3) as.integer(a[3]) else 11L cfg <- yaml::read_yaml("simulations/config/experiments.yml") n <- cfg$scales$draft$n_patients outdir <- "results/bootstrap"; dir.create(outdir, showWarnings = FALSE, recursive = TRUE) designs <- c("naive", "landmark", "landmark_m5", "ccw", "seq") outs <- c(male = "t_male", disease = "t_disease") jobs <- CJ(world = c("W0", "WR"), scenario = c("null", "harm"), rep = seq_len(reps), sorted = FALSE) jobs[, file := file.path(outdir, sprintf("%s_%s_rep%03d.csv", world, scenario, rep))] todo <- jobs[!file.exists(file)] cat(sprintf("bootstrap: %d jobs, %d to do, B = %d, %d cores\n", nrow(jobs), nrow(todo), B, cores)) res <- parallel::mclapply(seq_len(nrow(todo)), function(i) tryCatch({ j <- todo[i] par <- load_params(j$world, j$scenario) rec <- simulate_records(par, rep = j$rep, n = n) # the same data as the main run's replicate bs <- rbindlist(lapply(seq_len(B), function(b) cbind(b = b, run_designs(bootstrap_records(rec, seed = 1e6 * j$rep + b), designs, "V1_ignore", outs))), fill = TRUE) out <- bs[, .(boot_se = sd(est, na.rm = TRUE), boot_mean = mean(est, na.rm = TRUE), B = sum(!is.na(est))), by = .(design, contrast, outcome)] fwrite(out[, `:=`(world = j$world, scenario = j$scenario, rep = j$rep)], j$file) NULL }, error = function(e) sprintf("%s/%s/%d: %s", todo$world[i], todo$scenario[i], todo$rep[i], conditionMessage(e))), mc.cores = cores, mc.preschedule = FALSE) err <- unlist(res[!vapply(res, is.null, TRUE)]) cat(sprintf("done; %d errors\n", length(err))); if (length(err)) cat(head(err, 3), sep = "\n")