-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim_setup.R
115 lines (96 loc) · 2.82 KB
/
sim_setup.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Oversampling: simulation setup ---------------------------------------
library(tidyverse)
library(doParallel)
library(MASS)
library(furrr)
library(tictoc)
options(mc.cores = future::availableCores() - 2)
set.seed(456987)
source(here::here("sim_functions.R"))
# 1) Super populations -------------------------------------------------
# 1A) Define inputs ----------------------------------------------------
n <- 1000000L
mus <- rep(0, 6L)
sds <- rep(1, 6L)
corr_matrix <- matrix(
c(
1, 0.3, 0.4, 0.1, 0.2, 0.5,
0.3, 1, 0.2, 0.1, 0.3, 0.2,
0.4, 0.2, 1, 0.2, 0.2, 0.2,
0.1, 0.1, 0.2, 1, 0.5, 0.4,
0.2, 0.3, 0.2, 0.5, 1, 0.2,
0.5, 0.2, 0.2, 0.4, 0.2, 1
),
nrow = 6L, ncol = 6L, byrow = TRUE
)
beta_list <- list(
"weak" = c(
log(1.25), log(1.5), log(1.25), log(1.5), log(1.25), log(1.5)
),
"strong" = c(
log(1.5), log(1.75), log(1.5), log(1.75), log(1.5), log(1.75)
)
)
intercept_treat <- seq(from = -3, to = 3, by = 0.05)
p_treat <- c(0.3, 0.5, 0.7)
delta_treat <- 0.01
alpha <- c(
log(1.25), log(1.25), log(1.5), log(1.5), log(1.75), log(1.75)
)
alpha_treatment_grid <- seq(from = -1, to = -0.1, by = 0.01)
intercept_outcome_grid <- seq(from = -3, to = 3, by = 0.1)
outcome_grid <- expand_grid(
interc = intercept_outcome_grid, alpha_tr = alpha_treatment_grid
)
delta_outcome <- 0.01
delta_rd <- 0.01
marginal_rd <- 0.15
p_outcome <- 0.2
# 1B) Simulate super-population ----------------------------------------
scenario_grid <- tidyr::expand_grid(
beta = beta_list, p_treat = p_treat
)
plan(multiprocess)
tic()
scenarios <- furrr::future_map2_dfr(
.x = scenario_grid[["beta"]], .y = scenario_grid[["p_treat"]],
~ sim_pop(
n = n, mus = mus, sds = sds, corr_matrix = corr_matrix,
beta = .x, intercept_treat = intercept_treat, p_treat = .y,
delta_treat = delta_treat, alpha = alpha,
outcome_grid = outcome_grid, p_outcome = p_outcome,
delta_outcome = delta_outcome, marginal_rd = marginal_rd,
delta_rd = delta_rd
), .progress = TRUE
) %>%
mutate(
treat_assign = rep(c("weak", "strong"), each = nrow(.)/2),
p_treat = rep(p_treat, 2L)
)
toc()
# 2) Sample from the superpopulation -----------------------------------
n_samples <- 10000L
sample_size <- c(100L, 250L, 500L, 1000L)
scen_data <- tidyr::expand_grid(
scenarios,
ss = sample_size
)
plan(multiprocess)
tic()
sim_samples <- furrr::future_map2(
.x = scen_data[["population"]], .y = scen_data[["ss"]],
~ sample_dfs(
population = .x, sample_size = .y, n_samples = n_samples
), .progress = TRUE
)
toc()
sim_data <- scen_data %>%
dplyr::mutate(samples = sim_samples) %>%
dplyr::select(-population)
# 3) Save the data -----------------------------------------------------
save(
scenarios, sim_data,
file = here::here(
glue::glue("simulation_data_pop_{n}_n_{n_samples}.rda")
)
)