-
Notifications
You must be signed in to change notification settings - Fork 3
/
2_mortality_models.R
324 lines (192 loc) · 7.5 KB
/
2_mortality_models.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# Run mortality models
# Includes the following steps:
# 1) Grid search over different density definitions
# 2) Grid search summary to identify density definition for main analysis
# 3) Main mortality models (include more output)
# 4) Mortality models with randomized mortality/conspecific density
# NOTE: Grid search is time consuming!
library(tidyr)
library(dplyr)
library(parallel)
library(pbapply)
# Define location of input and output -------------------------------------
# if not stated otherwise, load and save from/in repo folders
# Data prep input
if (!exists("path_input")) path_input = "data_prep/input/"
# Data prep output
if (!exists("path_output")) path_output = "data_prep/output/"
# Analysis outputs
if (!exists("path_mortality")) path_mortality = "out/mortality_models/"
# create folder for output
dir.create(path_mortality)
# Define sites ------------------------------------------------------------
# sites where tree or stem data is available
sites = unique(unlist(lapply(strsplit(c(list.files(paste0(path_input, "data_tree/"))
, list.files(paste0(path_input, "data_stem/"))),
"_"), "[[", 1)))
# Grid search mortality models --------------------------------------------
# define model runs
runs = c("gridsearchBAexpn", "gridsearchBAexp", "gridsearchNexpn", "gridsearchNexp")
for (run in runs) {
# decay definitions
decay_def = seq(1, 25, 2)
decays_con = decays_tot = decay_def
# chose n cores
ncpu <- detectCores()-1
ncpu <- ifelse(ncpu > 10, 3*length(decay_def), ncpu)
# settings
settings = expand.grid(decay_tot = decays_tot
, decay_con = decays_con
, site = sites, stringsAsFactors = F)
settings = split(settings, seq(nrow(settings)))
# create directory
dir.create(paste0(path_mortality, run))
for (decay_con in decays_con) {
for (decay_tot in decays_tot) {
dir.create(paste0(path_mortality, run, "/"
, sprintf("%02d", decay_con), "_", sprintf("%02d", decay_tot)))
}
}
cl <- makeCluster(ncpu)
source_setting = function(setting, run = run, paths) {
site = setting$site
decay_con = setting$decay_con
decay_tot = setting$decay_tot
source(paste0("code/mortality_models/mortality_", run, ".R"), local = T)
}
result <- pblapply(cl = cl, settings, source_setting, run = run,
paths = list(path_output = path_output,
path_input = path_input,
path_mortality = path_mortality))
stopCluster(cl)
# Combine results
coefs_combined = data.frame()
sums_combined = data.frame()
for (decay_tot in decays_tot) {
for (decay_con in decays_con) {
# objects for global results
coefs_global = data.frame()
sums_global = data.frame()
# vector of output object names
output_objects = gsub("_global", "", ls()[grepl("_global", ls())])
for (site in sites) {
# load output objects
load(paste0(path_mortality, run, "/"
, sprintf("%02d", decay_con), "_", sprintf("%02d", decay_tot), "/"
, site, "_mortality.Rdata"))
# combine output objects
for (i in output_objects) {
temp = get(i)
temp$site = site
assign(paste0(i, "_global"), rbind(get(paste0(i, "_global")), temp))
}
}
# combine different decay settings
for (i in output_objects) {
temp = get(paste0(i, "_global"))
temp$decay_con = decay_con
temp$decay_tot = decay_tot
assign(paste0(i, "_combined"), rbind(get(paste0(i, "_combined")), temp))
}
}
}
# Save result
save(list = ls()[grepl("_combined", ls()) | ls() == "sites_ordered"]
, file = paste0(path_mortality, run, "/combined_mortality.Rdata"))
}
# Grid search summary -----------------------------------------------------
# identifies optimal density definitions
# results stored in folder gridsearch_summary
source("code/mortality_models/grid_search_summary.R")
# Main mortality models ---------------------------------------------------
# optimal density definitions for these models are obtained from analyzing the grid search results
# see folder grid_search_summary
# chose model run
run = "main"
# chose n cores
ncpu <- detectCores()-1
ncpu <- ifelse(ncpu > 10, length(sites), ncpu)
# create directory
dir.create(paste0(path_mortality, run))
cl <- makeCluster(ncpu)
source_site = function(site, run, paths = paths) {
source(paste0("code/mortality_models/mortality_", run, ".R"), local = T)
}
result <- pblapply(cl = cl, sites, source_site, run = run,
paths = list(path_output = path_output,
path_input = path_input,
path_mortality = path_mortality))
stopCluster(cl)
# Session info
sink(paste0(path_mortality, run, "/", "sessionInfo.txt"))
sessionInfo()
sink()
# Combine results
# objects for global results
coefs_global = data.frame()
sums_global = data.frame()
AME_global = data.frame()
AMEsamples_global = data.frame()
rAME_global = data.frame()
rAMEsamples_global = data.frame()
nsp_global = data.frame()
# vector of output object names
output_objects = gsub("_global", "", ls()[grepl("_global", ls())])
for (site in sites) {
# load output objects
load(paste0(path_mortality, run, "/", site, "_mortality.Rdata"))
# combine output objects
for (i in output_objects) {
temp = get(i)
temp$site = site
assign(paste0(i, "_global"), rbind(get(paste0(i, "_global")), temp))
}
}
# Save result
save(list = ls()[grepl("_global", ls())]
, file = paste0(path_mortality, run, "/global_mortality.Rdata"))
rm(list = ls()[!(grepl("site", ls()) | grepl("path", ls()))])
# Randomization mortality models ------------------------------------------
# define model runs
runs = c("randomizedMort", "randomizedConD")
# chose n cores
ncpu <- detectCores()-1
ncpu <- ifelse(ncpu > 10, length(sites), ncpu)
for (run in runs) {
# create directory
dir.create(paste0(path_mortality, run))
cl <- makeCluster(ncpu)
source_site = function(site, run, paths = paths) {
source(paste0("code/mortality_models/mortality_", run, ".R"), local = T)
}
result <- pblapply(cl = cl, sites, source_site, run = run,
paths = list(path_output = path_output,
path_input = path_input,
path_mortality = path_mortality))
stopCluster(cl)
# Combine results
# objects for global results
coefs_global = data.frame()
sums_global = data.frame()
AME_global = data.frame()
AMEsamples_global = data.frame()
rAME_global = data.frame()
rAMEsamples_global = data.frame()
nsp_global = data.frame()
# vector of output object names
output_objects = gsub("_global", "", ls()[grepl("_global", ls())])
for (site in sites) {
# load output objects
load(paste0(path_mortality, run, "/", site, "_mortality.Rdata"))
# combine output objects
for (i in output_objects) {
temp = get(i)
temp$site = site
assign(paste0(i, "_global"), rbind(get(paste0(i, "_global")), temp))
}
}
# Save result
save(list = ls()[grepl("_global", ls())]
, file = paste0(path_mortality, run, "/global_mortality.Rdata"))
}
rm(list = ls()[!(grepl("site", ls()) | grepl("path", ls()))])