-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_models_enso.R
More file actions
263 lines (228 loc) · 8.11 KB
/
02_models_enso.R
File metadata and controls
263 lines (228 loc) · 8.11 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
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
library(rsoi)
library(janitor)
library(RColorBrewer)
library(lubridate)
library(ctmm)
library(tidyverse)
library(rethinking)
#get enso data
mei <- clean_names(download_mei())
d_mei <- mei[mei$year >= min(d_hr_gs$year),]
d_mei <- d_mei[complete.cases(d_mei),]
plot(d_mei$mei~d_mei$date)
#combie data frames, will do posterior across time series later
str(d_hr_gs)
str(d_mei)
elcol_pal <- rev(brewer.pal(3 , "RdYlBu"))
group_pal <- brewer.pal(11 , "Spectral")
d_mei$phase_index <- as.integer(d_mei$phase)
plot(d_mei$mei~d_mei$date , col=elcol_pal[d_mei$phase_index] , pch=19 , cex=0. , xlab="year" , ylab="MEI index")
mei_spl <- with(d_mei, smooth.spline(date, mei))
lines(mei_spl, col = "grey3")
abline(v=d_mei$date[1:33] , col="grey")
d_hr_gs_2 <- merge(d_hr_gs, d_mei , by="year")
d_hr_gs_2 <- d_hr_gs_2[d_hr_gs_2$month=="JJ",]
min(d_mei$year)
d_mei$year_index_overall <- d_mei$year - 1990
###mei consolidate
str(d_hr_gs_2)
mean_df <- aggregate(mei ~ year, d_mei, mean)
names(mean_df)[2] <- "mean_annual_mei"
max_df <- aggregate(mei ~ year, d_mei, max)
names(max_df)[2] <- "max_annual_mei"
min_df <- aggregate(mei ~ year, d_mei, min)
names(min_df)[2] <- "min_annual_mei"
sd_df <- aggregate(mei ~ year, d_mei, sd)
names(sd_df)[2] <- "sd_annual_mei"
### READ IN AREA DF
d_hr_gs <- read.csv("data/df_slpHRarea_group_size.csv")
##compile bigger data frames
d_hr_gs_3 <- merge(d_hr_gs, mean_df , by="year")
d_hr_gs_3 <- merge(d_hr_gs_3, min_df , by="year")
d_hr_gs_3 <- merge(d_hr_gs_3, max_df , by="year")
d_hr_gs_3 <- merge(d_hr_gs_3, sd_df , by="year")
#d_hr_gs_3 <- merge(d_hr_gs_3, d_akde , by="id")
d_hr_gs_3$year_index <- as.integer(as.factor(d_hr_gs_3$year))
d_mei_hr_data <- d_mei[is.element(d_mei$year , d_hr_gs_3$year),]
str(d_hr_gs_3)
# add columns
d_hr_gs_3$hr_area_mean <- d_hr_gs_3$area
d_hr_gs_3$hr_area_low <- d_hr_gs_3$low
d_hr_gs_3$hr_area_high <- d_hr_gs_3$high
d_hr_gs_3$group_index <- as.integer(as.factor(d_hr_gs_3$group))
d_hr_gs_3$group_size_std <- standardize(d_hr_gs_3$group_size)
list_area <- list(
hr_area_mean=d_hr_gs_3$hr_area_mean ,
hr_area_high=d_hr_gs_3$hr_area_high ,
hr_area_low=d_hr_gs_3$hr_area_low ,
mean_annual_mei=d_hr_gs_3$mean_annual_mei ,
min_annual_mei=d_hr_gs_3$min_annual_mei ,
max_annual_mei=d_hr_gs_3$max_annual_mei ,
sd_annual_mei=d_hr_gs_3$sd_annual_mei ,
group_index=d_hr_gs_3$group_index ,
group_size=d_hr_gs_3$group_size_std ,
year_index=d_hr_gs_3$year_index
)
list_area_2 <- list(
hr_area_mean=d_hr_gs_3$hr_area_mean ,
hr_area_high=d_hr_gs_3$hr_area_high ,
hr_area_low=d_hr_gs_3$hr_area_low ,
#hr_area_sd=d_hr_gs_3$hr_area_sd ,
hr_area_rate=d_hr_gs_3$rate ,
hr_area_shape=d_hr_gs_3$shape ,
group_index=d_hr_gs_3$group_index ,
group_size=d_hr_gs_3$group_size ,
group_size_std=d_hr_gs_3$group_size_std ,
log_group_size=log(d_hr_gs_3$group_size) ,
year_index=d_hr_gs_3$year_index,
mei=d_mei_hr_data$mei ,
year_mei=d_mei_hr_data$year ,
year_index_mei=as.integer(as.factor(d_mei_hr_data$year)),
N_years=length(unique(d_mei_hr_data$year)),
N=nrow(d_hr_gs_3) ,
N_groups=length(unique(d_hr_gs_3$group_index)) ,
mean_annual_mei=d_hr_gs_3$mean_annual_mei ,
min_annual_mei=d_hr_gs_3$min_annual_mei ,
max_annual_mei=d_hr_gs_3$max_annual_mei ,
sd_annual_mei=d_hr_gs_3$sd_annual_mei ,
kde_shape=d_hr_gs_3$shape ,
kde_rate=d_hr_gs_3$rate ,
kde_scale=d_hr_gs_3$scale ,
phase_index=d_mei_hr_data$phase_index,
N_mei = length(d_mei_hr_data$mei)
)
###visually inspect rate shape
library(rethinking)
library('dagitty')
set_ulam_cmdstan(TRUE)
###visually inspect eate shape
for(i in 10:20){
plot(density(rgamma(10000,shape=d_hr_gs_3$shape[[i]], rate=d_hr_gs_3$rate[[i]] ) , xlim=c(0,10)) , main="blah" )
lines(density(rgamma(10000,shape=d_hr_gs_3$shape[[i]], scale=d_hr_gs_3$scale[[i]] ) ) , lty=2 )
points( d_hr_gs_3$area[i] , 0.1 )
segments( x0=d_hr_gs_3$low[i], y0=0.1 , x1=d_hr_gs_3$high[i] ,y1= 0.1 , col="blue")
}
##stan models
file_name <- 'stan_code/test_mei.stan'
fit= stan( file = file_name,
data = list_area_2 ,
iter = 1000,
chains=4,
cores=4,
control=list(adapt_delta=0.9) ,
refresh=100,
init=0,
seed=12
)
precis(fit , depth=2)
file_name <- 'stan_code/mei_hr.stan'
fit_hr= stan( file = file_name,
data = list_area_2 ,
iter = 4000,
chains=4,
cores=4,
control=list(adapt_delta=0.99) ,
refresh=250,
init=0,
seed=232
)
precis(fit_hr , depth=2)
#####hr shape scale
file_name <- 'stan_code/hr_meas.stan'
fit_hr_post= stan( file = file_name,
data = list_area_2 ,
iter = 4000,
chains=4,
cores=4,
control=list(adapt_delta=0.99) ,
refresh=250,
init=0,
seed=813
)
precis(fit_hr_post , depth=2)
file_name <- 'stan_code/hr_meas_varef.stan'
fit_hr_varef= stan( file = file_name,
data = list_area_2 ,
iter = 4000,
chains=4,
cores=4,
control=list(adapt_delta=0.99) ,
refresh=250,
init=0,
seed=813
)
precis(fit_hr_varef , depth=2)
##below is model in supplemental actually used in paper
file_name <- 'stan_code/hr_mei_meas_er.stan'
fit_hr_mei_meas_er= stan( file = file_name,
data = list_area_2 ,
iter = 4000,
chains=4,
cores=4,
control=list(adapt_delta=0.99) ,
refresh=250,
init=0,
seed=3169
)
precis(fit_hr_mei_meas_er , depth=2 , c("sigma_g"))
post_hrgsmei <- extract.samples(fit_hr_mei_meas_er)
#precis(fit_hr, depth=2 , pars=c("sigma_g"))
###below is model used in main result
file_name <- 'stan_code/hr_mei_gs_meas_er.stan'
fit_hr_mei_gs_meas_er= stan( file = file_name,
data = list_area_2 ,
iter = 4000,
chains=4,
cores=4,
control=list(adapt_delta=0.99) ,
refresh=250,
init=0,
seed=169
)
precis(fit_hr_mei_gs_meas_er , depth=2 , c("v_mu" ))
precis(fit_hr_mei_gs_meas_er , depth=2 , c("v_mu" , "sigma_g"))
precis(fit_hr_mei_gs_meas_er , depth=3, c("Rho_g") )
###home range only
file_name <- 'stan_code/hr_gs_meas_er.stan'
fit_hr_gs_meas_er= stan( file = file_name,
data = list_area_2 ,
iter = 4000,
chains=4,
cores=4,
control=list(adapt_delta=0.99) ,
refresh=250,
init=0,
seed=169
)
precis(fit_hr_gs_meas_er, depth=2 , pars=c("v_mu" , "sigma_g" , "k") )
post_hrgs <- extract.samples(fit_hr_gs_meas_er)
## home range on log scale, is same as if you don't log it does not matter
list_area_2_log <- list_area_2
list_area_2_log$group_size <- list_area_2_log$log_group_size
file_name <- 'stan_code/hr_gs_meas_er.stan'
fit_hr_loggs_meas_er= stan( file = file_name,
data = list_area_2_log ,
iter = 4000,
chains=4,
cores=4,
control=list(adapt_delta=0.99) ,
refresh=250,
init=0,
seed=169
)
precis(fit_hr_loggs_meas_er, depth=2 , pars=c("v_mu" , "sigma_g" , "k") )
post_hrgs <- extract.samples(fit_hr_gs_meas_er)
file_name <- 'stan_code/rip_mei_meas_er.stan' #stupid name
fit_mei_rip = stan( file = file_name,
data = list_rip ,
iter = 3000,
chains=4,
cores=4,
control=list(adapt_delta=0.99) ,
refresh=250,
seed=1239
)
post_meirip <- extract.samples(fit_mei_rip)
precis(fit_mei_rip , depth=2)
precis(fit_mei_rip , depth=3 , pars="v")
precis(fit_mei_rip , depth=3 , pars="Rho_g")