-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathFitting_GLMs.R
356 lines (322 loc) · 13.2 KB
/
Fitting_GLMs.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
346
347
348
349
350
351
352
353
354
355
356
### Location^3 Estimation Model Code
## Function to run GLMs on data from OM
## FUnction returns only the fitted and predicted values
## Code by Lewis Barnett and James Smith
#'dat' are all the observations from the OM
#'year_fcast' is the year used to split historical (fitting) and forecast (testing) data
#'type' is either 'tweedie' or 'delta'
#'covs' is a vector indicating model covariates, where:
# - E is enviro only
# - Sr is spatiotemporal random field only
# - ESt is enviro + spatial trend random field
# - ESr is enviro + spatiotemporal random field
# - c("E", "Sr", "ESt", "ESr") does them all
## Comments by LB:
# note that there is a much cleaner way of iterating through models
# but for now simplifying for clarity and consistency with GAM fit formatting above
# see ?sdmTMB for options
# # project to UTM with spTransform [probably only necessary when measuring COG? otherwise this changes the original data file halfway through the analysis]
# dat_ll <- dat
# coordinates(dat_ll) <- c("lon", "lat")
# proj4string(dat_ll) <- CRS("+proj=longlat +datum=WGS84")
# dat_utm <- spTransform(dat_ll,
# CRS("+proj=utm +zone=10 +datum=WGS84 +units=km"))
# dat = as.data.frame(dat_utm) # convert back from sp object to data frame
# need to remove future years from test data set via weights
weights <- rep(0,nrow(dat))
weights[which(dat$year <= year_fcast)] <- 1 #keep historical period
# subset of data with only presences
dat_pres <- dat[dat$pres == 1,]
# make SPDE (stochastic partial differential equation that these INLA-based methods rely on)
spde <- try(make_mesh(data = dat, xy_cols = c("lon","lat"), n_knots = 200), silent=TRUE) # increase knots to at least ~250 for WC data
spde_pos <- try(make_mesh(data = dat_pres, xy_cols = c("lon","lat"), n_knots = 200), silent=TRUE)
# plot(spde) # can vary number of knots to modify the mesh until you get what you want
tw_formula <- formula(paste("abundance ~ 0 +", env_formula))
delta1_formula <- formula(paste("pres ~ 0 +", env_formula))
delta2_formula <- formula(paste("log_abundance ~ 0 +", env_formula_deltaN)) #JS: I find logging abundance leads to fewer fitting issues
if ("E" %in% covs) { #Enviro only; no spatiotemporal random fields (but simple space?)
print("Fitting GLM-E")
if (type == "tweedie") {
glm_E <- try(sdmTMB( #'glmm3' in original script
formula = tw_formula,
time_varying = NULL,
spde = spde,
time = "year",
family = tweedie(link = "log"),
data = dat,
anisotropy = TRUE,
ar1_fields = FALSE,
weights = weights,
spatial_only = TRUE,
spatial_trend = FALSE,
quadratic_roots = TRUE,
control = sdmTMBcontrol(step.min = 0.01, step.max = 1)
))
P_glm_E <- predict(glm_E)
dat_hist$glm_E <- exp(P_glm_E$est[P_glm_E$year <= year_fcast])
dat_fcast$glm_E <- exp(P_glm_E$est[P_glm_E$year > year_fcast])
}
if (type == "delta") {
glm_E_P <- try(sdmTMB( #presence-absence part
formula = delta1_formula,
time_varying = NULL,
spde = spde,
time = "year",
family = binomial(link = "logit"),
data = dat,
anisotropy = TRUE,
ar1_fields = FALSE,
weights = weights,
spatial_only = TRUE,
spatial_trend = FALSE,
quadratic_roots = TRUE,
control = sdmTMBcontrol(step.min = 0.01, step.max = 1)
))
glm_E_N <- try(sdmTMB( #abundance when non-zero part
formula = delta2_formula,
time_varying = NULL,
spde = spde_pos,
time = "year",
family = gaussian(link = "identity"),
data = dat_pres,
anisotropy = TRUE,
ar1_fields = FALSE,
weights = weights[which(dat_pres$pres == 1)],
spatial_only = TRUE,
spatial_trend = FALSE,
quadratic_roots = TRUE,
control = sdmTMBcontrol(step.min = 0.01, step.max = 1)
))
P_glm_E_P <- predict(glm_E_P) #binomial part, link scale
P_glm_E_P$est_prob <- exp(P_glm_E_P$est)/(1 + exp(P_glm_E_P$est)) #as probabilities
P_glm_E_N <- predict(glm_E_N, newdata = dat) #abundance part
P_glm_E_P$est_delta <- P_glm_E_P$est_prob * exp(P_glm_E_N$est) #'exp' here if using log_abundance as response
#TEMPORARY
#plot above
# plot(aggregate(est_delta ~ year,data=P_glm_E_P, FUN="sum"), type='b')
#plot get_index()
# P_glm_E_N <- predict(glm_E_N, newdata = dat,return_tmb_object = TRUE ) #abundance part
# test <- get_index(P_glm_E_N, bias_correct = TRUE)
# ggplot(test, aes(year, est)) + geom_line() +
# geom_ribbon(aes(ymin = lwr, ymax = upr), alpha = 0.4) +
# xlab('Year') + ylab('Biomass estimate (metric tonnes)')
dat_hist$glm_E <- P_glm_E_P$est_delta[P_glm_E_P$year <= year_fcast]
dat_fcast$glm_E <- P_glm_E_P$est_delta[P_glm_E_P$year > year_fcast]
}
}
if ("Sr" %in% covs) { #only spatiotemporal random fields as AR1
print("Fitting GLM-Sr")
if (type == "tweedie") {
glm_Sr <- try(sdmTMB( #'glmm1' in original script
formula = abundance ~ 1,
time_varying = NULL,
spde = spde,
time = "year",
family = tweedie(link = "log"),
data = dat,
anisotropy = TRUE,
ar1_fields = TRUE, #false for spatiotemporal random fields as IID, true for ar1
weights = weights,
spatial_only = FALSE,
spatial_trend = FALSE, #true for spatiotemporal random fields as IID, false for ar1
control = sdmTMBcontrol(step.min = 0.01, step.max = 1)
))
P_glm_Sr <- predict(glm_Sr)
dat_hist$glm_Sr <- exp(P_glm_Sr$est[P_glm_Sr$year <= year_fcast])
dat_fcast$glm_Sr <- exp(P_glm_Sr$est[P_glm_Sr$year > year_fcast])
}
if (type == "delta") {
start <- Sys.time()
print("glm_Sr_P")
glm_Sr_P <- try(sdmTMB( #presence-absence part
formula = pres ~ 1,
time_varying = NULL,
spde = spde,
time = "year",
family = binomial(link = "logit"),
data = dat,
anisotropy = TRUE,
ar1_fields = TRUE,
weights = weights,
spatial_only = FALSE,
spatial_trend = FALSE,
control = sdmTMBcontrol(step.min = 0.01, step.max = 1)
))
end <- Sys.time(); round(end-start, 2)
start <- Sys.time()
print("glm_Sr_N")
glm_Sr_N <- try(sdmTMB( #non-zero abundance part
formula = log_abundance ~ 1,
time_varying = NULL,
spde = spde_pos,
time = "year",
family = gaussian(link = "identity"),
data = dat_pres,
anisotropy = TRUE,
ar1_fields = TRUE,
weights = weights[which(dat_pres$pres == 1)],
spatial_only = FALSE,
spatial_trend = FALSE,
control = sdmTMBcontrol(step.min = 0.01, step.max = 1)
))
end <- Sys.time(); round(end-start, 2)
P_glm_Sr_P <- predict(glm_Sr_P) #binomial part, link scale
P_glm_Sr_P$est_prob <- exp(P_glm_Sr_P$est)/(1 + exp(P_glm_Sr_P$est)) #as probabilities
P_glm_Sr_N <- predict(glm_Sr_N, newdata = dat) #abundance part
P_glm_Sr_P$est_delta <- P_glm_Sr_P$est_prob * exp(P_glm_Sr_N$est) #'exp' here if using log_abundance as response
dat_hist$glm_Sr <- P_glm_Sr_P$est_delta[P_glm_Sr_P$year <= year_fcast]
dat_fcast$glm_Sr <- P_glm_Sr_P$est_delta[P_glm_Sr_P$year > year_fcast]
}
}
if ("ESt" %in% covs) { # enviro and spatial trend random field, but no spatiotemporal random fields
print("Fitting GLM-ESt")
if (type == "tweedie") {
glm_ESt <- try(sdmTMB( #'glm4' in original script
formula = tw_formula,
time_varying = NULL,
spde = spde,
time = "year",
family = tweedie(link = "log"),
data = dat,
anisotropy = TRUE,
ar1_fields = FALSE,
weights = weights,
spatial_only = TRUE,
spatial_trend = TRUE,
quadratic_roots = TRUE
))
P_glm_ESt <- predict(glm_ESt)
dat_hist$glm_ESt <- exp(P_glm_ESt$est[P_glm_ESt$year <= year_fcast])
dat_fcast$glm_ESt <- exp(P_glm_ESt$est[P_glm_ESt$year > year_fcast])
}
if (type == "delta") {
glm_ESt_P <- try(sdmTMB( #presence-absence part
formula = delta1_formula,
time_varying = NULL,
spde = spde,
time = "year",
family = binomial(link = "logit"),
data = dat,
anisotropy = TRUE,
ar1_fields = FALSE,
weights = weights,
spatial_only = TRUE,
spatial_trend = TRUE,
quadratic_roots = TRUE
))
glm_ESt_N <- try(sdmTMB( #non-zero abundance part
formula = delta2_formula,
time_varying = NULL,
spde = spde_pos,
time = "year",
family = gaussian(link = "identity"),
data = dat_pres,
anisotropy = TRUE,
ar1_fields = FALSE,
weights = weights[which(dat_pres$pres == 1)],
spatial_only = TRUE,
spatial_trend = TRUE,
quadratic_roots = TRUE
))
P_glm_ESt_P <- predict(glm_ESt_P) #binomial part, link scale
P_glm_ESt_P$est_prob <- exp(P_glm_ESt_P$est)/(1 + exp(P_glm_ESt_P$est)) #as probabilities
P_glm_ESt_N <- predict(glm_ESt_N, newdata = dat) #abundance part
P_glm_ESt_P$est_delta <- P_glm_ESt_P$est_prob * exp(P_glm_ESt_N$est) #'exp' here if using log_abundance as response
dat_hist$glm_ESt <- P_glm_ESt_P$est_delta[P_glm_ESt_P$year <= year_fcast]
dat_fcast$glm_ESt <- P_glm_ESt_P$est_delta[P_glm_ESt_P$year > year_fcast]
}
}
if ("ESr" %in% covs) { # enviro and spatiotemporal random fields as AR1
print("Fitting GLM-ESr")
if (type == "tweedie") {
glm_ESr <- try(sdmTMB( #'glmm5' in original script
formula = tw_formula,
time_varying = NULL,
spde = spde,
time = "year",
family = tweedie(link = "log"),
data = dat,
anisotropy = TRUE,
ar1_fields = TRUE,
weights = weights,
spatial_only = FALSE,
spatial_trend = FALSE,
quadratic_roots = TRUE
))
P_glm_ESr <- predict(glm_ESr)
dat_hist$glm_ESr <- exp(P_glm_ESr$est[P_glm_ESr$year <= year_fcast])
dat_fcast$glm_ESr <- exp(P_glm_ESr$est[P_glm_ESr$year > year_fcast])
}
if (type == "delta") {
print("Fitting GLM-E_P")
glm_ESr_P <- try(sdmTMB( #presence-absence part
formula = delta1_formula,
time_varying = NULL,
spde = spde,
time = "year",
family = binomial(link = "logit"),
data = dat,
anisotropy = TRUE,
ar1_fields = TRUE,
weights = weights,
spatial_only = FALSE,
spatial_trend = FALSE,
quadratic_roots = TRUE
))
print("Fitting GLM-E_N")
glm_ESr_N <- try(sdmTMB( #non-zero abundance part
formula = delta2_formula,
time_varying = NULL,
spde = spde_pos,
time = "year",
family = gaussian(link = "identity"),
data = dat_pres,
anisotropy = TRUE,
ar1_fields = TRUE,
weights = weights[which(dat_pres$pres == 1)],
spatial_only = FALSE,
spatial_trend = FALSE,
quadratic_roots = TRUE
))
P_glm_ESr_P <- predict(glm_ESr_P) #binomial part, link scale
P_glm_ESr_P$est_prob <- exp(P_glm_ESr_P$est)/(1 + exp(P_glm_ESr_P$est)) #as probabilities
P_glm_ESr_N <- predict(glm_ESr_N, newdata = dat) #abundance part
P_glm_ESr_P$est_delta <- P_glm_ESr_P$est_prob * exp(P_glm_ESr_N$est) #'exp' here if using log_abundance as response
dat_hist$glm_ESr <- P_glm_ESr_P$est_delta[P_glm_ESr_P$year <= year_fcast]
dat_fcast$glm_ESr <- P_glm_ESr_P$est_delta[P_glm_ESr_P$year > year_fcast]
}
}
# # plot 'partial effect' for base model ***for some reason this isn't working - still says mismatch of years
# predict_glmm <- function(model, new_dat) {
# dummy = data.frame(year = unique(dat$year),
# lat=dat$lat[1],
# lon=unique(dat$lon[1]),
# temp_s = dat$temp_s[1],
# chla_s = dat$chla_s[1],
# mld_s = dat$chla_s[1])
# pred = predict(model,
# newdata=rbind(new_dat[,c("year","lat","lon","temp_s","chla_s", "mld_s")],
# dummy), xy_cols = c("lon", "lat"))
#
# # drop dummy data
# pred = pred[-c(seq(nrow(pred)-nrow(dummy)+1,nrow(pred))),]
# pred$abundance = dat_x$abundance # add true abundance
#
# return(pred)
# }
#
# newdat_temp <- data.frame(temp_raw=dat$temp, temp_s=dat$temp_s,
# chla_s=median(dat$chla_s),
# mld_s=median(dat$mld_s),
# lat=median(dat$lat),
# lon=median(dat$lon),
# year=median(dat$year))
# newdat_temp <- newdat_temp[order(newdat_temp$temp_s),]
#
# P1 <- predict_glmm(glm_E_P, newdat_temp)
# P1 <- exp(P1$est)/(1 + exp(P1$est))
# P2 <- predict_glmm(glm_E_N, newdat_temp)
# P2 <- exp(P2$est)
#
# plot(newdat_temp$temp_raw, P1, type="l", main="glm_E_P - Temp")
# plot(newdat_temp$temp_raw, P2, type="l", main="glm_E_N - Temp")