-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBARS_example_runs.R
158 lines (107 loc) · 4.58 KB
/
BARS_example_runs.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
###################################################
#all of the heavy-tailed models for BARS
# using the settings applied to the CWS 2019 analysis.
inits <- NULL
n_burnin <- 20000
n_saved_steps = 1200
n_thin = 20
n_iter = ((n_saved_steps*n_thin))
n_chains = 3
n_adapt = NULL
dir.create("output", showWarnings = F)
library(dplyr)
library(tidyverse)
library(bbsBayes) #CRAN version
library(foreach)
library(doParallel)
#fetch_bbs_data()
stratified_data <- stratify(by = "bbs_cws")
allspecies.eng = stratified_data$species_strat$english
allspecies.fre = stratified_data$species_strat$french
allspecies.num = stratified_data$species_strat$sp.bbs
allspecies.file = str_replace_all(str_replace_all(allspecies.eng,"[:punct:]",replacement = ""),
"\\s",replacement = "_")
###################################################
# Analysis by Species X Model Combination
###################################################
models = c("gamye","gam","slope","firstdiff")
nspecies = length(allspecies.eng)
nrecs_sp = (table(stratified_data$bird_strat$AOU))
set.seed(2019)
sp.order = sample(size = nspecies,x = c(1:nspecies),replace = F)
splitters = c("Clark's Grebe","Western Grebe","Alder Flycatcher","Willow Flycatcher")
split_miny = c(1990,1990,1978,1978)
names(split_miny) <- splitters
# Set up parallel stuff
n_cores <- 4 #each core will run a jags model in parallel, so total requirement = n_cores*3
cluster <- makeCluster(n_cores, type = "PSOCK")
registerDoParallel(cluster)
i = which(allspecies.eng == "Barn Swallow")
fullrun <- foreach(mm = models,
.packages = 'bbsBayes',
.inorder = FALSE,
.errorhandling = "pass") %dopar%
{
species = allspecies.file[i]
species.eng = allspecies.eng[i]
species.num = allspecies.num[i]
miny = NULL
if(species.eng %in% splitters){
miny <- split_miny[species.eng]
}
##### one-off because this species was introduced in North America in the mid-1980s - meaningless to include pre 1990
if(species.eng == "Eurasian Collared-Dove"){
miny <- 1990
}
if(species.eng == "Cave Swallow"){
miny <- 1985
}
sp.dir = paste0("output/",mm,"_",species)
dir.create(sp.dir, showWarnings = F)
### this if file.exists line can be uncommented if re-running using previous versions as initial values
### or if re-starting analysis, in which case it only runs the model for species with no saved results
if(!file.exists(paste0(sp.dir, "/jags_mod_full.RData"))){
#if(!file.exists(paste0(sp.dir, "/jags_data.RData"))){
### optional approach to using previous runs as initial values
# load(paste0(sp.dir, "/jags_mod_full.RData"))
# inits <- get_final_values(jags_mod)
# n_burnin <- 0
# n_saved_steps = 1200
# n_thin = 20
# n_iter = ((n_saved_steps*n_thin))
#
# }else{
# inits <- NULL
# n_burnin <- 10000
# n_saved_steps = 1200
# n_thin = 20
# n_iter = ((n_saved_steps*n_thin))
# } #end if file.exists loop for setting initial values
rm(list = c("jags_data","jags_mod"))
#### identifying the K folds for cross-validation
## selecting stratified samples that remove 10% of data within each stratum
jags_data <- prepare_jags_data(strat_data = stratified_data,
species_to_run = species.eng,
min_max_route_years = 2,
min_year = miny,
model = mm,
heavy_tailed = T)
save(jags_data, file = paste0(sp.dir, "/jags_data.RData"))
jags_mod <- run_model(jags_data = jags_data,
n_iter = n_iter,
n_burnin = n_burnin,
n_chains = n_chains,
n_thin = n_thin,
parallel = T,
model_file_path = "model/GAMYE_Alt_prior.R",
inits = inits,
#modules = NULL,
parameters_to_save = c("n","n3"))
#my_sso <- shinystan::launch_shinystan(shinystan::as.shinystan(jags_mod$samples, model_name = "My_tricky_model"))
save(list = c("jags_mod","jags_data"), file = paste0(sp.dir, "/jags_mod_full.RData"))
rm(list = c("jags_mod","jags_data"))
### uncomment to restart analysis without overwriting any previous results
}# end if results don't yet exist
}#end of full model parallel loop
stopCluster(cl = cluster)
#save(list = c("sp.rerun"),file = "sp.rerun.RData")