-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_run_sims.R
More file actions
68 lines (56 loc) · 3.05 KB
/
2_run_sims.R
File metadata and controls
68 lines (56 loc) · 3.05 KB
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
library(tidyverse)
library(ltmle)
library(doParallel)
library(foreach)
library(sandwich)
library(lme4)
library(gee)
source("0_DGP.R")
source("1_fit_models.R")
run_full_set <- function(sim_type = "complex_covars", nsims = 5, cores = 8, seed = NA){
if(!is.na(seed)){
set.seed(seed)
}
trt_clusts <- c(25, 50)
trt_clust_size <- c(5, 10)
control_clusts <- c(25, 50)
control_clust_size <- c(1, 5, 10)
txt_eff <- c(.25, 0, -.25)
v <- 20
binary <- c(T, F)
sim_type <- sim_type
params <- expand_grid(trt_clusts, trt_clust_size,
control_clusts, control_clust_size,
txt_eff, binary, v, sim_type)
params
one_set <- function(DGP_specification, nsims = 5, cores = 30){
# Get true PATE values by averaging...
pop <- bind_rows(lapply(as.list(1:1000), function(x) gen_obs_dat(LPS_only = T,
trt_clusts = DGP_specification[[1,"trt_clusts"]],
trt_clust_size = DGP_specification[[1,"trt_clust_size"]],
control_clusts = DGP_specification[[1,"control_clusts"]],
control_clust_size = DGP_specification[[1,"control_clust_size"]],
txt_eff = DGP_specification[[1,"txt_eff"]],
binary = DGP_specification[[1,"binary"]],
sim_type = DGP_specification[[1,"sim_type"]])))
PATE <- generate_summary_measures(LP0 = pop$LP0, LP1 = pop$LP1, binary = DGP_specification[[1,"binary"]])
registerDoParallel(cores = cores)
results <- data.frame(foreach(j = 1:nsims, .combine = rbind, .errorhandling = "remove") %dopar% {
fit_models(dat = gen_obs_dat(trt_clusts = DGP_specification[[1,"trt_clusts"]],
trt_clust_size = DGP_specification[[1,"trt_clust_size"]],
control_clusts = DGP_specification[[1,"control_clusts"]],
control_clust_size = DGP_specification[[1,"control_clust_size"]],
txt_eff = DGP_specification[[1,"txt_eff"]],
binary = DGP_specification[[1,"binary"]],
sim_type = DGP_specification[[1,"sim_type"]]),
binary = DGP_specification[[1,"binary"]],
control_clust_size = DGP_specification[[1,"control_clust_size"]],
v = DGP_specification[[1,"v"]])
})
stopImplicitCluster()
return(cbind.data.frame(results, PATE, DGP_specification, cores = cores, nsims = nsims))
}
return(data.frame(foreach(j = 1:nrow(params), .combine = rbind) %do%
{one_set(DGP_specification = params[j,], cores = cores,
nsims = nsims)}))
}