-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1_Data_simulation.R
More file actions
215 lines (156 loc) · 6.8 KB
/
Copy path1_Data_simulation.R
File metadata and controls
215 lines (156 loc) · 6.8 KB
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
### simulating fake population trajectories
# Download the Pacific Decadal Oscilation data and save to rds
# library(rsoi)
# pdo <- download_pdo() # Run on February 28, 2022
# saveRDS(pdo,file = "data/pdo.rds")
### pdo is monthly PDO values Jan 1900 - Jan 2022
library(tidyverse)
library(bbsBayes2) # https://github.com/bbsBayes/bbsBayes2
# select real BBS data for PAWR -------------------------------------------
species = "Pine Warbler"
species_f <- gsub(species,pattern = " ",replacement = "_") #name without spaces
stratification = "bbs_usgs"
s <- stratify(by = stratification,
species = species,
release = 2022)
prep_data <- prepare_data(s)
prep_spat_data <- prepare_spatial(prep_data,
strata_map = load_map(stratification))
print(prep_spat_data$spatial_data$map) #visualise the spatial strata
# dataframe of all observations -------------------------------------------
real_data <- prep_spat_data$raw_data
min_year <- min(real_data$year)
# generate simulated trajectories -----------------------------------------
# set up spatial ----------------------------------------------------------
coord_f <- function(geometry, w_xy = "X"){ # function to extract single coord
tt = sf::st_centroid(geometry) %>%
sf::st_coordinates()%>%
as.data.frame()
return(tt[,w_xy])
}
base_map <- load_map(stratify_by = stratification) %>%
filter(strata_name %in% real_data$strata_name) %>%
select(strata_name,geom) %>%
mutate(x_centroid = coord_f(geom,"X"),
y_centroid = coord_f(geom,"Y"),
y_scale = as.numeric(scale(y_centroid)),
north = y_scale - min(y_scale),
south = max(y_scale) - y_scale)
vis_map <- ggplot(base_map)+
geom_sf(aes(fill = south))+
geom_sf_text(aes(x = x_centroid, y = y_centroid,
label = strata_name),
size = 1)+
scale_colour_viridis_c()
print(vis_map)
# a cyclical pattern similar to something climatic (~approximately 5 year cycle)
# shift the cycle intensity from east to west
#
### using the a loess smooth of the realised time-series of the pacific decadal oscillation
### span of the smooth set to 10 years
loess_sm <- function(x,year,sp){
sm <- loess(x~year,
span = sp)
return(predict(sm))
}
pdo <- readRDS("data/pdo.rds")
pdo_ann <- pdo %>%
group_by(Year) %>%
summarise(annual_mean_pdo = mean(PDO)) %>%
filter(Year >= min_year,
Year < 2022) %>%
mutate(loess_mean_pdo = loess_sm(annual_mean_pdo,Year,0.2)) %>%
rename(year = Year)
vis_pdo <- ggplot(data = pdo_ann,
aes(x = year,y = loess_mean_pdo))+
geom_line()+
geom_point(aes(x = year,y = annual_mean_pdo))
print(vis_pdo)
## Generate stratum smooths and intercepts ----------------------------------------
# latitudinal variation in the slopes
SLOPE_1 = 0.015# ~ 1.5%/year increase in north and stable in south
### use simple, smooth, x-y coordinate variation in betas and stratas
set.seed(2019)
random_year_effects <- rnorm(length(min_year:2021)*length(base_map$strata_name),0,0.05)
strata_base_trajs <- base_map %>%
sf::st_set_geometry(.,NULL) %>%
expand_grid(.,year = min_year:2021) %>%
left_join(.,pdo_ann,by = "year") %>%
mutate(year_centered = year-1995,
slope = SLOPE_1*sqrt(north),
pdo_str = annual_mean_pdo*0.05 + random_year_effects,
pdo_smooth = loess_mean_pdo*0.1*(north),
intercept = (south*0.5),
expected_strata = intercept + slope*year_centered + pdo_smooth + pdo_str,
expected_count = exp(expected_strata))
vis_trajs <- ggplot(strata_base_trajs,
aes(x = year,y = expected_strata))+
geom_line(aes(group = strata_name,colour = north))+
scale_colour_viridis_c()
print(vis_trajs)
saveRDS(strata_base_trajs,
"data/simulated_data_true_trajectories.rds")
#MAs <- round(log(c(1,5,10,20,50)),2)
MAs <- c(0.1,0.5,1,5,10)
# Loop Mean Abundance -----------------------------------------------------
for(ma in MAs){
log_ma <- round(log(ma),2)
ma_f <- gsub(as.character(ma),pattern = ".",replacement = "-",
fixed = TRUE)
sd_site <- 0.7
set.seed(2019)
site_effects <- rnorm(prep_spat_data$model_data$n_sites,0,sd_site)
sd_obs <- 0.4
set.seed(2019)
observer_effects <- rnorm(prep_spat_data$model_data$n_observers,0,sd_obs)
sd_noise <- 0.2
set.seed(2019)
noise_effects <- rnorm(prep_spat_data$model_data$n_counts,0,sd_noise)
#retrans <- 0.5*(sd_obs^2 + sd_noise^2)
retrans <- 0.5*(sd_obs^2 + sd_noise^2 + sd_site^2)
#adjust and select the strata mean log-scale expected values
strata_base_trajs_2 <- strata_base_trajs %>%
mutate(expected_strata = log_ma + intercept + slope*year_centered + pdo_smooth + pdo_str) %>%
select(strata_name,
year,
expected_strata)
# # center the site and observer effects within each stratum - just to ensure that the variation in sampling
## through time has minimal effect on the stratum mean counts
site_obs_df <- real_data %>%
select(strata_name,site,observer) %>%
distinct() %>%
group_by(strata_name) %>%
mutate(site_eff = as.numeric(scale(site_effects[site],scale = FALSE)),
observer_eff = as.numeric(scale(observer_effects[observer],scale = FALSE))) #%>%
# group_by(strata_name) %>%
# summarise(test = mean(site_eff),
# test_obs = mean(observer_eff))
# link to raw data
sim_real_data <- real_data %>%
left_join(.,strata_base_trajs_2,
by = c("strata_name","year")) %>%
left_join(.,site_obs_df,
by = c("strata_name","site","observer")) %>%
mutate(expected_log_count = expected_strata + site_eff + observer_eff + noise_effects)
mean_nuisance <-sim_real_data %>%
group_by(strata_name) %>%
summarise(mean_site = mean(site_effects[site]),
mean_obs = mean(observer_effects[observer]),
.groups = "keep")
strata_base_trajs_2 <- strata_base_trajs_2 %>%
mutate(expected_mean_count = exp(expected_strata + retrans))
saveRDS(strata_base_trajs_2,
paste0("data/simulated_data_true_trajectories_",ma_f,".rds"))
set.seed(2019)
sim_counts <- rpois(prep_spat_data$model_data$n_counts,exp(sim_real_data$expected_log_count))
prep_spat_data_new <- prep_spat_data
prep_data_new <- prep_data
# replace counts with simulated counts ------------------------------------
prep_spat_data_new$raw_data$count <- sim_counts
prep_spat_data_new$model_data$count <- sim_counts
saveRDS(prep_spat_data_new,paste0("data/simulated_spatial_data_mean",ma_f,".rds"))
prep_data_new$raw_data$count <- sim_counts
prep_data_new$model_data$count <- sim_counts
saveRDS(prep_data_new,paste0("data/simulated_hier_data_mean",ma_f,".rds"))
#save prepared data as rds for future modeling
}#end ma loop