Repository navigation
Expand file tree
/
Copy pathsapsuckRz_analysis.R
More file actions
612 lines (498 loc) · 33.7 KB
/
Copy pathsapsuckRz_analysis.R
File metadata and controls
612 lines (498 loc) · 33.7 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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
#sapsuckRz_anylsis
#load required packages
library(data.table)
library(ggplot2)
library(vegan)
library(dplyr)
library(reshape2)
library(lubridate)
library(lme4)
library(r2glmm)
library(car)
library(mgcv)
library(zoo)
source("custom_functions.r")
##load in aphid data
#Using fread in the data.table package because this file is quite large
aphid<-fread("https://ndownloader.figshare.com/files/9465745")
aphid<-aphid[,-1]
colnames(aphid)<-c("Year","State","Site","Date","Sex","Species","Captures")
##Looking for phenology change
aphid.max<-as.data.frame(aphid.cut %>% group_by(Year,Site,Aphid.Species) %>% slice(which.max(Captures)))
aphid.first<-as.data.frame(aphid.cut %>% group_by(Year,Site,Aphid.Species) %>% slice(Captures!=0) %>% slice(which.min(Date)))
#looking across all aphid species ####WE WILL NEED THIS LATTER####
aphid.all<-aggregate(Captures~Year+Date+Site,data=aphid,sum)
##degree day accumulation##
#load in weather data
weather<-read.csv("https://ndownloader.figshare.com/files/8448380")
#make weather data wide
weather.cast<-dcast(weather, date+siteid~param,value.var="data",sum)
names(weather.cast)<-c("date","site.id","max.temp","min.temp","precip")
#calculate degree days for each site in each year
#first replace missing values with interpolated data
weather.cast$max.temp<-replace.missing(weather.cast$max.temp)
weather.cast$min.temp<-replace.missing(weather.cast$min.temp)
weather.cast$precip<-replace.missing(weather.cast$precip)
#create day of year variable
weather.cast$date<-ymd(weather.cast$date)
weather.cast$doy<-yday(weather.cast$date)
#reorder so that we calculate degree days for each site in each year
weather.cast<-weather.cast[with(weather.cast, order(site.id,date)), ]
#flip some temp data because min is higher than max
weather.cast[weather.cast$min.temp > weather.cast$max.temp, c("max.temp", "min.temp")] <- weather.cast[weather.cast$min.temp > weather.cast$max.temp, c("min.temp", "max.temp")]
#degree days
weather.cast$dd<-allen(weather.cast$max.temp, weather.cast$min.temp, 10.000001)
#degree day accumulation
#starting March 1
weather.cast$dd.acum<-accum.allen(weather.cast$max.temp,weather.cast$min.temp,10.000001,weather.cast$doy,60)
##precipitation data
#precipitation accumulation over the growing season
#starting March 1
weather.cast$precip.accum<-accum.precip.time(weather.cast$precip,weather.cast$doy,60)
##Combine the weather and aphid data##
names(aphid.all)<-c("year","doy","site.id","captures")#so they match
weather.cast$year<-isoyear(weather.cast$date)
data<-merge(aphid.all,weather.cast,by=c("site.id","year","doy"),all=T)
#Calculate rolling averages degree day accumulation
data$dd.week<-rollmean(data$dd, 7, align = "right", fill = NA)
data$dd.month<-rollmean(data$dd, 30, align = "right", fill = NA)
data$dd.year<-data$dd.acum/data$doy
#Calculate rolling average precipitation
data$precip.week<-rollmean(data$precip, 7, align = "right", fill = NA)
data$precip.month<-rollmean(data$precip, 30, align = "right", fill = NA)
data$precip.year<-data$precip.accum/data$doy
#Calculate total precipitation before peak
data$precip.week.tot<-rollsum(data$precip, 7, align = "right", fill = NA)
data$precip.month.tot<-rollsum(data$precip, 30, align = "right", fill = NA)
##Start building models##
##load in landcover data##
cdl<-read.csv("Data/cdl.csv")
cdl.cut<-cdl[,c(2:3,11:50)]
colnames(cdl.cut)[1]<-"year"
colnames(cdl.cut)[2]<-"site.id"
#add landcover data to dataframe
cdl.cut$year<-as.factor(cdl.cut$year)
data$site.id<-as.factor(data$site.id)
cdl.cut<-aggregate(.~site.id+year,data=cdl.cut,mean)
data<-merge(data,cdl.cut,by=c("site.id","year"),all.x=T)
#adding lat long
coords<-read.csv("Data/counties+coordinates2.csv")
coords<-coords[,1:3]
colnames(coords)<-c("site.id","lat","long")
data<-merge(data,coords,by="site.id",all.x=T)
#rescale data
data.scale2<-scale(data[,c(12:55)],center=F,scale=T)
data.scale2<-cbind(data[,c(1:11)],data.scale2)
##Analyze some data!!!
mod1<-lmer(peak~precip.accum+forest+ag+(1|site.id)+(1|year),data=data.scale2)
Anova(mod1,type=3) #car
tiff("peak_precip.tiff",width = 4200, height = 4200, units = "px", res = 600)
ggplot(data, aes(x = precip.accum, y = peak))+
geom_point(size=2)+
geom_smooth(method="lm",size=2,color="red",se=F)+
annotate("text",label="p>0.001",x=190,y=1720,size=5)+
annotate("text",label="paste(R ^ 2, \" = 0.40\")",x=200,y=1800,parse = TRUE,size=5)+
labs(x = "Precipitation accumulation (mm)", y = "Degree day at peak aphid abundance")+
theme(text = element_text(size=24),axis.text=element_text(colour="black"),panel.background=element_blank(),panel.grid.major=element_blank(),panel.grid.minor=element_blank(),axis.line = element_line(size=.7, color="black"),legend.position="none")
dev.off()
###RESIDUAL PLOT FOR PEAKS###
mod.gam.peak.resid <- gam(peak ~ ag_corn10+ag_beans10+ag_smgrains10+forest10, data = data.scale2)
tiff("peak_precip_resid.tiff",width = 4200, height = 4200, units = "px", res = 600)
ggplot(na.omit(data.scale2), aes(x = precip.accum, y = resid(mod.gam.peak.resid)))+
geom_point(size=2)+
geom_smooth(method="lm",size=2,color="red",se=F)+
annotate("text",label="p>0.001",x=1.5,y=150,size=5)+
annotate("text",label="paste(R ^ 2, \" = 0.40\")",x=1.5,y=140,parse = TRUE,size=5)+
labs(x = "Precipitation accumulation (mm)", y = "Degree day at peak aphid abundance\n(model residuals)")+
theme(text = element_text(size=24),axis.text=element_text(colour="black"),panel.background=element_blank(),panel.grid.major=element_blank(),panel.grid.minor=element_blank(),axis.line = element_line(size=.7, color="black"),legend.position="none")
dev.off()
##total aphid abundance
aphid.total<-aggregate(captures~year+site.id,data=aphid.all,sum)
weather.max<-aggregate(.~site.id+year,data=weather.cast,max)
data2<-merge(aphid.total,weather.max,by=c("site.id","year"),all.x=T)
data2<-merge(data2,cdl.cut,by=c("site.id","year"),all.x=T)
#rescale variables because the orders of magnitude difference in scales is making glmer.nb angry
data2<-merge(data2,coords,by="site.id",all.x=T)
data.scale<-scale(data2[,9:51],center=F,scale=T)
data.scale<-cbind(data2[,1:8],data.scale,data2[,52:53])
#model
mod.pos<-glmer(captures~dd.acum+precip.accum+forest10+ag10+(1|site.id)+(1|year),data=data.scale,family=poisson)
Anova(mod.pos,type=3)
#Function for checking for overdispersion
overdisp_fun <- function(model) {
vpars <- function(m) {
nrow(m)*(nrow(m)+1)/2
}
model.df <- sum(sapply(VarCorr(model),vpars))+length(fixef(model))
rdf <- nrow(model.frame(model))-model.df
rp <- residuals(model,type="pearson")
Pearson.chisq <- sum(rp^2)
prat <- Pearson.chisq/rdf
pval <- pchisq(Pearson.chisq, df=rdf, lower.tail=FALSE)
c(chisq=Pearson.chisq,ratio=prat,rdf=rdf,p=pval)
}
overdisp_fun(mod.pos)
##
mod2<-glmer.nb(captures~dd.acum+precip.accum+forest10+ag_corn10+ag_beans10+ag_wheat10+ag_smgrains10+(1|site.id)+(1|year),data=data.scale)
Anova(mod2,type=3)
#plots
tiff("captures_dd.acum.tiff",width = 4200, height = 4200, units = "px", res = 600)
ggplot(data.scale, aes(x = dd.acum, y = captures))+
geom_point(size=2)+
stat_function(fun=function(x)exp(fixef(mod2)[1] + fixef(mod2)[2]*x),size=2,color="red")+
annotate("text",label="p<0.001",x=.865,y=21000,size=5)+
annotate("text",label="paste(R ^ 2, \" = 0.31\")",x=.865,y=20000,parse = TRUE,size=5)+
labs(x = "Degree day accumulation", y = "Total aphid abundance\n(# of captures)")+
theme(text = element_text(size=24),axis.text=element_text(color="black"),panel.background=element_blank(),panel.grid.major=element_blank(),panel.grid.minor=element_blank(),axis.line = element_line(size=.7, color="black"),legend.position="none")
dev.off()
###TOTAL CAPTURES RESIDUAL MODEL###
##Not working. I'm not sure we can really plot against the resid for this one because it has a negbi relationship
gam.captures.resid<-gam(captures ~ precip.accum+ag_corn10+ag_beans10+ag_smgrains10+forest10, data = data.scale,family="nb")
gam.captures<-gam(resid(gam.captures.resid)~dd.acum,data=na.omit(data.scale),family="nb")
tiff("captures_dd_acum_resid.tiff",width = 4200, height = 4200, units = "px", res = 600)
ggplot(na.omit(data.scale), aes(x = dd.acum, y = resid(gam.captures.resid)))+
geom_point(size=2)+
stat_function(fun=function(x)exp(coef(gam.captures)[1] + coef(gam.captures)[3]*x),size=2,color="red")+
#annotate("text",label="p<0.001",x=.865,y=21000,size=5)+
#annotate("text",label="paste(R ^ 2, \" = 0.31\")",x=.865,y=20000,parse = TRUE,size=5)+
labs(x = "Degree day accumulation", y = "Total aphid abundance\n(# of captures)")+
theme(text = element_text(size=24),axis.text=element_text(color="black"),panel.background=element_blank(),panel.grid.major=element_blank(),panel.grid.minor=element_blank(),axis.line = element_line(size=.7, color="black"),legend.position="none")
dev.off()
#GAM to add lat long spatial smoother
mod.gam1<-gam(peak ~ precip.accum+s(lat,long)+s(site.id,bs="re")+s(year,bs="re"), data = data)
summary(mod.gam1)
mod.gam2<-gam(captures ~ precip.accum+dd.acum+ag10+forest10+s(lat,long)+s(year,bs="re"), data = data.scale,family="nb")
summary(mod.gam2)
rsq.partial(mod.gam2,adj=T,type="lr")
mod.gam3<-gam(captures ~ precip.accum+dd.acum+ag_corn10+ag_beans10+ag_smgrains10+forest10+s(lat.x,long.x)+s(year,bs="re"), data = data.scale,family="nb")
summary(mod.gam3)
library(sandwich)
cov.mod.gam3 <- vcovHC(mod.gam3, type = "HC0")
stderr.mod.gam3 <- sqrt(diag(cov.mod.gam3))
r.est <- cbind(Estimate = coef(mod.gam3), "Robust SE" = stderr.mod.gam3,
"pr(>|z|" = 2 * pnorm(abs(coef(mod.gam3)/stderr.mod.gam3), lower.tail = F),
LL = coef(mod.gam3) - 1.96*stderr.mod.gam3,
UL = coef(mod.gam3) + 1.96*stderr.mod.gam3)
r.est
mod.gam2<-gam(captures ~ precip.accum+dd.acum+ag_corn10+ag_smgrains10+forest10+s(lat,long)+s(year,bs="re"), data = data.scale,family=nb())
summary(mod.gam2)
mod.gam3<-gam(captures ~ precip.accum+dd.acum+ag+forest+s(lat,long)+s(year,bs="re"), data = data.scale,family="poisson")
mod.corn<-glmer.nb(captures~ag_corn++(1|site.id)+(1|year),data=data.scale)
tiff("captures_corn.tiff",width = 4200, height = 4200, units = "px", res = 600)
ggplot(data.scale, aes(x = ag_corn, y = captures))+
geom_point(size=2)+
stat_function(fun=function(x)exp(fixef(mod.corn)[1] + fixef(mod.corn)[2]*x),size=2,color="red")+
annotate("text",label="p=0.002",x=.2,y=21000,size=5)+
annotate("text",label="paste(R ^ 2, \" = 0.29\")",x=.2,y=20000,parse = TRUE,size=5)+
labs(x = "% Landscape corn cover", y = "Total aphid abundance\n(# of captures)")+
theme(text = element_text(size=24),axis.text=element_text(color="black"),panel.background=element_blank(),panel.grid.major=element_blank(),panel.grid.minor=element_blank(),axis.line = element_line(size=.7, color="black"),legend.position="none")
dev.off()
mod.wheat<-glmer.nb(captures~ag_wheat+(1|site.id)+(1|year),data=data.scale) #something weird going on here
tiff("captures_wheat.tiff",width = 4200, height = 4200, units = "px", res = 600)
ggplot(data.scale, aes(x = ag_wheat, y = captures))+
geom_point(size=2)+
stat_function(fun=function(x)exp(fixef(mod.wheat)[1] + fixef(mod.wheat)[2]*x),size=2,color="red")+
annotate("text",label="p=0.01",x=.2,y=21000,size=5)+
annotate("text",label="paste(R ^ 2, \" = 0.27\")",x=.3,y=20000,parse = TRUE,size=5)+
labs(x = "% Landscape wheat cover", y = "Total aphid abundance\n(# of captures)")+
theme(text = element_text(size=24),axis.text=element_text(color="black"),panel.background=element_blank(),panel.grid.major=element_blank(),panel.grid.minor=element_blank(),axis.line = element_line(size=.7, color="black"),legend.position="none")
dev.off()
#The spatial smoother doesn't seem to matter
logLik(mod.gam2)
logLik(mod.gam3)
stat <- as.numeric(2 * (logLik(mod.gam2) - logLik(mod.gam3)))
pchisq(stat, df = 33.97 - 25.16, lower.tail = FALSE)
####Disaggregate species for Soybean Aphid (Aphis glycines) Cherry-Oat Aphid (Rhopalosiphum padi) Corn Leaf Aphid (Rhopalosiphum maidis)####
aphid.aglyc <- aggregate(Captures ~ Site + Date + Year, data = aphid[aphid$Species == "Aphis.glycines"], sum)
aphid.rpadi <- aggregate(Captures ~ Site + Date + Year, data = aphid[aphid$Species == "Rhopalosiphum.padi"], sum)
aphid.rmaidis <- aggregate(Captures ~ Site + Date + Year, data = aphid[aphid$Species == "Rhopalosiphum.maidis"], sum)
colnames(aphid.aglyc) <- c("site.id","doy","year","captures")
colnames(aphid.rpadi) <- c("site.id","doy","year","captures")
colnames(aphid.rmaidis) <- c("site.id","doy","year","captures")
#aglyc
##total aphid abundance
aphid.total.aglyc<-aggregate(captures~year+site.id,data=aphid.aglyc,sum)
weather.max<-aggregate(.~site.id+year,data=weather.cast,max)
data.aglyc<-merge(aphid.total.aglyc,weather.max,by=c("site.id","year"),all.x=T)
data.aglyc<-merge(data.aglyc,cdl.cut,by=c("site.id","year"),all.x=T)
##rescale variables because the orders of magnitude difference in scales is making glmer.nb angry
data.aglyc<-merge(data.aglyc,coords,by="site.id",all.x=T)
data.scale.aglyc<-scale(data.scale.aglyc[,9:51],center=F,scale=T)
data.scale.aglyc<-cbind(data.aglyc[,1:8],data.scale.aglyc,data.aglyc[,52:53])
#rpadi
##total aphid abundance
aphid.total.rpadi<-aggregate(captures~year+site.id,data=aphid.rpadi,sum)
weather.max<-aggregate(.~site.id+year,data=weather.cast,max)
data.rpadi<-merge(aphid.total.rpadi,weather.max,by=c("site.id","year"),all.x=T)
data.rpadi<-merge(data.rpadi,cdl.cut,by=c("site.id","year"),all.x=T)
##rescale variables because the orders of magnitude difference in scales is making glmer.nb angry
data.rpadi<-merge(data.rpadi,coords,by="site.id",all.x=T)
data.scale.rpadi<-scale(data.rpadi[,9:51],center=F,scale=T)
data.scale.rpadi<-cbind(data.rpadi[,1:8],data.scale.rpadi,data.rpadi[,52:53])
#rmaidis
##total aphid abundance
aphid.total.rmaidis<-aggregate(captures~year+site.id,data=aphid.rmaidis,sum)
weather.max<-aggregate(.~site.id+year,data=weather.cast,max)
data.rmaidis<-merge(aphid.total.rmaidis,weather.max,by=c("site.id","year"),all.x=T)
data.rmaidis<-merge(data.rmaidis,cdl.cut,by=c("site.id","year"),all.x=T)
##rescale variables because the orders of magnitude difference in scales is making glmer.nb angry
data.rmaidis<-merge(data.rmaidis,coords,by="site.id",all.x=T)
data.scale.rmaidis<-scale(data.rmaidis[,9:51],center=F,scale=T)
data.scale.rmaidis<-cbind(data.rmaidis[,1:8],data.scale.rmaidis,data.rmaidis[,52:53])
#Models! (The disaggregated ones)
##aglyc
mod.gam2.aglyc<-gam(captures ~ precip.accum+dd.acum+ag10+forest10+s(lat,long)+s(year,bs="re"), data = data.scale.aglyc,family="nb")
summary(mod.gam2.aglyc)
mod.gam3.aglyc<-gam(captures ~ precip.accum+dd.acum+ag_corn10+ag_beans10+ag_smgrains10+forest10+s(lat,long)+s(year,bs="re"), data = data.scale.aglyc,family="nb")
summary(mod.gam3.aglyc)
rsq.partial(mod.gam3.aglyc,type="lr")
#rpadi
mod.gam2.rpadi<-gam(captures ~ precip.accum+dd.acum+ag10+forest10+s(lat,long)+s(year,bs="re"), data = data.scale.rpadi,family="nb")
summary(mod.gam2.rpadi)
mod.gam3.rpadi<-gam(captures ~ precip.accum+dd.acum+ag_corn10+ag_beans10+ag_smgrains10+forest10+s(lat,long)+s(year,bs="re"), data = data.scale.rpadi,family="nb")
summary(mod.gam3.rpadi)
#rmaidis
mod.gam2.rmaidis<-gam(captures ~ precip.accum+dd.acum+ag10+forest10+s(lat,long)+s(year,bs="re"), data = data.scale.rmaidis,family="nb")
summary(mod.gam2.rmaidis)
mod.gam3.rmaidis<-gam(captures ~ precip.accum+dd.acum+ag_corn10+ag_beans10+ag_smgrains10+forest10+s(lat,long)+s(year,bs="re"), data = data.scale.rmaidis,family="nb")
summary(mod.gam3.rmaidis)
rsq.partial(mod.gam3.rmaidis,type="lr")
r2beta(glmer.nb(captures ~ precip.accum+dd.acum+ag_corn10+ag_beans10+ag_smgrains10+forest10+(1|year), data = data.scale.rmaidis))
####Relative abund. by species for Soybean Aphid (Aphis glycines) Cherry-Oat Aphid (Rhopalosiphum padi) Corn Leaf Aphid (Rhopalosiphum maidis)####
data.ratio.aglyc <- merge(data.scale.aglyc,data.scale[,c(1:3)],by = c("site.id","year"),suffixes = c("",".total"))
data.ratio.aglyc$ratio <- data.ratio.aglyc$captures/data.ratio.aglyc$captures.total
data.ratio.rpadi <- merge(data.scale.rpadi,data.scale[,c(1:3)],by = c("site.id","year"),suffixes = c("",".total"))
data.ratio.rpadi$ratio <- data.ratio.rpadi$captures/data.ratio.rpadi$captures.total
data.ratio.rmaidis <- merge(data.scale.rmaidis,data.scale[,c(1:3)],by = c("site.id","year"),suffixes = c("",".total"))
data.ratio.rmaidis$ratio <- data.ratio.rmaidis$captures/data.ratio.rmaidis$captures.total
#Models! (the ratio ones)
##aglyc
mod.gam2.aglyc.ratio<-gam(ratio ~ precip.accum+dd.acum+ag10+forest10+s(lat,long)+s(year,bs="re"), data = data.ratio.aglyc)
summary(mod.gam2.aglyc.ratio)
mod.gam3.aglyc.ratio<-gam(ratio ~ precip.accum+dd.acum+ag_corn+ag_beans+ag_smgrains+forest+s(lat,long)+s(year,bs="re"), data = data.ratio.aglyc)
summary(mod.gam3.aglyc.ratio)
#rpadi
mod.gam2.rpadi.ratio<-gam(ratio ~ precip.accum+dd.acum+ag10+forest10+s(lat,long)+s(year,bs="re"), data = data.ratio.rpadi)
summary(mod.gam2.rpadi.ratio)
mod.gam3.rpadi.ratio<-gam(ratio ~ precip.accum+dd.acum+ag_corn10+ag_beans10+ag_smgrains10+forest10+s(lat,long)+s(year,bs="re"), data = data.ratio.rpadi)
summary(mod.gam3.rpadi.ratio)
#rmaidis
mod.gam2.rmaidis.ratio<-gam(ratio ~ precip.accum+dd.acum+ag10+forest10+s(lat,long)+s(year,bs="re"), data = data.ratio.rmaidis)
summary(mod.gam2.rmaidis.ratio)
mod.gam3.rmaidis.ratio<-gam(ratio ~ precip.accum+dd.acum+ag_corn10+ag_beans10+ag_smgrains10+forest10+s(lat,long)+s(year,bs="re"), data = data.ratio.rmaidis)
summary(mod.gam3.rmaidis.ratio)
# Land Cover Non-linearity/Discontinuity Detection ####
mod.gam.detect <- gam(captures ~ precip.accum+dd.acum+ag_beans10+ag_smgrains10+forest10, data = data.scale,family=nb())
data.scale.detect <- subset(data.scale, !is.na(ag_beans10))
data.scale.detect$resid <- mod.gam.detect$y - mod.gam.detect$fitted.values
plot(data.scale.detect$ag_corn10, data.scale.detect$resid, xlab = "% Landcover Corn w/i 10km", ylab = "Residuals from Restricted Model")
abline(mean(data.scale.detect$resid),mod.gam3$coefficients[4])
mod.gam.detect.aglyc <- gam(captures ~ precip.accum+dd.acum+ag_smgrains+ag_corn+forest, data = data.scale.aglyc,family=nb())
data.scale.detect.aglyc <- subset(data.scale.aglyc, !is.na(ag_beans10))
data.scale.detect.aglyc$resid <- mod.gam.detect.aglyc$y - mod.gam.detect.aglyc$fitted.values
plot(data.scale.detect.aglyc$ag_beans10[abs(data.scale.detect.aglyc$resid)<1500], data.scale.detect.aglyc$resid[abs(data.scale.detect.aglyc$resid)<1500], xlab = "% Landcover soybeans w/i 10km", ylab = "Residuals from Restricted Model")
abline(mean(mod.gam3.aglyc$y - mod.gam3.aglyc$fitted.values + mod.gam3.aglyc$coefficients[5] * mod.gam3.aglyc$model$ag_beans10),mod.gam3.aglyc$coefficients[5], col = "red", lwd = 2)
mod.gam.detect.rpadi <- gam(captures ~ precip.accum+dd.acum+ag_beans10+ag_smgrains10+forest10, data = data.scale.rpadi,family=nb())
data.scale.detect.rpadi <- subset(data.scale.rpadi, !is.na(ag_beans10))
data.scale.detect.rpadi$resid <- mod.gam.detect.rpadi$y - mod.gam.detect.rpadi$fitted.values
plot(data.scale.detect.rpadi$ag_corn10, data.scale.detect.rpadi$resid, xlab = "% Landcover Corn w/i 10km", ylab = "Residuals from Restricted Model")
abline(mean(mod.gam3.rpadi$y - mod.gam3.rpadi$fitted.values + mod.gam3.rpadi$coefficients[4] * mod.gam3.rpadi$model$ag_corn10), mod.gam3.rpadi$coefficients[4])
mod.gam.detect.rmaidis <- gam(captures ~ precip.accum+dd.acum+ag_beans10+ag_smgrains10+forest10, data = data.scale.rmaidis,family=nb())
data.scale.detect.rmaidis <- subset(data.scale.rmaidis, !is.na(ag_beans10))
data.scale.detect.rmaidis$resid <- mod.gam.detect.rmaidis$y - mod.gam.detect.rmaidis$fitted.values
plot(data.scale.detect.rmaidis$ag_corn10, data.scale.detect.rmaidis$resid, xlab = "% Landcover Corn w/i 10km", ylab = "Residuals from Restricted Model")
abline(mean(mod.gam3.rmaidis$y - mod.gam3.rmaidis$fitted.values + mod.gam3.rmaidis$coefficients[4] * mod.gam3.rmaidis$model$ag_corn10), mod.gam3.rmaidis$coefficients[4], col = "red", lwd = 2)
##########################################
cc <- confint(mod1,parm="beta_")
ctab <- as.data.frame(cbind(est=fixef(mod1),cc))
colnames(ctab)<-c("est","lower","upper")
tiff("peak_effects.tiff",width = 4200, height = 4200, units = "px", res = 600)
ggplot(ctab,aes(x=rownames(ctab),y=est))+
geom_point(size=2)+
geom_errorbar(aes(ymin=lower, ymax=upper), width=.2,position=position_dodge(0.1))+
scale_x_discrete(labels=c("Intercept", "Landscape\ncrop cover","Landscape\nforest cover","Precipitation\naccumulation"))+
geom_hline(yintercept=0,linetype="dashed")+
labs(x = "", y = "Effect size")+
ggtitle("Peak aphid abundance")+
coord_flip()+
theme(text = element_text(size=24),axis.text=element_text(color="black"),panel.background=element_blank(),panel.grid.major=element_blank(),panel.grid.minor=element_blank(),axis.line = element_line(size=.7, color="black"),legend.position="none")
dev.off()
dd <- confint(mod2,parm="beta_",method="Wald")
dtab <- as.data.frame(cbind(est=fixef(mod2),dd))
colnames(dtab)<-c("est","lower","upper")
tiff("abund_effects.tiff",width = 4200, height = 4200, units = "px", res = 600)
ggplot(dtab,aes(x=rownames(dtab),y=est))+
geom_point(size=2)+
geom_errorbar(aes(ymin=lower, ymax=upper), width=.2,position=position_dodge(0.1))+
scale_x_discrete(labels=c("Intercept", "Landscape\ncrop cover","Degree day\naccumulation","Landscape\nforest cover","Precipitation\naccumulation"))+
geom_hline(yintercept=0,linetype="dashed")+
labs(x = "", y = "Effect size")+
ggtitle("Total aphid abundance")+
coord_flip()+
theme(text = element_text(size=24),axis.text=element_text(color="black"),panel.background=element_blank(),panel.grid.major=element_blank(),panel.grid.minor=element_blank(),axis.line = element_line(size=.7, color="black"),legend.position="none")
dev.off()
#####Individual species figures###
#These figures aren't 100% correct because the lines are lm fits but the models are negbi
#need to figure out if we can fit a residual model with function from negbi model
mod.gam.aglyc.resid <- gam(captures ~ precip.accum+dd.acum+ag_smgrains10+ag_corn10+forest10, data = data.scale.aglyc,family=nb())
tiff("captures_gly_soy.tiff",width = 4200, height = 4200, units = "px", res = 600)
ggplot(na.omit(data.scale.aglyc), aes(x = ag_beans10, y = resid(mod.gam.aglyc.resid)))+
geom_point(size=2)+
geom_smooth(method="lm",size=2,color="red",se=F)+
#stat_function(fun=function(x)exp(fixef(mod.wheat)[1] + fixef(mod.wheat)[2]*x),size=2,color="red")+
#annotate("text",label="p=0.01",x=.2,y=21000,size=5)+
#annotate("text",label="paste(R ^ 2, \" = 0.27\")",x=.3,y=20000,parse = TRUE,size=5)+
labs(x = "% Landscape soybean cover\nwithin 10k", y = "A.glyc abundance\n(# of captures) residuals")+
theme(text = element_text(size=24),axis.text=element_text(color="black"),panel.background=element_blank(),panel.grid.major=element_blank(),panel.grid.minor=element_blank(),axis.line = element_line(size=.7, color="black"),legend.position="none")
dev.off()
##Madis model
mod.gam.madis.resid <- gam(captures ~ precip.accum+dd.acum+ag_smgrains10+ag_beans10+forest10, data = data.scale.rmaidis,family=nb())
tiff("captures_madis_corn.tiff",width = 4200, height = 4200, units = "px", res = 600)
ggplot(na.omit(data.scale.rmaidis), aes(x = ag_corn10, y = resid(mod.gam.madis.resid)))+
geom_point(size=2)+
geom_smooth(method="lm",size=2,color="red",se=F)+
#stat_function(fun=function(x)exp(fixef(mod.wheat)[1] + fixef(mod.wheat)[2]*x),size=2,color="red")+
#annotate("text",label="p=0.01",x=.2,y=21000,size=5)+
#annotate("text",label="paste(R ^ 2, \" = 0.27\")",x=.3,y=20000,parse = TRUE,size=5)+
labs(x = "% Landscape corn cover\nwithin 10k", y = "R. madis abundance\n(# of captures) residuals")+
theme(text = element_text(size=24),axis.text=element_text(color="black"),panel.background=element_blank(),panel.grid.major=element_blank(),panel.grid.minor=element_blank(),axis.line = element_line(size=.7, color="black"),legend.position="none")
dev.off()
##Padi model
mod.gam.padi.resid <- gam(captures ~ precip.accum+dd.acum+ag_beans10+ag_corn10+forest10, data = data.scale.rpadi,family=nb())
tiff("captures_padi_wheat.tiff",width = 4200, height = 4200, units = "px", res = 600)
ggplot(na.omit(data.scale.rpadi), aes(x = ag_wheat10, y = resid(mod.gam.padi.resid)))+
geom_point(size=2)+
geom_smooth(method="lm",size=2,color="red",se=F)+
#stat_function(fun=function(x)exp(fixef(mod.wheat)[1] + fixef(mod.wheat)[2]*x),size=2,color="red")+
#annotate("text",label="p=0.01",x=.2,y=21000,size=5)+
#annotate("text",label="paste(R ^ 2, \" = 0.27\")",x=.3,y=20000,parse = TRUE,size=5)+
labs(x = "% Landscape wheat cover\nwithin 10k", y = "R. padi abundance\n(# of captures) residuals")+
theme(text = element_text(size=24),axis.text=element_text(color="black"),panel.background=element_blank(),panel.grid.major=element_blank(),panel.grid.minor=element_blank(),axis.line = element_line(size=.7, color="black"),legend.position="none")
dev.off()
####Predicted values plots####
# Pooled species, dd.acum
data.scale <- na.omit(data.scale)
newdata.ddacum <- data.frame(
precip.accum = seq(from = mean(data.scale$precip.accum), to = mean(data.scale$precip.accum), length.out = 100),
dd.acum = seq(from = min(data.scale$dd.acum), to = max(data.scale$dd.acum), length.out = 100),
ag_corn10 = seq(from = mean(data.scale$ag_corn10), to = mean(data.scale$ag_corn10), length.out = 100),
ag_beans10 = seq(from = mean(data.scale$ag_beans10), to = mean(data.scale$ag_beans10), length.out = 100),
ag_smgrains10 = seq(from = mean(data.scale$ag_smgrains10), to = mean(data.scale$ag_smgrains10), length.out = 100),
forest10 = seq(from = mean(data.scale$forest10), to = mean(data.scale$forest10), length.out = 100),
lat = seq(from = mean(data.scale$lat), to = mean(data.scale$lat), length.out = 100),
long = seq(from = mean(data.scale$long), to = mean(data.scale$long), length.out = 100),
year = seq(from = mean(data.scale$year), to = mean(data.scale$year), length.out = 100)
)
newdata.ddacum <- cbind(newdata.ddacum, predict.gam(mod.gam2, newdata.ddacum, se.fit = T))
newdata.ddacum <- within(newdata.ddacum, {
TotalCaptures <- exp(fit)
LL <- exp(fit - 1.96 * se.fit)
UL <- exp(fit + 1.96 * se.fit)
})
newdata.ddacum$dd.acum <- newdata.ddacum$dd.acum*(sqrt(mean(data2$dd.acum^2))) #De-scaling
ggplot(newdata.ddacum, aes(dd.acum, TotalCaptures)) +
geom_ribbon(aes(ymin = LL, ymax = UL), alpha = .25) +
#geom_point(data = na.omit(data.total), aes(x = dd.acum, y = captures), size = 2) +
geom_line(size = 2) +
#scale_y_continuous(trans = 'log2') +
labs(x = "Seasonal Degree Day Accumulation", y = "Predicted Total Seasonal Captures") +
theme(text = element_text(size=24),axis.text=element_text(color="black"),panel.background=element_blank(),panel.grid.major=element_blank(),panel.grid.minor=element_blank(),axis.line = element_line(size=.7, color="black"),legend.position="none")
ggsave("fig3-2.tiff", height = 7, width = 7, units = "in", device = "tiff")
# Pooled species, precip.accum
data.scale <- na.omit(data.scale)
newdata.precip <- data.frame(
precip.accum = seq(from = min(data.scale$precip.accum), to = max(data.scale$precip.accum), length.out = 100),
dd.acum = seq(from = mean(data.scale$dd.acum), to = mean(data.scale$dd.acum), length.out = 100),
ag_corn10 = seq(from = mean(data.scale$ag_corn10), to = mean(data.scale$ag_corn10), length.out = 100),
ag_beans10 = seq(from = mean(data.scale$ag_beans10), to = mean(data.scale$ag_beans10), length.out = 100),
ag_smgrains10 = seq(from = mean(data.scale$ag_smgrains10), to = mean(data.scale$ag_smgrains10), length.out = 100),
forest10 = seq(from = mean(data.scale$forest10), to = mean(data.scale$forest10), length.out = 100),
lat = seq(from = mean(data.scale$lat), to = mean(data.scale$lat), length.out = 100),
long = seq(from = mean(data.scale$long), to = mean(data.scale$long), length.out = 100),
year = seq(from = mean(data.scale$year), to = mean(data.scale$year), length.out = 100)
)
newdata.precip <- cbind(newdata.precip, predict.gam(mod.gam2, newdata.precip, se.fit = T))
newdata.precip <- within(newdata.precip, {
TotalCaptures <- exp(fit)
LL <- exp(fit - 1.96 * se.fit)
UL <- exp(fit + 1.96 * se.fit)
})
newdata.precip$precip.accum <- newdata.precip$precip.accum*(sqrt(mean(data2$precip.accum^2)))
ggplot(newdata.precip, aes(precip.accum, TotalCaptures)) +
geom_ribbon(aes(ymin = LL, ymax = UL), alpha = .25) +
#geom_point(data = na.omit(data.total), aes(x = precip.accum, y = captures),size = 2) +
geom_line(size = 2) +
#scale_y_continuous(trans = 'log2') +
labs(x = "Seasonal Precipitation Accumulation (mm)", y = "Predicted Total Seasonal Captures") +
theme(text = element_text(size=24),axis.text=element_text(color="black"),panel.background=element_blank(),panel.grid.major=element_blank(),panel.grid.minor=element_blank(),axis.line = element_line(size=.7, color="black"),legend.position="none")
ggsave("fig4-2.tiff", height = 7, width = 8, units = "in", device = "tiff")
# Disagg species, corn
data.scale <- na.omit(data.scale)
newdata.corn <- data.frame(
precip.accum = seq(from = mean(data.scale$precip.accum), to = mean(data.scale$precip.accum), length.out = 100),
dd.acum = seq(from = mean(data.scale$dd.acum), to = mean(data.scale$dd.acum), length.out = 100),
ag_corn10 = seq(from = min(data.scale$ag_corn10), to = max(data.scale$ag_corn10), length.out = 100),
ag_beans10 = seq(from = mean(data.scale$ag_beans10), to = mean(data.scale$ag_beans10), length.out = 100),
ag_smgrains10 = seq(from = mean(data.scale$ag_smgrains10), to = mean(data.scale$ag_smgrains10), length.out = 100),
forest10 = seq(from = mean(data.scale$forest10), to = mean(data.scale$forest10), length.out = 100),
lat = seq(from = mean(data.scale$lat), to = mean(data.scale$lat), length.out = 100),
long = seq(from = mean(data.scale$long), to = mean(data.scale$long), length.out = 100),
year = seq(from = mean(data.scale$year), to = mean(data.scale$year), length.out = 100)
)
newdata.corn <- rbind(newdata.corn, newdata.corn, newdata.corn)
newdata.corn$Species <- as.factor(c(rep("A. glycines", 100), rep("R. padi", 100), rep("R. maidis", 100)))
predict.corn <- as.data.frame(predict.gam(mod.gam3.aglyc, newdata.corn[1:100,], se.fit = T))
predict.corn <- rbind(predict.corn, as.data.frame(predict.gam(mod.gam3.rpadi, newdata.corn[101:200,], se.fit = T)))
predict.corn <- rbind(predict.corn, as.data.frame(predict.gam(mod.gam3.rmaidis, newdata.corn[201:300,], se.fit = T)))
newdata.corn <- cbind(newdata.corn, predict.corn)
newdata.corn <- within(newdata.corn, {
TotalCaptures <- exp(fit)
LL <- exp(fit - 1.96 * se.fit)
UL <- exp(fit + 1.96 * se.fit)
})
newdata.corn$ag_corn10 <- newdata.corn$ag_corn10*(sqrt(mean(na.omit(data2)$ag_corn10^2)))
ggplot(newdata.corn[newdata.corn$Species!="R. padi",], aes(ag_corn10, TotalCaptures)) +
#geom_point(data = na.omit(data.total.aglyc), aes(x = ag_corn10, y = captures), color = "#F8766D" ,size = 2) +
#geom_point(data = na.omit(data.total.rmaidis), aes(x = ag_corn10, y = captures), color = "#00BFC4" ,size = 2) +
geom_ribbon(aes(ymin = LL, ymax = UL, fill = Species), alpha = .25) +
geom_line(aes(colour = Species), size = 2) +
#scale_y_continuous(trans = 'log2') +
labs(x = "% Land Cover Corn w/i 10km Radius", y = "Predicted Total Seasonal Captures") +
theme(text = element_text(size=24),axis.text=element_text(color="black"),panel.background=element_blank(),panel.grid.major=element_blank(),panel.grid.minor=element_blank(),axis.line = element_line(size=.7, color="black"),
legend.text = element_text(face = "italic"))
ggsave("fig5-2.tiff", height = 7, width = 7, units = "in", device = "tiff")
# Disagg species, beans
data.scale <- na.omit(data.scale)
newdata.beans <- data.frame(
precip.accum = seq(from = mean(data.scale$precip.accum), to = mean(data.scale$precip.accum), length.out = 100),
dd.acum = seq(from = mean(data.scale$dd.acum), to = mean(data.scale$dd.acum), length.out = 100),
ag_corn10 = seq(from = mean(data.scale$ag_corn10), to = mean(data.scale$ag_corn10), length.out = 100),
ag_beans10 = seq(from = min(data.scale$ag_beans10), to = max(data.scale$ag_beans10), length.out = 100),
ag_smgrains10 = seq(from = mean(data.scale$ag_smgrains10), to = mean(data.scale$ag_smgrains10), length.out = 100),
forest10 = seq(from = mean(data.scale$forest10), to = mean(data.scale$forest10), length.out = 100),
lat = seq(from = mean(data.scale$lat), to = mean(data.scale$lat), length.out = 100),
long = seq(from = mean(data.scale$long), to = mean(data.scale$long), length.out = 100),
year = seq(from = mean(data.scale$year), to = mean(data.scale$year), length.out = 100)
)
newdata.beans <- rbind(newdata.beans, newdata.beans, newdata.beans)
newdata.beans$Species <- as.factor(c(rep("A. glycines", 100), rep("R. padi", 100), rep("R. maidis", 100)))
predict.beans <- as.data.frame(predict.gam(mod.gam3.aglyc, newdata.beans[1:100,], se.fit = T))
predict.beans <- rbind(predict.beans, as.data.frame(predict.gam(mod.gam3.rpadi, newdata.beans[101:200,], se.fit = T)))
predict.beans <- rbind(predict.beans, as.data.frame(predict.gam(mod.gam3.rmaidis, newdata.beans[201:300,], se.fit = T)))
newdata.beans <- cbind(newdata.beans, predict.beans)
newdata.beans <- within(newdata.beans, {
TotalCaptures <- exp(fit)
LL <- exp(fit - 1.96 * se.fit)
UL <- exp(fit + 1.96 * se.fit)
})
newdata.beans$ag_beans10 <- newdata.beans$ag_beans10*(sqrt(mean(na.omit(data2)$ag_beans10^2)))
ggplot(newdata.beans[newdata.beans$Species!= "R. padi",], aes(ag_beans10, TotalCaptures)) +
#geom_point(data = na.omit(data.total.aglyc), aes(x = ag_beans10, y = captures), color = "#F8766D", size = 2) +
#geom_point(data = na.omit(data.total.rmaidis), aes(x = ag_beans10, y = captures), color = "#00BFC4", size = 2) +
geom_ribbon(aes(ymin = LL, ymax = UL, fill = Species), alpha = .25) +
geom_line(aes(colour = Species), size = 2) +
#scale_y_continuous(trans = 'log2') +
labs(x = "% Land Cover Soybeans w/i 10km Radius", y = "Predicted Total Seasonal Captures") +
theme(text = element_text(size=24),axis.text=element_text(color="black"),panel.background=element_blank(),panel.grid.major=element_blank(),panel.grid.minor=element_blank(),axis.line = element_line(size=.7, color="black"),
legend.text = element_text(face = "italic"))
ggsave("fig6-2.tiff", height = 7, width = 8, units = "in", device = "tiff")