generated from mattocci27/quarto-targets-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_targets.R
137 lines (129 loc) · 2.88 KB
/
_targets.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
library(targets)
library(tarchetypes)
library(tidyverse)
library(stantargets)
library(furrr)
library(bayesplot)
source("R/functions.R")
# parallel computing on local or on the same node
plan(multicore)
options(clustermq.scheduler = "multicore")
tar_option_set(packages = c(
"tidyverse",
"cmdstanr",
"bayesplot",
"ggrepel",
"patchwork",
"janitor",
"loo"
))
# keep memory usage down to a minimum
tar_option_set(
garbage_collection = TRUE,
memory = "transient"
)
list(
# eight schools
tar_target(
schools_data,
list(
J = 8,
y = c(28, 8, -3, 7, -1, 1, 18, 12),
sigma = c(15, 10, 16, 11, 9, 11, 10, 18))
),
# fit a single stan model
tar_stan_mcmc(
fit,
"stan/model.stan",
data = schools_data,
refresh = 0,
chains = 3,
parallel_chains = 1,
iter_warmup = 2000,
iter_sampling = 2000,
adapt_delta = 0.9,
max_treedepth = 15,
seed = 123,
return_draws = TRUE,
return_diagnostics = TRUE,
return_summary = TRUE,
summaries = list(
mean = ~mean(.x),
sd = ~sd(.x),
mad = ~mad(.x),
~posterior::quantile2(.x, probs = c(0.025, 0.05, 0.25, 0.5, 0.75, 0.95, 0.975)),
posterior::default_convergence_measures()
)
),
# prepare different tau values to pass dynamic branches
tar_target(
sim_tau,
seq(0.01, 40, length = 20)
),
# schools data with different tau values (dynamic branches)
tar_target(
simulated_schools_data,
simulate_schools_data(sim_tau),
pattern = map(sim_tau)
),
# compile stan model so that targets can track
tar_target(
simulation_stan,
compile_model("stan/simulation.stan"),
format = "file"
),
# fit stan model to each simulated data (dynamic branches)
tar_target(
sim_fit,
fit_sim_model(
data = simulated_schools_data,
simulation_stan,
refresh = 0,
chains = 4,
parallel_chains = 1,
iter_warmup = 2000,
iter_sampling = 2000,
adapt_delta = 0.9,
max_treedepth = 15,
seed = 123),
pattern = map(simulated_schools_data)
),
# export summary csv
tar_target(
summary_csv,
my_write_csv(fit_summary_model, "data/fit_summary_model.csv"),
format = "file"
),
# produce png and pdf figures
tar_target(
theta_tau_line_plot, {
p <- theta_tau_line(sim_fit)
my_ggsave(
"figs/theta_tau_line",
p,
height = 3.5,
width = 3.5
)
},
format = "file"
),
tar_target(
theta_intervals_plot, {
p <- mcmc_intervals(fit_draws_model, regex_pars = "theta\\[")
my_ggsave(
"figs/theta_intervals",
p,
height = 3.5,
width = 3.5
)
},
format = "file"
),
# it's hard to debug quarto using targets at the moment
# tar_quarto(
# manuscript_pdf,
# "ms/manuscript.qmd"
# ),
# putting NULL makes easy to remove tar_target from this list
NULL
)