-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfit_occupancy_models.Rmd
302 lines (215 loc) · 18.4 KB
/
fit_occupancy_models.Rmd
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
---
title: "Fit Occupancy Model"
author: "Daniel Hocking"
date: "Friday, April 17, 2015"
output: html_document
---
################################################
## Model fitting of catchments < 50 km2 ##
################################################
```{r load packages}
library(lme4)
library(arm)
library(boot) # needed for inv.logit function for AUC
library(AUC)
library(dplyr)
```
```{r load data}
df.fit <- readRDS(file = "Data/fit.RData")
df.valid <- readRDS(file = "Data/valid.RData")
df.means <- readRDS(file = "Data/means.RData")
```
## GLMM
## Global model with all covariates and check the optimizer:
(http://stackoverflow.com/questions/21344555/convergence-error-for-development-version-of-lme4)[http://stackoverflow.com/questions/21344555/convergence-error-for-development-version-of-lme4]
```{r glmm optimizer test, cache=TRUE, results='hide'}
# varying intercept: all covariates
g0.bobyqa <- glmer(pres ~ area:prcp + meanJulyTemp + forest + allonnet + (1|fhuc10), family = binomial(link = "logit"), data = df.fit, control=glmerControl(optimizer="bobyqa", check.conv.singular="warning")) # no warning
summary(g0.bobyqa)
g0.NM <- update(g0.bobyqa,control=glmerControl(optimizer="Nelder_Mead")) # warning
library(optimx)
g0.nlminb <- update(g0.bobyqa,control=glmerControl(optimizer="optimx",
optCtrl=list(method="nlminb"))) # no warning
g0.LBFGSB <- update(g0.bobyqa,control=glmerControl(optimizer="optimx",
optCtrl=list(method="L-BFGS-B"))) # warning
library(nloptr)
## from https://github.com/lme4/lme4/issues/98:
defaultControl <- list(algorithm="NLOPT_LN_BOBYQA",xtol_rel=1e-6,maxeval=1e5)
nloptwrap2 <- function(fn,par,lower,upper,control=list(),...) {
for (n in names(defaultControl))
if (is.null(control[[n]])) control[[n]] <- defaultControl[[n]]
res <- nloptr(x0=par,eval_f=fn,lb=lower,ub=upper,opts=control,...)
with(res,list(par=solution,
fval=objective,
feval=iterations,
conv=if (status>0) 0 else status,
message=message))
}
g0.bobyqa2 <- update(g0.bobyqa,control=glmerControl(optimizer=nloptwrap2)) #no warning
g0.NM2 <- update(g0.bobyqa,control=glmerControl(optimizer=nloptwrap2,
optCtrl=list(algorithm="NLOPT_LN_NELDERMEAD"))) # no warning
getpar <- function(x) c(getME(x,c("theta")),fixef(x))
modList <- list(bobyqa=g0.bobyqa,NM=g0.NM,nlminb=g0.nlminb,
bobyqa2=g0.bobyqa2,NM2=g0.NM2,LBFGSB=g0.LBFGSB)
ctab <- sapply(modList,getpar)
library(reshape2)
mtab <- melt(ctab)
library(ggplot2)
theme_set(theme_bw())
ggplot(mtab,aes(x=Var2,y=value,colour=Var2))+
geom_point()+facet_wrap(~Var1,scale="free")
ggplot(subset(mtab,Var2 %in% c("NM", "LBFGSB", "NM2","bobyqa","bobyqa2")),
aes(x=Var2,y=value,colour=Var2))+
geom_point()+facet_wrap(~Var1,scale="free")
```
Use the built-in bobyqa optimizer
## GLMM modeling
Global model and random effects selection
```{r glmm comparison 1, cache=TRUE, results='hide'}
# Global model wih varying intercept
glmm.M01 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
summary(glmm.M01)
# plot residuals of glmm.M1 by HUC10 basin
plot(df.fit$fhuc10, resid(glmm.M01), xlab="HUC 10 basin", ylab="Residuals")
# varying intercept & flow
glmm.M02 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + summer_prcp_mm|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
summary(glmm.M02)
# varying intercept & forest
glmm.M03 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + forest|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
summary(glmm.M03)
# varying intercept & area
glmm.M04 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + area|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
summary(glmm.M04)
# varying intercept & tmax.stream
glmm.M05 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + meanJulyTemp|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# varying intercept & devel
glmm.M06 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + devel_hi|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# varying intercept & ag
glmm.M07 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + agriculture|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# varying intercept & allonnet
glmm.M08 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + allonnet|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# compare models using AIC
AIC(glmm.M01, glmm.M02, glmm.M03, glmm.M04, glmm.M05, glmm.M06, glmm.M07, glmm.M08)
# plot residuals by HUC10 basin
plot(df.fit$fhuc10, resid(glmm.M04), xlab="HUC 10 basin", ylab="Residuals")
```
## The model (M04) with varying-intercept & area, with all covariates is better than intercept-only model
## Add more random effects to varying-intercept & forest model
```{r glmm comparison 2}
# varying intercept & forest & flow
glmm.M11 <-glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + area + summer_prcp_mm|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# varying intercept & forest & rise.slope
glmm.M12 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + area + forest|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# varying intercept & forest & tmax.stream
glmm.M13 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + area + meanJulyTemp|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# varying intercept & forest & surfC
glmm.M14 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + area |fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# varying intercept & forest & wet
glmm.M15 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + area + allonnet|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# varying intercept & forest & wet
glmm.M16 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + area + agriculture|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# varying intercept & forest & wet
glmm.M17 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + area + devel_hi|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# compare models using AIC
AIC(glmm.M04, glmm.M11, glmm.M12, glmm.M13, glmm.M14, glmm.M15, glmm.M16, glmm.M17)
# random forest (M03) is best
```
## Add more random effects to varying-intercept & forest model
```{r glmm comparison 2b}
# varying intercept & forest & flow
glmm.M21 <-glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + area + agriculture + summer_prcp_mm|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# varying intercept & forest & rise.slope
glmm.M22 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + area + agriculture + forest|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# varying intercept & forest & tmax.stream
glmm.M23 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + area + agriculture + meanJulyTemp|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# varying intercept & forest & surfC
glmm.M24 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + area + agriculture |fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# varying intercept & forest & wet
glmm.M25 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + area + agriculture + allonnet|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# varying intercept & forest & wet
#glmm.M16 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + area + agriculture|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# varying intercept & forest & wet
glmm.M27 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + area + agriculture + devel_hi|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# compare models using AIC
AIC(glmm.M16, glmm.M21, glmm.M22, glmm.M23, glmm.M24, glmm.M25, glmm.M27)
# random forest (M21) is best
```
## Add more random effects to varying-intercept & forest model
```{r glmm comparison 2c}
# varying intercept & forest & flow
#glmm.M21 <-glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + area + agriculture + summer_prcp_mm|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# varying intercept & forest & rise.slope
glmm.M22c <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + area + agriculture + summer_prcp_mm + forest|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# varying intercept & forest & tmax.stream
glmm.M23c <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + area + agriculture + summer_prcp_mm + meanJulyTemp|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# varying intercept & forest & surfC
glmm.M24c <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + area + agriculture + summer_prcp_mm |fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# varying intercept & forest & wet
glmm.M25c <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + area + agriculture + summer_prcp_mm + allonnet|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# varying intercept & forest & wet
#glmm.M16 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + area + agriculture|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# varying intercept & forest & wet
glmm.M27c <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + area + agriculture + summer_prcp_mm + devel_hi|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
glmm.M28c <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + area + agriculture + summer_prcp_mm + forest + meanJulyTemp |fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa")) # warning that too many parameters
# compare models using AIC
AIC(glmm.M21, glmm.M22c, glmm.M23c, glmm.M24c, glmm.M25c, glmm.M27c, glmm.M28c)
# use M23c with random: 1 + area + agriculture + summer_prcp_mm + meanJulyTemp
```
## Now that random effects are set, compare reduced models
```{r glmm comparison 3: substracting interactions}
# remove interactions
# remove tmax.stream * forest
glmm.M31 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + summer_prcp_mm*forest + (1 + area + agriculture + summer_prcp_mm + meanJulyTemp|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# remove temp * ag
glmm.M32 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + area + agriculture + summer_prcp_mm + meanJulyTemp|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# remove summer_prcp_mm * forest
glmm.M33 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + agriculture*meanJulyTemp + meanJulyTemp * forest + (1 + area + agriculture + summer_prcp_mm + meanJulyTemp|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
AIC(glmm.M23c, glmm.M31, glmm.M32, glmm.M33)
# removing each interaction was better or no different so remove all
# No interactions
glmm.M34 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + (1 + area + agriculture + summer_prcp_mm + meanJulyTemp|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
AIC(glmm.M23c, glmm.M31, glmm.M32, glmm.M33, glmm.M34) # No temp*ag best M32
```
remove covariates
```{r model comparison 4 - remove individual covariates that were moderately correlated or we were skeptical about a priori}
# No ag
#glmm.M41 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + (1 + forest|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# No impoundment
glmm.M42 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + devel_hi + agriculture + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + area + agriculture + summer_prcp_mm + meanJulyTemp|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# No hydrologic/soil/bedrock characteristics
glmm.M43 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + allonnet + devel_hi + agriculture + meanJulyTemp * forest + summer_prcp_mm*forest + (1 + area + agriculture + summer_prcp_mm + meanJulyTemp|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# Simple model
glmm.M44 <- glmer(pres ~ area*summer_prcp_mm + meanJulyTemp + forest + agriculture + meanJulyTemp * forest + (1 + area + agriculture + summer_prcp_mm + meanJulyTemp|fhuc10), family = binomial(link = "logit"), data = df.fit, control = glmerControl(optimizer="bobyqa"))
# compare models using AIC
AIC(glmm.M32, glmm.M42, glmm.M43, glmm.M44) # M32 still the best model
(S32 <- summary(glmm.M32))
#cbind(S34$coefficients[-9 , "Estimate"], S41$coefficients[ , "Estimate"])
# plot residuals by HUC10 basin
plot(df.fit$fhuc10, resid(glmm.M32), xlab="HUC 10 basin",ylab="Residuals")
plot(glmm.M32)
summary(glmm.M32) # model M35 is the best. Use as final model
```
Use model 34
### Check VIF for potential multicolliniarity issues
```{r}
vif.lme <- function (fit) {
## adapted from rms::vif
v <- vcov(fit)
nam <- names(fixef(fit))
## exclude intercepts
ns <- sum(1 * (nam == "Intercept" | nam == "(Intercept)"))
if (ns > 0) {
v <- v[-(1:ns), -(1:ns), drop = FALSE]
nam <- nam[-(1:ns)] }
d <- diag(v)^0.5
v <- diag(solve(v/(d %o% d)))
names(v) <- nam
v }
vif.lme(glmm.M32)
```
```{r}
save(df.fit, glmm.M32, file='Data/fit_data_out.RData')
```
```{r}
library(stargazer)
```