-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_Convergence.R
252 lines (186 loc) · 7.13 KB
/
2_Convergence.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
## convergence confirmation for BBS results
library(bbsBayes2)
library(tidyverse)
library(foreach)
library(doParallel)
#setwd("C:/Users/SmithAC/Documents/GitHub/CWS_2022_BBS_Analyses")
#setwd("C:/GitHub/CWS_2023_BBS_Analyses")
# set output_dir to the directory where the saved modeling output rds files are stored
# output_dir <- "D:/CWS_2023_BBS_Analyses/output"
# output_dir <- "output"
output_dir <- "F:/CWS_2022_BBS_Analyses/output"
n_cores = 6
re_run <- TRUE # set to TRUE if re-assessing convergence of models
sp_list <- readRDS("species_list.rds") %>%
filter(model == TRUE)
# sp_re_fit <- readRDS(paste0("species_rerun_converge_fail_2024-12-04.rds"))
# sp_list <- sp_list %>%
# filter(english %in% sp_re_fit)
#
# sp_rerun <- c("Northern Shrike","Willow Ptarmigan", "Herring Gull",
# "Common Loon",
# "American Pipit",
# "Redpoll (Common/Hoary)")
# sp_list <- sp_list %>%
# filter(english %in% sp_rerun)
#
# build cluster -----------------------------------------------------------
cluster <- makeCluster(n_cores, type = "PSOCK")
registerDoParallel(cluster)
test <- foreach(i = rev(1:nrow(sp_list)),
.packages = c("bbsBayes2",
"tidyverse",
"cmdstanr"),
.errorhandling = "pass") %dopar%
{
#for(i in 1:4){
sp <- as.character(sp_list[i,"english"])
aou <- as.integer(sp_list[i,"aou"])
if(file.exists(paste0(output_dir,"/fit_",aou,".rds")) &
(!file.exists(paste0("Convergence/summ_",aou,".rds")) | re_run )){
# identifying first years for selected species ----------------------------
fy <- NULL
if(aou %in% c(4661,4660)){ #Alder and Willow Flycatcher
fy <- 1978 #5 years after the split
}
if(aou %in% c(10,11,22860)){ # Clark's and Western Grebe and EUCD
fy <- 1990 #5 years after the split and first year EUCD observed on > 3 BBS routes
}
if(aou == 6121){ # CAve Swallow
fy = 1985
}
strat <- "bbs_cws"
fit <- readRDS(paste0(output_dir,"/fit_",aou,".rds"))
summ <- get_summary(fit)
saveRDS(summ,paste0("Convergence/summ_",aou,".rds"))
}
}
parallel::stopCluster(cluster)
sp_list <- readRDS("species_list.rds") %>%
filter(model == TRUE)
# Compile convergence values ----------------------------------------------
re_compile <- TRUE
if(re_compile){
summ_comb <- NULL
for(i in 1:nrow(sp_list)){
sp <- as.character(sp_list[i,"english"])
aou <- as.integer(sp_list[i,"aou"])
if(file.exists(paste0("Convergence/summ_",aou,".rds"))){
summ <- readRDS(paste0("Convergence/summ_",aou,".rds")) %>%
mutate(species = sp,
sp_n = aou)
summ_comb <- bind_rows(summ_comb,
summ)
}
}
saveRDS(summ_comb,"Convergence/All_species_convergence_summary.rds")
}else{
summ_comb <- readRDS("Convergence/All_species_convergence_summary.rds")
}
sp_run <- summ_comb %>%
group_by(sp_n) %>%
summarise(max_rhat = max(rhat, na.rm = TRUE),
min_ess = min(ess_bulk,na.rm = TRUE))
sp_not_run <- sp_list %>%
left_join(.,sp_run,
by = c("aou" = "sp_n")) %>%
filter(is.na(max_rhat)) %>%
arrange(-n_routes)
sp_not_run_but_should <- sp_not_run %>%
filter(n_years > 20, n_routes > 20,
n_obs > 500)
if(nrow(sp_not_run_but_should) > 0){
stop(paste(nrow(sp_not_run_but_should),"species or more are missing, including",
paste(sp_not_run_but_should$english,collapse = ", "),
"confirm that they each have < 1 stratum"))
}
fail <- summ_comb %>%
#filter(rhat > 1.05) %>%
mutate(variable_type = str_extract(variable,"^\\w+"),
rhat_fail = ifelse(rhat > 1.05,TRUE,FALSE),
ess_fail = ifelse(ess_bulk < 100, TRUE, FALSE)) %>%
group_by(species,sp_n,variable_type,rhat_fail,ess_fail) %>%
summarise(n_fail = n(),
max_rhat = max(rhat),
mean_rhat = mean(rhat),
min_ess = min(ess_bulk),
mean_ess = mean(ess_bulk))
fail_ess <- summ_comb %>%
mutate(variable_type = str_extract(variable,"^\\w+"),
ess_fail = ifelse(ess_bulk < 100, TRUE, FALSE)) %>%
group_by(species,sp_n,variable_type,ess_fail) %>%
summarise(n_fail = n(),
min_ess = min(ess_bulk),
mean_ess = mean(ess_bulk))
sp_fail_ess <- fail_ess %>%
ungroup() %>%
filter(ess_fail) %>%
select(species) %>%
distinct() %>%
unlist()
ess_fail_sum <- fail_ess %>%
filter(species %in% sp_fail_ess,
!is.na(ess_fail)) %>%
pivot_wider(id_cols = c(species,sp_n,variable_type),
names_from = ess_fail,
values_from = c(n_fail,min_ess,mean_ess)) %>%
mutate(n_fail_FALSE = ifelse(is.na(n_fail_FALSE),0,n_fail_FALSE),
n_fail_TRUE = ifelse(is.na(n_fail_TRUE),0,n_fail_TRUE),
p_fail = n_fail_TRUE/(n_fail_FALSE+n_fail_TRUE))
ess_fail_rerun <- ess_fail_sum %>%
filter(p_fail >= 0.01 | (p_fail > 0 & grepl("^n",variable_type)))
### one off decision to not re-run RWBL - ess-fail only for 1.2% of the strata-intercepts (2/163)
ess_fail_rerun <- ess_fail_rerun %>%
filter(species != "Red-winged Blackbird")
paste(unique(ess_fail_rerun[,c("sp_n","species")]),collapse = ", ")
# rhat fails --------------------------------------------------------------
fail_rhat <- summ_comb %>%
mutate(variable_type = str_extract(variable,"^\\w+"),
rhat_fail = ifelse(rhat > 1.05, TRUE, FALSE)) %>%
group_by(species,sp_n,variable_type,rhat_fail) %>%
summarise(n_fail = n(),
max_rhat = max(rhat),
mean_rhat = mean(rhat))
sp_fail_rhat <- fail_rhat %>%
ungroup() %>%
filter(rhat_fail) %>%
select(species) %>%
distinct() %>%
unlist()
rhat_fail_sum <- fail_rhat %>%
filter(species %in% sp_fail_rhat,
!is.na(rhat_fail)) %>%
pivot_wider(id_cols = c(species,sp_n,variable_type),
names_from = rhat_fail,
values_from = c(n_fail,max_rhat,mean_rhat)) %>%
mutate(n_fail_FALSE = ifelse(is.na(n_fail_FALSE),0,n_fail_FALSE),
n_fail_TRUE = ifelse(is.na(n_fail_TRUE),0,n_fail_TRUE),
p_fail = n_fail_TRUE/(n_fail_FALSE+n_fail_TRUE))
rhat_fail_rerun <- rhat_fail_sum %>%
filter(p_fail >= 0.01 | (p_fail > 0 & grepl("^n",variable_type)))
paste(unique(rhat_fail_rerun[,c("sp_n","species")]),collapse = ", ")
species_re_run_combined <- unique(c(unique(rhat_fail_rerun$species),
unique(ess_fail_rerun$species)))
saveRDS(species_re_run_combined,
file = paste0("species_rerun_converge_fail_",as_date(Sys.Date()),".rds"))
# copy_model_file("gamye","spatial",
# dir = "models_alt")
# above is to provide a model file to set up some alternate priors
# for the convergence fails
# # explore site effect fails for a selected species ------------------------
#
#
# sp <- "Sandhill Crane"
# sp_sel <- summ_comb %>%
# filter(species == sp) %>%
# mutate(variable_type = str_extract(variable,"^\\w+"))
#
#
# sdste <- sp_sel %>%
# filter(grepl("sdste",variable_type))
# ste <- sp_sel %>%
# filter(grepl("ste_raw",variable_type)) %>%
# mutate(scaled_mean = mean*as.numeric(sdste$mean),
# scaled_lci = q5*as.numeric(sdste$mean),
# scaled_uci = q95*as.numeric(sdste$mean))
#