This repository has been archived by the owner on Dec 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
_targets.R
153 lines (130 loc) · 5.95 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
## Set options for using tar_make_clustermq()
## Uncomment and edit these lines to run model targets on a cluster computer via SSH
# options(
# clustermq.scheduler = "ssh",
# clustermq.ssh.host = "ericscott@hpg.rc.ufl.edu", # use your user and host
# clustermq.ssh.log = "~/cmq_ssh.log" # log for easier debugging
# # clustermq.worker.timeout = 2400
# )
## Uncomment these lines to run locally on multiple cores
options(
clustermq.scheduler = "multiprocess"
)
## Load your packages, e.g. library(targets).
source("./packages.R")
source("./conflicts.R")
## Load your R files
lapply(list.files("./R", full.names = TRUE), source)
## Set options
options(tidyverse.quiet = TRUE)
tar_option_set()
## tar_plan supports drake-style targets and also tar_target()
tar_plan(
# Prep SPEI data
maxlag = 36,
tar_target(xa_file, here("data", "xavier_daily_0.25x0.25.csv"), format = "file"),
xa_raw = read_csv(xa_file),
xa_spei = calc_spei_xa(xa_raw, scale = 3),
xa_lag = lag_spei(xa_spei, maxlag),
# Prep demographic data
tar_target(demog_file, here("data", "ha_bdffp_demog.csv"), format = "file"),
demog_raw = read_csv(demog_file,
col_names = TRUE,
cols(plot = col_character(),
bdffp_reserve_no = col_character(),
shts = col_integer(),
year = col_integer(),
infl = col_integer(),
tag_number = col_character(),
HA_ID_Number = col_character())),
demog_done = wrangle_demog(demog_raw),
model_data = join_filter_demog_spei(demog_done, xa_lag),
model_data_cf = dplyr::filter(model_data, habitat == "CF"),
model_data_1ha = dplyr::filter(model_data, habitat == "1-ha"),
model_data_cf_sub = subset_cf(model_data),
# Data validation
tar_render(validate_data, "doc/validate_data.Rmd", deployment = "main"),
# Select number of knots for GAMs
# tar_render(knot_choice, "doc/knot_choice.Rmd", deployment = "main"),
# Fit demographic models
tar_target(s_cf, fit_surv(model_data_cf, k = c(10,15,15))),
tar_target(s_1ha, fit_surv(model_data_1ha, k = c(10,10,10))),
tar_target(g_cf, fit_growth(model_data_cf, k = c(25,5,15))),
tar_target(g_1ha, fit_growth(model_data_1ha, k = c(10,5,15))),
tar_target(f_cf, fit_flwr(model_data_cf, k = c(10,15,15))),
tar_target(f_1ha, fit_flwr(model_data_1ha, k = c(10,10,18))),
# Validate and summarize results
### Check for edf differences due to sample size
tar_target(g_cf_sub, fit_growth(model_data_cf_sub, k = c(25,5,15))),
tar_target(f_cf_sub, fit_flwr(model_data_cf_sub, k = c(10,15,15))),
tar_target(s_cf_sub, fit_surv(model_data_cf_sub, k = c(10,15,15))),
tar_render(validate_models, "doc/appendix_A_model_validation.Rmd", deployment = "main"),
# Descriptive / Exploratory Data Analysis Figures
normals = normals_data(),
normals_plot = plot_normals(normals),
plot_dates = get_plot_daterange(model_data, maxlag),
eda_spei = plot_eda_spei(xa_lag, plot_dates),
eda_surv_ts = plot_eda_surv_ts(model_data, plot_dates),
eda_size = plot_eda_size_foldchange(model_data, plot_dates),
eda_flwr = plot_eda_flwr(model_data, plot_dates),
eda_plot = plot_eda_combine(eda_size, eda_surv_ts, eda_flwr, eda_spei),
surv_curve = plot_eda_surv_cohort(demog_done),
# Model output figures
## Survival
s_cf_eval = my_eval_smooth(s_cf),
s_1ha_eval = my_eval_smooth(s_1ha),
s_spei_plot = plot_cb_3panel(s_cf_eval, s_1ha_eval,
response_lab = "P(survival)"),
## Growth
g_cf_eval = my_eval_smooth(g_cf),
g_1ha_eval = my_eval_smooth(g_1ha),
g_spei_plot = plot_cb_3panel(g_cf_eval, g_1ha_eval,
response_lab = "$log(size_{t+1})$"),
## Flowering
f_cf_eval = my_eval_smooth(f_cf),
f_1ha_eval = my_eval_smooth(f_1ha),
f_spei_plot = plot_cb_3panel(f_cf_eval, f_1ha_eval,
response_lab = "P(flowering)"),
## Size covariate
s_covar_plot = plot_covar_smooth(frag_model = s_1ha, cf_model = s_cf,
covar = "log_size_prev") +
labs(x = TeX("$log(size_t)$"), y = "P(survival)"),
g_covar_plot = plot_covar_smooth(frag_model = g_1ha, cf_model = g_cf,
covar = "log_size_prev") +
labs(x = TeX("$log(size_t)$"), y = TeX("log(size_{t+1})")),
f_covar_plot = plot_covar_smooth(frag_model = f_1ha, cf_model = f_cf,
covar = "log_size_prev") +
labs(x = TeX("$log(size_t)$"), y = "P(flowering)"),
size_plot = make_size_plot(
s = s_covar_plot,
g = g_covar_plot,
f = f_covar_plot,
model_data = model_data
),
# Tables
intercept_df = make_intercept_df(s_cf, s_1ha, g_cf, g_1ha, f_cf, f_1ha),
results_df = make_results_df(s_cf, s_1ha, g_cf, g_1ha, f_cf, f_1ha),
# Main text
tar_render(paper, "doc/paper.Rmd", deployment = "main"),
# Supplemental
tar_target(rpde_file,
here("data", "supplemental", "Estacao_Rio Preto da Eva_1980-01-01_2016-12-31.csv"),
format = "file"),
tar_target(manaus_file,
here("data", "supplemental", "Estacao_Manaus_1980-01-01_2016-12-31.csv"),
format = "file"),
rpde = read_csv(rpde_file),
manaus = read_csv(manaus_file),
embrapa_mon = tidy_embrapa(rpde, manaus),
embrapa_wide = calc_spei_embrapa(embrapa_mon),
tar_target(trmm_file, here("data", "supplemental", "trmm.csv"), format = "file"),
trmm = read_tidy_trmm(trmm_file),
tar_target(gspei_file,
here("data", "supplemental", "global_spei_-59.75_-2.25.csv"),
format = "file"),
gspei = read_tidy_gspei(gspei_file),
trmm_spei = calc_spei_trmm(trmm, xa_spei),
tar_target(la_file, here("data", "HA-la-stems-ht.xlsx"), format = "file"),
la_data = read_tidy_la(la_file),
tar_render(supplemental, "doc/appendix_B_supplemental_figs.Rmd", deployment = "main")
)