generated from isglobal-cep/template_new_paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_targets_sa_rqX.R
executable file
·137 lines (132 loc) · 3.63 KB
/
_targets_sa_rqX.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
source("DAGs/dag_v2.R")
source("R/dictionaries.R")
source("R/data.R")
source("R/utils.R")
targets::tar_option_set(
format = "qs"
)
params_dag <- list(
type = "minimal",
effect = "total"
)
rq <- Sys.getenv("TAR_PROJECT")
exposure <- switch(rq,
"rq1" = "chemical",
"rq2" = "chemical",
"rq3" = "biomarker",
"rq4" = "biomarker")
outcome <- switch(rq,
"rq1" = "outcome",
"rq2" = "biomarker",
"rq3" = "outcome",
"rq4" = "chemical")
if (rq == "rq2") {
tbl_outcomes <- tibble::tibble(
name = vars_of_interest(append_to_chem = NULL)$new_metabolites |>
stringr::str_to_lower(),
outcome = vars_of_interest(append_to_chem = NULL)$new_metabolites
)
} else {
tbl_outcomes <- tibble::tibble(
name = params(is_hpc = Sys.getenv("is_hpc"))$variables[[rq]]$outcome |>
stringr::str_to_lower(),
outcome = params(is_hpc = Sys.getenv("is_hpc"))$variables[[rq]]$outcome
)
}
# Effect modifier
by <- "e3_sex"
# Create folders to store results
invisible(lapply(c("figures"), function(x) {
path_save_res <- paste0(
"results/", x, "/", paste0(rq, "_SA")
)
if (!dir.exists(path_save_res)) {
dir.create(path_save_res)
}
}))
list(
targets::tar_target_raw(
name = paste0(rq, "_dag"),
command = expression(
load_dag(
dags = dags(),
exposure = exposure,
outcome = outcome,
params_dag = params_dag
)
)
),
# End dag target
targets::tar_target_raw(
name = paste0(rq, "_load_dat"),
command = substitute(
rq_load_data(res_dag = dag),
env = list(dag = as.symbol(paste0(rq, "_dag")))
)
),
# End load_dat target
##############################################################################
targets::tar_target_raw(
name = paste0(rq, "_preproc_dat"),
command = substitute(
rq_prepare_data(dat = dat,
filter_panel = FALSE, type_sample_hcp = NULL,
is_sa = TRUE),
env = list(dat = as.symbol(paste0(rq, "_load_dat")))
)
),
# End preproc_dat target
##############################################################################
targets::tar_target_raw(
name = paste0(rq, "_weights"),
command = substitute(
rq_estimate_weights(
dat = dat,
by = by,
include_selection_weights = FALSE,
save_results = TRUE,
parallel = FALSE,
workers = 10
),
env = list(dat = as.symbol(paste0(rq, "_preproc_dat")))
)
),
# End weights target
tarchetypes::tar_map(
values = tbl_outcomes,
names = "name",
targets::tar_target_raw(
name = paste0(rq, "_weighted_fits"),
command = substitute(
rq_fit_model_weighted(
dat = dat,
outcome = outcome,
by = c(by),
is_panel = FALSE,
weights = weights$estimated_weights,
parallel = FALSE,
workers = 10
),
env = list(
dat = as.symbol(paste0(rq, "_preproc_dat")),
weights = as.symbol(paste0(rq, "_weights"))
)
)
),
# End weighted_fits target
targets::tar_target_raw(
name = paste0(rq, "_marginal"),
command = substitute(
rq_estimate_marginal_effects(
fits = all_fits$fits,
by = by,
is_hcp = FALSE,
parallel = TRUE,
workers = 3
),
env = list(all_fits = as.symbol(paste0(rq, "_weighted_fits")))
)
) # End marginal target
) # End loop over outcomes
##############################################################################
)