-
Notifications
You must be signed in to change notification settings - Fork 0
/
INSECTS_analysis.R
275 lines (229 loc) · 10.8 KB
/
INSECTS_analysis.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
### INSECTS ANALYSIS
### Programmer: Kevin Wolz
### Originally Created: 20 Nov 2014
### Last Updated: 20 Nov 2014
if(ANALYSIS){
##### SPECIES RICHNESS #####
sp.rich = subset(alpha.pitfall, samples==36)
sp.rich$S.est.84CI.lb = sp.rich$S.est + sp.rich$S.est.sd * 1.372
sp.rich$S.est.84CI.ub = sp.rich$S.est - sp.rich$S.est.sd * 1.372
sp.rich.range = with(sp.rich, range(S.est, S.est.84CI.ub, S.est.84CI.lb))
abun.range = with(sp.rich, range(indiv.comp))
abun.range = c(floor(abun.range[1]), ceiling(abun.range[2]))
## PLOT SPECIES RICHNESS vs. MONTH by SYSTEM
pdf(paste(outputPlotPath, "Species_Richness_", yr, ".pdf",sep=""))
par(mar=c(5.1,5.1,4.1,2.1)) #bottom, left, top, right margins
add = FALSE
for (i in unique(sp.rich$system)){
data = subset(sp.rich, system==i)
attach(data)
plotCI(month, S.est,
(S.est.sd * 1.372),
#ui = S.est.84CI.ub,
#li = S.est.84CI.lb,
col = system,
ylim = sp.rich.range,
pch = 19, cex.main = 1.6, cex.lab = 1.4,
main = paste("Species Richness ", yr, sep = ""),
xlab = "Month",
ylab = "Species Richness",
add = add)
add = TRUE
lines(month, S.est, col = system, lty = 1)
abline(h=mean(S.est), col = system, lwd = 2, lty = 2)
detach(data)
}
legend("topleft", c("WPP (84% CI)", "CSR (84% CI)", "WPP Monthly Mean", "CSR Monthly Mean"), col = c(2,1,2,1), pch = c(19,19,NA,NA), lty = c(NA, NA, 2, 2), lwd = c(NA, NA, 2, 2))
dev.off()
## PLOT ABUNDANCE vs. MONTH by SYSTEM
pdf(paste(outputPlotPath, "Abundance_", yr, ".pdf",sep=""))
par(mar=c(5.1,5.1,4.1,2.1)) #bottom, left, top, right margins
plot(sp.rich$month, sp.rich$indiv.comp, type="n",
ylim = abun.range,
cex.main = 1.6, cex.lab = 1.4,
main = paste("Insect Abundance ", yr, sep = ""),
xlab = "Month",
ylab = "Number of Individuals")
for (i in unique(sp.rich$system)){
data = subset(sp.rich, system==i)
attach(data)
lines(month, indiv.comp,
type = "b",
col = system,
pch = 19)
abline(h=mean(indiv.comp), col = system, lwd = 2, lty = 2)
detach(data)
}
legend("topright", c("WPP", "CSR", "WPP Monthly Mean", "CSR Monthly Mean"), col = c(2,1,2,1), pch = c(19,19,NA,NA), lty = c(NA, NA, 2, 2), lwd = c(NA, NA, 2, 2))
dev.off()
##### ALPHA DIVERSITY #####
alpha = subset(alpha.pitfall, samples==16)
alpha.indices = c("ACE", "ICE", "chao1", "chao2", "jack1", "jack2", "bootstrap", "alpha", "shannon", "shannon.exp", "simpson.inv")
#alpha$S.est.84CI.lb = sp.rich$S.est + sp.rich$S.est.sd * 1.372
#lpha$S.est.84CI.ub = sp.rich$S.est - sp.rich$S.est.sd * 1.372
## PLOT ALPHA DIVERSITY vs. MONTH by SYSTEM
for(index in alpha.indices){
MEAN = paste(index, ".mean", sep = "")
SD = paste(index, ".sd", sep = "")
INDEX = cbind(alpha[, c(1:8)], alpha[, c(MEAN, SD)])
names(INDEX)[9:10] = c("avg", "sd")
alpha.range = range((INDEX$avg+INDEX$sd), (INDEX$avg-INDEX$sd))
pdf(paste(outputPlotPath, "ALPHA_DIVERISTY_", index, "_", yr, ".pdf",sep=""))
par(mar=c(5.1,5.1,4.1,2.1)) #bottom, left, top, right margins
add = FALSE
for (i in unique(INDEX$system)){
data = subset(INDEX, system==i)
attach(data)
plotCI(month, avg,
sd,
col = system,
ylim = alpha.range,
pch = 19, cex.main = 1.6, cex.lab = 1.4,
main = paste(index, yr, sep = " "),
xlab = "Month",
ylab = paste(index),
add = add)
add = TRUE
lines(month, avg, col = system, lty = 1)
abline(h=mean(avg), col = system, lwd = 2, lty = 2)
detach(data)
}
legend("topleft", c("WPP (sd)", "CSR (sd)", "WPP Monthly Mean", "CSR Monthly Mean"), col = c(2,1,2,1), pch = c(19,19,NA,NA), lty = c(NA, NA, 2, 2), lwd = c(NA, NA, 2, 2))
dev.off()
}
##### BEA DIVERSITY #####
beta = beta.pitfall
#beta = subset(beta, shared.species.obs > 0)
beta.indices = c("jaccard", "sorensen", "chao.jaccard.est", "chao.sorensen.est", "morisita.horn", "bray.curtis")
beta.summary = summaryBy(list(beta.indices, c("system", "year", "month")), data = beta, FUN = c(mean, sd))
## PLOT BETA DIVERSITY vs. MONTH by SYSTEM
for(index in beta.indices){
MEAN = paste(index, ".mean", sep = "")
SD = paste(index, ".sd", sep = "")
INDEX = cbind(beta.summary[, c(1:3)], beta.summary[, c(MEAN, SD)])
names(INDEX)[4:5] = c("avg", "sd")
beta.range = range((INDEX$avg+INDEX$sd), (INDEX$avg-INDEX$sd))
pdf(paste(outputPlotPath, "BETA_DIVERISTY_", index, "_", yr, ".pdf", sep=""))
par(mar=c(5.1,5.1,4.1,2.1)) #bottom, left, top, right margins
add = FALSE
for (i in unique(INDEX$system)){
data = subset(INDEX, system==i)
attach(data)
plotCI(month, avg,
sd,
col = system,
ylim = beta.range,
pch = 19, cex.main = 1.6, cex.lab = 1.4,
main = paste(index, yr, sep = " "),
xlab = "Month",
ylab = paste(index),
add = add)
add = TRUE
lines(month, avg, col = system, lty = 1)
abline(h=mean(avg), col = system, lwd = 2, lty = 2)
detach(data)
}
legend("topleft", c("WPP (sd)", "CSR (sd)", "WPP Monthly Mean", "CSR Monthly Mean"), col = c(2,1,2,1), pch = c(19,19,NA,NA), lty = c(NA, NA, 2, 2), lwd = c(NA, NA, 2, 2))
dev.off()
}
##### COUNT DATA #####
INSECTS = names(count.pitfall)[15:ncol(count.pitfall)]
count.summary = summaryBy(list(c(INSECTS), c("year", "month", "system")), data=count.pitfall, FUN = sum, na.rm = TRUE, keep.names = TRUE)
## PLOT SPECIES ABUNDANCE THROUGH TIME by SYSTEM
for(insect in INSECTS){
yrange = c(0,ifelse(max(count.summary[,insect])>0,max(count.summary[,insect]),1))
pdf(paste(outputCountPlotPath, insect, ".pdf",sep=""))
par(mar=c(5.1,5.1,4.1,2.1)) #bottom, left, top, right margins
plot(count.summary$month, count.summary[,insect], type="n",
ylim = yrange,
cex.main = 1.6, cex.lab = 1.4,
main = paste(insect),
xlab = "Month",
ylab = "Number of Individuals")
for(season in unique(count.summary$year)){
for(sys in unique(count.summary$system)){
year.count.summary = subset(count.summary, year==season & system==sys)
lines(year.count.summary$month, year.count.summary[,insect],
type="b",
pch = 19,
col = year.count.summary$system,
lty = year.count.summary$year-2012)
}
}
legend("topleft", c("WPP 2013", "CSR 2013", "WPP 2014", "CSR 2014"), col = c(2,1,2,1), pch = c(19,19,19,19), lty = c(1, 1, 2, 2), lwd = c(1, 1, 1, 1))
dev.off()
}
##### ABUNDANCE ANALYSIS #####
#count.summary$n.indivs = rowSums(count.summary[,4:ncol(count.summary)])
count.sys.summary = summaryBy(list(c(INSECTS), c("system")), data=count.summary, FUN = sum, na.rm = TRUE, keep.names = TRUE)
abundance = data.frame(species = names(count.sys.summary[,2:ncol(count.sys.summary)]), WPP = as.integer(count.sys.summary[2,2:ncol(count.sys.summary)]), CSR = as.integer(count.sys.summary[1,2:ncol(count.sys.summary)]))
abundance$WPP.minus.CSR = abundance$WPP - abundance$CSR
abundance$WPP.div.CSR = abundance$WPP / abundance$CSR
abundance$WPP.perc.CSR = abundance$WPP.minus.CSR / abundance$CSR
abundance$WPP.div.CSR[which(abundance$CSR==0)] = NA
abundance$WPP.perc.CSR[which(abundance$CSR==0)] = NA
abundance$WPPrel = abundance$WPP/36
abundance$CSRrel = abundance$CSR/16
abundance$WPPrel.minus.CSRrel = abundance$WPPrel - abundance$CSRrel
abundance$WPPrel.div.CSRrel = abundance$WPPrel / abundance$CSRrel
abundance$WPPrel.perc.CSRrel = abundance$WPPrel.minus.CSRrel / abundance$CSRrel
abundance$WPPrel.div.CSRrel[which(abundance$CSRrel==0)] = NA
abundance$WPPrel.perc.CSRrel[which(abundance$CSRrel==0)] = NA
write.table(abundance, paste(outputDataPath, "Abundance_Comparison.csv", sep=""), row.names = FALSE, sep = ",")
##### COMMUNITY OVERLAP ANALYSIS #####
incidence.summary = count.summary
incidence.summary[4:ncol(incidence.summary)] = as.numeric(count.summary[4:ncol(count.summary)] > 0)
incidence.summary[which(incidence.summary$system=="WPP"),4:ncol(incidence.summary)] = incidence.summary[which(incidence.summary$system=="WPP"),4:ncol(incidence.summary)] * 10
incidence.overlap = summaryBy(list(c(INSECTS), c("year", "month")), data=incidence.summary, FUN = sum, na.rm = TRUE, keep.names = TRUE)
cats = incidence.overlap[,1:2]
incidence.overlap = sapply(incidence.overlap, as.factor)
incendence.table = apply(as.matrix(incidence.overlap[,3:ncol(incidence.overlap)]), 1, table)
ios = cats
ios = rbind(ios,ios,ios)
ios$metric = rep(c("WPP.only", "CSR.only", "both"), each = nrow(cats))
ios$num = c(incendence.table[3,], incendence.table[2,], incendence.table[4,])
ios.tots = summaryBy(num ~ year + month, data = ios, FUN = sum)
ios.rel = ios
for(i in 1:nrow(ios.rel)){
ios.rel$num[i] = ios.rel$num[i] / ios.tots$num.sum[which(ios.tots$year == ios.rel$year[i] & ios.tots$month == ios.rel$month[i] )] * 100
}
pdf(paste(outputPlotPath, "Community_Overlap.pdf",sep=""))
barchart( num ~ as.factor(month) | as.factor(year),
data=ios,
groups=metric,
stack=T,
horizontal=F,
auto.key=T,
main = list("Community Overlap", cex = 2.2),
xlab = list("Month", cex = 2),
ylab = list("Number of Species", cex = 2),
scales = list(cex = 1.05),
par.strip.text = list(cex = 1.2),
col = c("darkred","black","red"),
aspect = 1/2,
par.settings = list(strip.background=list(col="lightgrey")),
key = list(space = "top",
rectangles = list(col=c("darkred","black","red")),
text = list(c("BOTH", "CSR ONLY", "WPP ONLY"))))
dev.off()
pdf(paste(outputPlotPath, "Community_Overlap_Perc.pdf",sep=""))
barchart( num ~ as.factor(month) | as.factor(year),
data=ios.rel,
groups=metric,
stack=T,
horizontal=F,
auto.key=T,
main = list("Community Overlap", cex = 2.2),
xlab = list("Month", cex = 2),
ylab = list("Number of Species (%)", cex = 2),
scales = list(cex = 1.05),
par.strip.text = list(cex = 1.2),
col = c("darkred","black","red"),
aspect = 1/2,
par.settings = list(strip.background=list(col="lightgrey")),
key = list(space = "top",
rectangles = list(col=c("darkred","black","red")),
text = list(c("BOTH", "CSR ONLY", "WPP ONLY"))))
dev.off()
#plot(ios.rel$num[which(ios.rel$metric=="both")], type = "b")
}