-
Notifications
You must be signed in to change notification settings - Fork 0
/
IMR_acoustic_survey.R
406 lines (371 loc) · 11.3 KB
/
IMR_acoustic_survey.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
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
#
# Capelin Acoustic Transect Analysis
# Original author: Pat Sullivan (AllData.R)
# Modified by Janelle Morano
## Working directory: Github repository "BarentsSea"
#
#setwd(""/Users/janellemorano/Documents/Github/BarentsSea")
## Read in data
data = read.csv("AllData.csv", head = TRUE)
head(data)
dim(data)
(iyear = unique(data$year))
#zero-group fish are immature capelin
library(dplyr)
library(ggplot2)
hist(data$capelin)
hist(data$cod)
plot(data$capelin, data$cod)
qqnorm(data$capelin)
qqnorm(data$cod)
#plot(byyear$year, byyear$capelin)
## AVERAGE species by year
#average COD by year
#tapply(cod, year, mean)
ave.cod<-tapply(data$cod, data$year, mean)
#average CAPELIN by year
ave.capelin<-tapply(data$capelin, data$year, mean)
#average POLAR COD by year
#tapply(polar_cod, year, mean)
ave.polar_cod<-tapply(data$polar_cod, data$year, mean)
#average HERRING by year
ave.herring<-tapply(data$herring, data$year, mean)
#average SAITHE by year
ave.saithe<-tapply(data$saithe, data$year, mean)
#average REDFISH by year
ave.redfish<-tapply(data$redfish, data$year, mean)
#average HADDOCK_COD by year
ave.haddock_cod<-tapply(data$haddock_cod, data$year, mean)
#average HADDOCK by year
ave.haddock<-tapply(data$haddock, data$year, mean)
#average BLUE WHITING by year
ave.blue_whiting<-tapply(data$blue_whiting, data$year, mean)
#average NORWAY POUT by year
ave.norway_pout<-tapply(data$Norway_pout, data$year, mean)
#average PLANKTON by year
#no data for plankton
#ave.plankton<-tapply(data$plankton, data$year, mean)
#average ZERO GROUP FISH by year
ave.zero_group_fish<-tapply(data$zero_group_fish, data$year, mean)
## Simple graph of all of the species
plot(iyear, ave.capelin,type = "o", lty = 1, col = "blue")
lines(iyear, ave.cod, type = "o", lty = 1, col = "darkgreen")
lines(iyear, ave.herring, type = "o", lty = 1, col = "lightblue")
lines(iyear, ave.haddock, type = "o", lty = 1, col = "green")
#lines(iyear, ave.redfish, type = "o", lty = 1, col = "red")
lines(iyear, ave.polar_cod, type = "o", lty = 1, col = "purple")
#lines(iyear, ave.saithe, type = "o", lty = 1, col = "red")
#lines(iyear, ave.haddock_cod, type = "o", lty = 1, col = "red")
#lines(iyear, ave.blue_whiting, type = "o", lty = 1, col = "red")
#lines(iyear, ave.norway_pout, type = "o", lty = 1, col = "red")
lines(iyear, ave.zero_group_fish, type = "o", lty = 1, col = "red")
legend("topleft", legend=c("capelin", "cod", "herring", "haddock", "polar cod", 'zero group fish'),
col=c("blue", "darkgreen", "lightblue", "green", "purple", "red"), lty=1, cex=1)
## Plot with ggplot2
## Not working...
#
# data %>%
# group_by(year, month) %>%
# summarise(year, month)
# #Average catch of capelin and cod
# byyear <- data %>%
# group_by(year) %>%
# summarise(
# capelin = mean(capelin, na.rm = TRUE),
# cod = mean(cod, na.rm = TRUE),
# )
#
# theme_set(theme_classic())
# ggplot(byyear, aes(year, cod))
#
# select(data, year, capelin, cod)
# group_by(data, year)
# summarise(year, delay = mean(dep_delay, na.rm = TRUE))
#ggplot(capelin, aes(year, avg)+geom_bar)
#ggplot(data=capelin, aes(x=year, y=avg) + theme_bw()
# geom_bar()
## Plot survey locations
#
plot(lat~lon,data=data)
#
# Plot relative capelin densities
#
yy = 2013
for(yy in iyear){
datai = data[data$year==yy,]
plot(lat~lon,data=datai,cex=4*datai$capelin/max(datai$capelin),lwd=2)
title(paste("Capelin Abundance",yy))
locator(1)
}
#
# Plot relative cod densities
#
yy = 2013
for(yy in iyear){
datai2 = data[data$year==yy,]
plot(lat~lon,data=datai2,cex=4*datai$cod/max(datai$cod),lwd=2)
title(paste("Cod Abundance",yy))
locator(1)
}
#
##############################################################
#
# Geostatistics
#
#
library(sp)
#
# Read the data into R
#
data$Xloc = data$lon
data$Yloc = data$lat
coordinates(data)=c("Xloc","Yloc")
#
# Do some simple exploratory analysis of the data
#
hist(data$capelin)
stem(data$capelin)
#
# Plot the sample locations
#
par(pty="s")
plot(Yloc~Xloc,data=data)
title("Capelin Locations")
#
#
library(RColorBrewer)
library(classInt)
#
## Examine the capelin concentrations over the area
#
# Press ESC to move through the years.
# Crosshairs allow you to select a point, but not sure why
# (The color of the point corresponds to the
# Concentration of cadmium, as specified in the q5Colors object)
#
yy = 2013
for(yy in iyear){
datai = data[data$year==yy,]
pal = brewer.pal(5,"Blues")
q5 = classIntervals(datai$capelin, n=5, style="quantile")
q5Colors = findColours(q5,pal)
plot(c(min(datai$Xloc),max(datai$Xloc)),
c(min(datai$Yloc),max(datai$Yloc)),
xlab="Longitude",ylab="Latitude",type="n")
plot(datai,col=q5Colors,pch=19,add=T)
legend("bottomright",fill=attr(q5Colors,"palette"),
legend = names(attr(q5Colors,"table")),bty="n")
title(paste("Capelin Abundance Over Area",yy))
locator(1)
}
#
##Examine the cod concentrations over the area
yy = 2013
for(yy in iyear){
datai2 = data[data$year==yy,]
pal2 = brewer.pal(5,"Greens")
q52 = classIntervals(datai$cod, n=5, style="quantile")
q5Colors2 = findColours(q5,pal2)
plot(c(min(datai2$Xloc),max(datai2$Xloc)),
c(min(datai2$Yloc),max(datai2$Yloc)),
xlab="Longitude",ylab="Latitude",type="n")
plot(datai2,col=q5Colors2,pch=19,add=T)
legend("bottomright",fill=attr(q5Colors2,"palette"),
legend = names(attr(q5Colors2,"table")),bty="n")
title(paste("Cod Abundance Over Area",yy))
locator(1)
}
#
##Examine the cod and capeline concentrations next to each other over the area
#
par(mfrow=c(1,2))
plot(c(min(datai$Xloc),max(datai$Xloc)),
c(min(datai$Yloc),max(datai$Yloc)),
xlab="Longitude",ylab="Latitude",type="n")
plot(datai,col=q5Colors,pch=19,add=T)
title(paste("Capelin Abundance Over Area",yy))
plot(c(min(datai2$Xloc),max(datai2$Xloc)),
c(min(datai2$Yloc),max(datai2$Yloc)),
xlab="Longitude",ylab="Latitude",type="n")
plot(datai2,col=q5Colors2,pch=19,add=T)
title(paste("Cod Abundance Over Area",yy))
# Option:
# layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE))
yy = 2012
datai2 = data[data$year==yy,]
pal = brewer.pal(5,"Greens")
q5 = classIntervals(datai$cod, n=5, style="quantile")
q5Colors = findColours(q5,pal)
plot(c(min(datai2$Xloc),max(datai2$Xloc)),
c(min(datai2$Yloc),max(datai2$Yloc)),
xlab="Longitude",ylab="Latitude",type="n")
plot(datai2,col=q5Colors,pch=19,add=T)
legend("bottomright",fill=attr(q5Colors,"palette"),
legend = names(attr(q5Colors,"table")),bty="n")
title(paste("Cod Abundance Over Area",yy))
locator(1)
}
################################################################################
#
# Kriging Capeline
#
library(gstat)
#
# Choose Year ##########################
#
yy = 2004
datai = data.frame(data[data$year==yy,])
datai$Xloc = jitter(datai$Xloc)
datai$Yloc = jitter(datai$Yloc)
coordinates(datai) = c("Xloc","Yloc")
#
par(mfrow=c(1,2))
hist(datai$capelin,main="Capelin",col="dodgerblue")
hist(log(datai$capelin+1),main="Log(Capelin+1)",col="dodgerblue")
par(mfrow=c(1,1))
#
#
# Plot relative abundances
#
plot(lat~lon,data=datai,cex=4*datai$capelin/max(datai$capelin),lwd=2)
title(paste("Capelin Abundance",yy))
#
#
# Plot relative abundances using color
#
library(RColorBrewer)
library(classInt)
#
# Plot a point at a location
# The color of the point corresponds to the
# Concentration of cadmium, as specified in the q5Colors object
#
pal = brewer.pal(5,"Blues")
q5 = classIntervals(datai$capelin, n=5, style="quantile")
q5Colors = findColours(q5,pal)
plot(c(min(datai$Xloc),max(datai$Xloc)),
c(min(datai$Yloc),max(datai$Yloc)),
xlab="Longitude",ylab="Latitude",type="n")
plot(datai,col=q5Colors,pch=19,add=T)
legend("bottomright",fill=attr(q5Colors,"palette"),
legend = names(attr(q5Colors,"table")),bty="n")
title(paste("Capelin Abundance Over Area",yy))
#
# Calculate the empirical variogram
#
capelin.vario = variogram(log(capelin+1)~1,datai,cutoff=20)
#
# Plot the empirical variogram
#
plot(gamma~dist,capelin.vario,
ylim=c(0,max(gamma)),type='n',
xlab="Distance",ylab="Semivariance",
main=paste("Capelin Variogram",yy))
points(gamma~dist,capelin.vario,cex=2*np/max(np),pch=16,col="lightblue")
#
# Fit the model first by eye
#
my.range = locator(1)$x
my.nugget = locator(1)$y
my.psill = locator(1)$y-my.nugget
#
capelin.eye = vgm(model="Sph",psill=my.psill,range=my.range,nugget=my.nugget)
plot(gamma~dist,capelin.vario,
ylim=c(0,max(gamma)),type='n',
xlab="Distance",ylab="Semivariance",
main=paste("Capelin Variogram",yy))
points(gamma~dist,capelin.vario,cex=2*np/max(np),pch=16,col="lightblue")
vgmline = variogramLine(capelin.eye,max(capelin.vario$dist))
lines(gamma~dist,vgmline,lwd=2)
#
# Now use eye parameters to start fit of model
#
#
capelin.fit=fit.variogram(capelin.vario,
vgm(model="Sph",psill=my.psill,range=my.range,nugget=my.nugget),
fit.method=1)
#
# Look at estimates
#
capelin.fit
capelin.psill=capelin.fit$psill[2]
capelin.range=capelin.fit$range[2]
capelin.nugget=capelin.fit$psill[1]
#
# Plot data, model and parameter estimates
#
#
plot(gamma~dist,capelin.vario,
ylim=c(0,max(gamma)),type='n',
xlab="Distance",ylab="Semivariance",
main=paste("Capelin Variogram",yy))
points(gamma~dist,capelin.vario,cex=2*np/max(np),pch=16,col="lightblue")
vgmline = variogramLine(capelin.fit,max(capelin.vario$dist))
lines(gamma~dist,vgmline,lwd=2)
#
legend("bottomright",legend = c(
paste("Psill =",round(capelin.psill,2)),
paste("Range =",round(capelin.range,2)),
paste("Nugget = ",round(capelin.nugget,2))),
bty="n")
#
#
# Create a grid of points to predict over
#
library(geoR)
#
capelin.grid = expand.grid(
Xloc=seq(min(datai$Xloc),max(datai$Xloc),length=50),
Yloc=seq(min(datai$Yloc),max(datai$Yloc),length=50))
names(capelin.grid)=c("Xloc","Yloc")
coordinates(capelin.grid)=c("Xloc","Yloc")
capelin.grid = as(capelin.grid, "SpatialPixels")
#
# Now plot the data and overlay the prediction grid
#
plot(Yloc~Xloc,capelin.grid,cex=1.2,pch='+',col="green")
points(Yloc~Xloc,datai,pch=".")
#
#
# Predict the value at all the points in the domain
#
date()
capelin.ok = krige(log(capelin+1)~1, datai, capelin.grid, capelin.fit)
date()
#
# Plot the prediction
#
plot(c(min(datai$Xloc),max(datai$Xloc)),
c(min(datai$Yloc),max(datai$Yloc)),
type="n",xlab="Longitude",ylab="Latitude")
image(capelin.ok["var1.pred"],col=heat.colors(4),add=T)
#contour(capelin.ok["var1.pred"],add=T)
title(paste("Predicted Log(Capelin+1)",yy))
legend("bottomright",legend=c(0,1,2,3),fill=heat.colors(4),
bty="n",title="log(Capelin+1)")
#
# Plot variance
#
plot(c(min(datai$Xloc),max(datai$Xloc)),
c(min(datai$Yloc),max(datai$Yloc)),
type="n",xlab="Longitude",ylab="Latitude")
image(capelin.ok["var1.var"],col=heat.colors(4),add=T)
points(datai$Xloc,datai$Yloc,pch=".")
legend("bottomright",legend=round(seq(.3,.5,length=4),2),fill=heat.colors(4),
bty="n",title="Variance")
# points(data.frame(capelin.grid)$Xloc,data.frame(capelin.grid)$Yloc,pch="+")
title(paste("Variance in Predictions",yy))
#
#
# Overlay Cod on Capelin Predictions
#
plot(c(min(datai$Xloc),max(datai$Xloc)),
c(min(datai$Yloc),max(datai$Yloc)),
type="n",xlab="Longitude",ylab="Latitude")
image(capelin.ok["var1.pred"],col=heat.colors(4),add=T)
#contour(capelin.ok["var1.pred"],add=T)
title(paste("Predicted Log(Capelin+1) \n Overlayed with Cod",yy))
legend("bottomright",legend=c(0,1,2,3),fill=heat.colors(4),
bty="n",title="log(Capelin+1)")
datai0 = datai[datai$cod>2,]
points(Yloc~Xloc,data=datai0,cex=4*datai0$cod/max(datai0$cod),lwd=1)