-
Notifications
You must be signed in to change notification settings - Fork 1
/
Fit_model_simulated_data_fixed_slope.R
179 lines (121 loc) · 5.07 KB
/
Fit_model_simulated_data_fixed_slope.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
### Fitting model to simulated BBS data
library(bbsBayes)
library(tidyverse)
library(cmdstanr)
setwd("C:/GitHub/GAMYE_Elaboration")
#species <- "Pine Warbler"
species = "Yellow-headed Blackbird"
species_f <- gsub(species,pattern = " ",replacement = "_")
tp = "breakpoint_cycle"
MAs <- round(log(c(0.1,0.5,1,5,10,50)),2)# true mean abundances for different simulations
for(ma in MAs[c(6,1,2,3,4,5)]){
output_dir <- "output/"
out_base <- paste0("sim_fixed_slope",tp,"_",ma,"_BBS")
csv_files <- paste0(out_base,"-",1:3,".csv")
load(paste0("Data/Simulated_data_",ma,"_",tp,"_BBS.RData"))
if(!file.exists(paste0(output_dir,csv_files[1]))){
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
year_linear_col <- GAM_year$Year_linear_col
year_basis = GAM_year$Year_basis
nknots_year = GAM_year$nknots_Year
nknots_year_smooth = nknots_year-1
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,
year_linear_col = year_linear_col,
nknots_year_smooth = nknots_year_smooth,
#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",out_base,Sys.time()))
mod.file = "models/gamye_iCAR_fixed_slope_sim.stan"
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_1 = runif(1,0.01,0.1),
sdbeta_s = runif(nknots_year_smooth,0.01,0.1),
sdBETA = runif(1,0.01,0.1),
sdyear = runif(nstrata,0.01,0.1),
beta_1_raw = rnorm(nstrata,0,0.1),
BETA_1 = rnorm(1,0,0.1),
BETA_raw = rnorm(nknots_year_smooth,0,0.1),
beta_raw = matrix(rnorm(nknots_year_smooth*nstrata,0,0.01),nrow = nstrata,ncol = nknots_year_smooth))}
## compile model
model <- cmdstan_model(mod.file)
stanfit <- model$sample(
data=stan_data,
refresh=200,
chains=3, iter_sampling=1500,
iter_warmup=1500,
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)
#stanfit1 <- as_cmdstan_fit(files = paste0(output_dir,csv_files))
save(list = c("stanfit","stan_data","csv_files",
"out_base"),
file = paste0(output_dir,"/",out_base,"_gamye_iCAR.RData"))
}
}#end ma loop