-
Notifications
You must be signed in to change notification settings - Fork 1
/
Mapping_trends_25_species.R
352 lines (256 loc) · 10.5 KB
/
Mapping_trends_25_species.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# spatial patterns in mean trends among species ---------------------------
library(bbsBayes)
library(tidyverse)
library(cmdstanr)
library(posterior)
library(patchwork)
library(sf)
library(spdep)
library(ggforce)
#library(tidybayes)
#source("functions/mungeCARdata4stan.R")
source("functions/neighbours_define.R") ## function to define neighbourhood relationships
source("functions/prepare-jags-data-alt.R") ## small alteration of the bbsBayes function
source("functions/get_basemap_function.R") ## loads one of the bbsBayes strata maps
source("functions/posterior_summary_functions.R") ## functions similar to tidybayes that work on cmdstanr output
## changes captured in a commit on Nov 20, 2020
strat = "bbs_usgs"
model = "slope"
firstYear = 2004
lastYear = 2019
scope = "RangeWide"
check_conv = TRUE #set to TRUE to run convergence summary
conv_summary <- NULL
load("Data/sp_sel.RData")
for(species in sp_sel[1:5]){
##########
### Currently not working - needs to have full time-series models run
### with the same data-saving process as for example species
### remove the use of CV-based results, they don't include 2019
#########
species_f <- gsub(species,pattern = " ",replacement = "_",fixed = T)
species_f <- gsub(species_f,pattern = "'",replacement = "",fixed = T)
output_dir <- "output"
sp_file <- paste0(output_dir,"/",species_f,"_",scope,"_",firstYear,"_",lastYear,"_slope_route_iCAR.RData")
# load(sp_file)
load(paste0("data/",species_f,"CV_base_data.RData"))
output_dir <- "output"
# for(spp in c("_","_BYM_","_Non_spatial_")){
out_base <- paste0(species_f,"_",firstYear,"_",lastYear)
#
out_base_Non_spatial <- paste0(species_f,"_Non_spatial_",firstYear,"_",lastYear,"_CV")
csv_files_Non_spatial <- paste0(output_dir,"/",out_base_Non_spatial,"-",1:3,".csv")
stanfit_Non_spatial <- as_cmdstan_fit(files = csv_files_Non_spatial)
if(check_conv){
stanf_df_Non_spatial <- stanfit_Non_spatial$draws(format = "df")
conv_summ <- summarise_draws(stanf_df_Non_spatial) %>%
mutate(species = species,
model = out_base_Non_spatial)
conv_summary <- bind_rows(conv_summary,conv_summ)
}
out_base_iCAR <- paste0(species_f,"_iCAR_",firstYear,"_",lastYear,"_CV")
csv_files_iCAR <- paste0(output_dir,"/",out_base_iCAR,"-",1:3,".csv")
stanfit_iCAR <- as_cmdstan_fit(files = csv_files_iCAR)
if(check_conv){
stanf_df_iCAR <- stanfit_iCAR$draws(format = "df")
conv_summ <- summarise_draws(stanf_df_iCAR) %>%
mutate(species = species,
model = out_base_iCAR)
conv_summary <- bind_rows(conv_summary,conv_summ)
}
out_base_BYM <- paste0(species_f,"_BYM_",firstYear,"_",lastYear,"_CV")
csv_files_BYM <- paste0(output_dir,"/",out_base_BYM,"-",1:3,".csv")
stanfit_BYM <- as_cmdstan_fit(files = csv_files_BYM)
if(check_conv){
stanf_df_BYM <- stanfit_BYM$draws(format = "df")
conv_summ <- summarise_draws(stanf_df__BYM) %>%
mutate(species = species,
model = out_base_BYM)
conv_summary <- bind_rows(conv_summary,conv_summ)
failed_rhat <- conv_summary %>%
filter(rhat > 1.05)
failed_ess_bulk <- conv_summary %>%
filter(ess_bulk < 100)
write.csv(conv_summary,file = paste0("trends_etc/conv_summ_",out_base,".csv"))
}
# extract trends and abundances -------------------------------------------
routes_df <- data.frame(routeF = jags_data$routeF,
route = jags_data$route,
year = jags_data$r_year,
obs = jags_data$obser) %>%
group_by(route,routeF,obs) %>%
summarise(n_yr_obs = n(),
.groups = "drop") %>%
group_by(route,routeF) %>%
summarise(n_obs = n(),
mean_y_obs = mean(n_yr_obs),
max_y_obs = max(n_yr_obs),
.groups = "drop") %>%
distinct()
tr_f <- function(x){
t <- (exp(x)-1)*100
}
trend_long <- NULL
abund_long <- NULL
for(sppn in c("_iCAR","_BYM","_Non_spatial")){
if(sppn == "_iCAR"){stanfit <- stanfit_iCAR}
if(sppn == "_BYM"){stanfit <- stanfit__BYM}
if(sppn == "_Non_spatial"){stanfit <- stanfit_Non_spatial}
# Trend gather ------------------------------------------------------------
trendst <- posterior_samples(fit = stanfit,
parm = "beta",
dims = "routeF") %>%
posterior_sums(.,
dims = "routeF")%>%
left_join(.,routes_df,by = "routeF") %>%
mutate(trend = tr_f(mean),
trend_lci = tr_f(lci),
trend_uci = tr_f(uci),
trend_se = tr_f(sd))%>%
select(route,trend,trend_lci,trend_uci,trend_se)
trend_ct <- trendst %>%
rename_with(.,~paste0(.x,sppn),.cols = contains("trend"))
trendst <- trendst %>%
mutate(version = sppn)
if(sppn == "_iCAR"){trend_comp <- trend_ct}else{
trend_comp <- left_join(trend_comp,trend_ct,by = "route")
}
trend_long <- bind_rows(trend_long,trendst)
# abundance gather --------------------------------------------------------
abundst <- posterior_samples(fit = stanfit,
parm = "alpha",
dims = "routeF") %>%
posterior_sums(.,
dims = "routeF")%>%
left_join(.,routes_df,by = "routeF") %>%
mutate(abund = exp(mean),
abund_lci = exp(lci),
abund_uci = exp(uci),
abund_se = exp(sd))%>%
select(route,abund,abund_lci,abund_uci,abund_se)
abund_ct <- abundst %>%
rename_with(.,~paste0(.x,sppn),.cols = contains("abund"))
abundst <- abundst %>%
mutate(version = sppn)
if(sppn == "_iCAR"){abund_comp <- abund_ct}else{
abund_comp <- left_join(abund_comp,abund_ct,by = "route")
}
abund_long <- bind_rows(abund_long,abundst)
}#end sppn
trends_rand <- posterior_samples(fit = stanfit__BYM,
parm = "beta_rand",
dims = "routeF") %>%
posterior_sums(.,
dims = "routeF")%>%
left_join(.,routes_df,by = "routeF") %>%
mutate(trend = tr_f(mean),
trend_lci = tr_f(lci),
trend_uci = tr_f(uci),
trend_se = tr_f(sd),
abs_trend = abs(trend))%>%
select(route,trend,trend_lci,trend_uci,trend_se,abs_trend) %>%
rename_with(.,~paste0(.x,"_rand"),.cols = contains("trend"))
trends_space <- posterior_samples(fit = stanfit__BYM,
parm = "beta_space",
dims = "routeF") %>%
posterior_sums(.,
dims = "routeF")%>%
left_join(.,routes_df,by = "routeF") %>%
mutate(trend = tr_f(mean),
trend_lci = tr_f(lci),
trend_uci = tr_f(uci),
trend_se = tr_f(sd),
abs_trend = abs(trend))%>%
select(route,trend,trend_lci,trend_uci,trend_se,abs_trend) %>%
rename_with(.,~paste0(.x,"_space"),.cols = contains("trend"))
save(list = c("trend_comp",
"abund_comp",
"trends_rand",
"trends_space"),
file = "Figures/example_trend_comparison_data.RData")
### compare the differences to the number of routes, and years with single obs etc.
tcplot <- ggplot(data = trend_comp,
aes(x = trend_iCAR,y = trend_Non_spatial,colour = trend_se_Non_spatial))+
geom_abline(slope = 1,intercept = 0)+
geom_point(alpha = 0.5)+
scale_colour_viridis_c(aesthetics = "colour",direction = 1)+
theme_bw()
tcplot2 <- ggplot(data = trend_comp,
aes(x = trend_iCAR,y = trend_BYM,colour = trend_se_BYM))+
geom_abline(slope = 1,intercept = 0)+
geom_point(alpha = 0.5)+
scale_colour_viridis_c(aesthetics = "colour",direction = 1)+
theme_bw()
print(tcplot + tcplot2)
# generate route map ------------------------------------------------------
strata_map <- bbsBayes::load_map(stratify_by = strat)
#
#
# route_map <- unique(strat_data$route_strat[,c("rt.uni","Latitude","Longitude","strat_name")])
#
# route_map = st_as_sf(route_map,coords = c("Longitude","Latitude"))
# st_crs(route_map) <- 4269 #NAD83 commonly used by US federal agencies
# #load strata map
#
# route_map = st_transform(route_map,crs = st_crs(strata_map))
#
#
# route_map_out = inner_join(route_map,dat_plot,by = c("rt.uni" = "BBS_route",
# "strat_name" = "BBS_stratum"))
#
#
strata_bounds <- st_union(route_map) #union to provide a simple border of the realised strata
bb = st_bbox(strata_bounds)
xlms = as.numeric(c(bb$xmin,bb$xmax))
ylms = as.numeric(c(bb$ymin,bb$ymax))
trend_plot_map <- route_map %>%
left_join(.,trend_long,by = "route") %>%
left_join(.,abund_long,by = c("route","version"))
save(list = c("trend_plot_map"),
file = "Figures/example_trend_map_data.RData")
# trend_plot_map_ns <- route_map %>%
# left_join(.,trends_ns,by = "route") %>%
# left_join(.,abund,by = "route")
# MAPPING -----------------------------------------------------------------
# plot_trend <- TRUE #set to false to plot the abundance
# if(plot_trend){
breaks <- c(-7, -4, -2, -1, -0.5, 0.5, 1, 2, 4, 7)
lgnd_head <- "Mean Trend\n"
trend_title <- "Mean Trend"
labls = c(paste0("< ",breaks[1]),paste0(breaks[-c(length(breaks))],":", breaks[-c(1)]),paste0("> ",breaks[length(breaks)]))
labls = paste0(labls, " %/year")
trend_plot_map$Tplot <- cut(trend_plot_map$trend,breaks = c(-Inf, breaks, Inf),labels = labls)
#
# }else{
# breaks <- c(-2, -1, -0.5, -0.1, -0.05, 0.05, 0.1, 0.5, 1, 2)
# lgnd_head <- "Mean Scaled Abundance\n"
# trend_title <- "Mean Scaled Abundance"
# labls = c(paste0("< ",breaks[1]),paste0(breaks[-c(length(breaks))],":", breaks[-c(1)]),paste0("> ",breaks[length(breaks)]))
# labls = paste0(labls, " Abund")
# route_map_out$Tplot <- cut(route_map_out$mean_Abund,breaks = c(-Inf, breaks, Inf),labels = labls)
#
# }
map_palette <- c("#a50026", "#d73027", "#f46d43", "#fdae61", "#fee090", "#ffffbf",
"#e0f3f8", "#abd9e9", "#74add1", "#4575b4", "#313695")
names(map_palette) <- labls
tmap = ggplot(trend_plot_map)+
#geom_sf(data = realized_strata_map,colour = gray(0.8),fill = NA)+
geom_sf(data = strata_map,colour = gray(0.8),fill = NA)+
geom_sf(aes(colour = Tplot,size = abund))+
scale_size_continuous(range = c(0.05,2),
name = "Mean Count")+
scale_colour_manual(values = map_palette, aesthetics = c("colour"),
guide = guide_legend(reverse=TRUE),
name = paste0(lgnd_head,firstYear,"-",lastYear))+
coord_sf(xlim = xlms,ylim = ylms)+
facet_wrap(vars(version),nrow = 3)
# png(filename = paste0("Figures/images/",g_sel,"_Mean_Trends_",firstYear,".png"),
# res = 600,
# width = 20,
# height = 15,
# units = "cm")
pdf(file = paste0("Figures/",species_f,out_base,"_vs_nonspatial.pdf"),
height = 11,
width = 8.5)
print(tmap)
dev.off()