forked from AdamCSmithCWS/GAMYE_Elaboration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fit_model_Sim_BBS.R
175 lines (125 loc) · 5.04 KB
/
Fit_model_Sim_BBS.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
### Fitting model to simulated BBS data
library(bbsBayes)
library(tidyverse)
library(cmdstanr)
# species = "Pacific Wren"
# species_f <- gsub(species,pattern = " ",replacement = "_")
#
# load(paste0("Simulated_data_",species_f,"_BBS.RData"))
# smpl = "balanced"
# output_dir <- "output/"
# out_base <- paste0(species_f,"_",smpl,"_BBS")
#
# csv_files <- paste0(out_base,"-",1:3,".csv")
# fit_rstan <- rstan::read_stan_csv(csvfiles = paste0(output_dir,csv_files))
# shinystan::launch_shinystan(shinystan::as.shinystan(fit_rstan))
#
#
# stanfit1 <- as_cmdstan_fit(files = paste0(output_dir,csv_files))
for(species in c("Cerulean Warbler","Pacific Wren")){
species_f <- gsub(species,pattern = " ",replacement = "_")
load(paste0("Simulated_data_",species_f,"_BBS.RData"))
for(smpl in rev(c("balanced","realised"))){
# GEnerate data -----------------------------------------------------------
if(smpl == "balanced"){tmp_data = balanced}
if(smpl == "realised"){tmp_data = realised}
nsites = max(routes_df$Route_Factored)
nstrata = max(strata_df$Stratum_Factored)
ncounts = nrow(tmp_data)
nyears = max(tmp_data$Year_Index)
count = tmp_data$count
strat = tmp_data$Stratum_Factored
year = tmp_data$Year_Index
site = tmp_data$Route_Factored
N_edges = neighbours$N_edges
node1 = neighbours$node1
node2 = neighbours$node2
nknots_year = GAM_year$nknots_Year
year_basis = GAM_year$Year_basis
nobservers = max(tmp_data$Observer_Factored)
observer = tmp_data$Observer_Factored
nroutes_strata <- routes_df %>% arrange(Stratum_Factored,
Route_Factored) %>%
group_by(Stratum_Factored) %>%
summarise(nroutes = n())
nsites_strata <- as.integer(nroutes_strata$nroutes)
maxnsites_strata <- max(nsites_strata)
ste_mat <- matrix(data = 0,
nrow = nstrata,
ncol = maxnsites_strata)
for(i in 1:nstrata){
ste_mat[i,1:nsites_strata[i]] <- routes_df[which(routes_df$Stratum_Factored == i),"Route_Factored"]
}
nonzeroweight <- rep(1,nstrata)
nu <- 100 #fixed df for t-distributed noise
stan_data = list(#scalar indicators
nsites = nsites,
nstrata = nstrata,
ncounts = ncounts,
nyears = nyears,
#basic data
count = count,
strat = strat,
year = year,
site = site,
#spatial structure
N_edges = N_edges,
node1 = node1,
node2 = node2,
#GAM structure
nknots_year = nknots_year,
year_basis = year_basis,
#Observer information
nobservers = nobservers,
observer = observer,
#Ragged array information to link sites to strata
nsites_strata = nsites_strata,
maxnsites_strata = maxnsites_strata,
ste_mat = ste_mat,
#nu = nu,
#weights
nonzeroweight = nonzeroweight
)
# Fit model ---------------------------------------------------------------
print(paste("beginning",species,smpl,"with",nstrata,"strata",Sys.time()))
mod.file = "models/gamye_iCAR.stan"
## compile model
model <- cmdstan_model(mod.file)
# Initial Values ----------------------------------------------------------
init_def <- function(){ list(noise_raw = rnorm(ncounts,0,0.1),
strata_raw = rnorm(nstrata,0,0.1),
STRATA = 0,
sdstrata = runif(1,0.01,0.1),
#eta = 0,
yeareffect_raw = matrix(rnorm(nstrata*nyears,0,0.1),nrow = nstrata,ncol = nyears),
obs_raw = rnorm(nobservers,0,0.1),
ste_raw = rnorm(nsites,0,0.1),
sdnoise = runif(1,0.01,0.2),
sdobs = runif(1,0.01,0.1),
sdste = runif(1,0.01,0.2),
sdbeta = runif(nknots_year,0.01,0.1),
sdBETA = runif(1,0.01,0.1),
sdyear = runif(nstrata,0.01,0.1),
BETA_raw = rnorm(nknots_year,0,0.1),
beta_raw = matrix(rnorm(nknots_year*nstrata,0,0.01),nrow = nstrata,ncol = nknots_year))}
output_dir <- "output/"
out_base <- paste0(species_f,"_",smpl,"_BBS")
stanfit <- model$sample(
data=stan_data,
refresh=200,
chains=3, iter_sampling=1000,
iter_warmup=1000,
parallel_chains = 3,
#pars = parms,
adapt_delta = 0.8,
max_treedepth = 14,
seed = 123,
init = init_def,
output_dir = output_dir,
output_basename = out_base)
csv_files <- paste0(out_base,"-",1:3,".csv")
#stanfit1 <- as_cmdstan_fit(files = paste0(output_dir,csv_files))
save(list = c("stanfit","stan_data","csv_files"),
file = paste0(output_dir,"/",out_base,"_gamye_iCAR.RData"))
}#end smpl
}