-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
512 lines (512 loc) · 21.3 KB
/
.Rhistory
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
# Rename hour to time for consistency when plotting below
test.agg$time <- test.agg$hour
test.agg
ntest <- length( unique(test.agg$Transmitter) ) # number of individual tags (here just the one test tag)
nrec <- nrow(rlocs) # number of receivers
tsteps <- max(test.agg$hour)
# This chunk saves the total number of encounters of each individual (rows) in each trap (cols) at each sampling occasion (array elements)
testY=array( NA, dim=c( ntest, nrec, tsteps ) )
for( t in 1:max(tsteps) ){
for ( i in 1:nrow(testY) ){
h1 <- test.agg[test.agg$tag==i,]
for( j in 1:nrow(rlocs) ){
# If there are no detections at that receiver in that time period, set to 0; otherwise set to the number of detections
testY[i,j,t]=ifelse(identical(h1[h1$hour==t & h1$rec==j,4], numeric(0)),0, h1[h1$hour==t & h1$rec==j,4])
}
}
}
testY
head(test.agg)
head(fish.agg)
sampling occasion (array elements)
testY=array( NA, dim=c( ntest, nrec, tsteps ) )
for( t in 1:max(tsteps) ){
for ( i in 1:nrow(testY) ){
h1 <- test.agg[test.agg$tag==i,]
for( j in 1:nrow(rlocs) ){
# If there are no detections at that receiver in that time period, set to 0; otherwise set to the number of detections
testY[i,j,t]=ifelse(identical(h1[h1$hour==t & h1$rec==j,6], numeric(0)),0, h1[h1$hour==t & h1$rec==j,6])
}
}
}
testY
head(test.agg)
fishdat$rec <- as.numeric(substr(fishdat$Station, 3, 4))
fishdat$count <- 1 # Add a column that indicates each record corresponds to 1 detection
# Aggregate the number of detections for each individual at each receiver in each time step
fish.agg <- aggregate(count ~ Transmitter + rec + east + north + hour, fishdat, sum)
# Create a numeric identifier for each transmitter
fish.agg$tag <- as.numeric(as.factor(fish.agg$Transmitter))
# Rename hour to time for consistency when plotting below
fish.agg$time <- fish.agg$hour
head(fish.agg)
nind <- length( unique(fish.agg$Transmitter) ) # number of individual tags (here just the one test tag)
# This chunk saves the total number of encounters of each individual (rows) in each trap (cols) at each sampling occasion (array elements)
Y=array( NA, dim=c( nind, nrec, tsteps ) )
for( t in 1:max(tsteps) ){
for ( i in 1:nrow(Y) ){
h1 <- fish.agg[fish.agg$tag==i,]
for( j in 1:nrow(rlocs) ){
# If there are no detections at that receiver in that time period, set to 0; otherwise set to the number of detections
Y[i,j,t]=ifelse(identical(h1[h1$hour==t & h1$rec==j,6], numeric(0)),0, h1[h1$hour==t & h1$rec==j,6])
}
}
}
Y
fish.agg
fit <- COA_Standard(nind=nind, # number of individuals
nrec=nrec, # number of receivers
ntime=tsteps, # number of time steps
ntrans=30, # number of expected transmissions per tag per time interval
y=Y, # array of detections
recX=as.vector(rlocs$east), # E-W receiver coordinates
recY=as.vector(rlocs$north), # N-S receiver coordinates
xlim=xlim, # E-W boundary of spatial extent (receiver array + buffer)
ylim=ylim) #
fit$Time
ew <- list(NA)
ns <- list(NA)
EN <- list(NA)
for (i in 1:nind){
ew[[i]] <- dplyr::select(fit$All_estimates, dplyr::starts_with( paste("sx[",i,",", sep='') ) )
ns[[i]] <- dplyr::select(fit$All_estimates, dplyr::starts_with( paste("sy[",i,",", sep='') ) )
# Merge them into one dataframe
EN[[i]]=cbind(data.table::melt(ew[[i]]),data.table::melt(ns[[i]])[,2])
EN[[i]]$id=as.numeric(EN[[i]]$variable) # The ID field corresponds to the time step.
colnames(EN[[i]])=c('variable','x','y','time')
}
# You will get a warning about 'No id variables' - don't worry about it.
## Plot posterior estimates
# Select individual for plotting
post <- EN[[1]]
coa <- as.data.frame(fit$COAs[,,1])
# Plot in ggplot
plotCOAs <- ggplot(aes(x=x,y=y),data=post) +
geom_hex(alpha=1,bins=100) +
geom_point(aes(x=x,y=y),data=coa,pch=25,cex=1.5,alpha=.8,fill=NA) +
facet_wrap(~time,ncol=2) +
scale_fill_gradientn(colours=c("white","blue"),name = "Frequency",na.value=NA) +
geom_point(aes(x=east,y=north),data=rlocs,cex=1) +
geom_point(aes(x=east,y=north),data=fish.agg,pch=21,cex=1.5,fill='#00BFC4') +
# Unhash line below to include test tag location
#geom_point(aes(x=east,y=north),data=testloc,cex=1.5,pch=21,fill='#F8766D') +
coord_fixed(xlim=c(-1,1),ylim=c(-0.5,1.5)) +
scale_x_continuous(breaks = c(-1, 0, 1)) +
#scale_y_continuous(breaks = c(-1, 0, 1)) +
theme(legend.position="none") +
labs(x='East-West (km)',y='North-South (km)')
plotCOAs
nsentinal <- 1
fit_tag <- COA_TagInt(nind=nind, # number of individuals
nrec=nrec, # number of receivers
ntime=tsteps, # number of time steps
ntest=nsentinal, # number of test tags
ntrans=30, # number of expected transmissions per tag per time interval
y=Y, # array of detections from tagged fish
test=testY, # array of detections from test tags
recX=as.vector(rlocs$east), # E-W receiver coordinates
recY=as.vector(rlocs$north), # N-S receiver coordinates
xlim=xlim, # E-W boundary of spatial extent (receiver array + buffer)
ylim=ylim, # N-S boundary of spatial extent (receiver array + buffer)
testX=array(testloc$east,dim=c(nsentinal)),
testY=array(testloc$north,dim=c(nsentinal)))
fit_tag$Time
ew <- list(NA)
ns <- list(NA)
EN <- list(NA)
for (i in 1:nind){
ew[[i]] <- dplyr::select(fit_tag$All_estimates, dplyr::starts_with( paste("sx[",i,",", sep='') ) )
ns[[i]] <- dplyr::select(fit_tag$All_estimates, dplyr::starts_with( paste("sy[",i,",", sep='') ) )
# Merge them into one dataframe
EN[[i]]=cbind(data.table::melt(ew[[i]]),data.table::melt(ns[[i]])[,2])
EN[[i]]$id=as.numeric(EN[[i]]$variable) # The ID field corresponds to the time step.
colnames(EN[[i]])=c('variable','x','y','time')
}
# You will get a warning about 'No id variables' - don't worry about it.
## Plot posterior estimates
# Select individual for plotting
post <- EN[[1]]
coa <- as.data.frame(fit_tag$COAs[,,1])
# Plot in ggplot
plotTagInt <- ggplot(aes(x=x,y=y),data=post) +
geom_hex(alpha=1,bins=100) +
geom_point(aes(x=x,y=y),data=coa,pch=25,cex=1.5,alpha=.8,fill=NA) +
facet_wrap(~time,ncol=2) +
scale_fill_gradientn(colours=c("white","blue"),name = "Frequency",na.value=NA) +
geom_point(aes(x=east,y=north),data=rlocs,cex=1) +
geom_point(aes(x=east,y=north),data=fish.agg,pch=21,cex=1.5,fill='#00BFC4') +
# Unhash line below to plot test tag location
#geom_point(aes(x=east,y=north),data=testloc,cex=1.5,pch=21,fill='#F8766D') +
coord_fixed(xlim=c(-1,1),ylim=c(-0.5,1.5)) +
scale_x_continuous(breaks = c(-1, 0, 1)) +
#scale_y_continuous(breaks = c(-1, 0, 1)) +
theme(legend.position="none") +
labs(x='East-West (km)',y='North-South (km)')
plotTagInt
COA=as.data.frame(aggregate(east~hour,data=fishdat,FUN=mean))
COAy=aggregate(north~hour,data=fishdat,FUN=mean)
COA$north=COAy[,2]
head(COA)
summary(COA)
COA
mw <- ggplot () +
geom_point(aes(x=east,y=north),fill='NA',data=COA,pch=25,cex=1.5,alpha=.8) +
facet_wrap(~hour,ncol=2) +
geom_point(aes(x=east,y=north),data=rlocs,cex=1) +
#geom_point(aes(x=east,y=north),data=testloc,cex=1.5,pch=21,fill='#F8766D') +
geom_point(aes(x=east,y=north),data=fishdat,pch=21,cex=1.5,fill='#00BFC4') +
coord_fixed(xlim=c(-1,1),ylim=c(-0.5,1.5)) +
scale_x_continuous(breaks = c(-1, 0, 1)) +
#scale_y_continuous(breaks = c(-1, 0, 1)) +
theme(legend.position="none") +
labs(x='East-West (km)',y='North-South (km)')
mw
library(ggpubr)
peez = ggarrange(mw, plotCOAs, plotTagInt,
labels = c("(a) Mean-weighted", "(b) Spatial point process", "(c) Test tag-integrated"),
ncol = 3, nrow = 1,
align = "hv", hjust = -0.15,
#widths = c(8,8,8), heights = c(2,2,2),
common.legend = F)
tiff(file = "TestTag_COA_examples.tiff", width = 8, height = 7.5, units = "in", res = 800)
peez
dev.off()
mw <- ggplot () +
geom_point(aes(x=east,y=north),fill='NA',data=COA,pch=25,cex=1.5,alpha=.8) +
facet_wrap(~hour,ncol=2) +
geom_point(aes(x=east,y=north),data=rlocs,cex=1) +
geom_point(aes(x=east,y=north),data=testloc,cex=1.5,pch=21,fill='#F8766D') +
geom_point(aes(x=east,y=north),data=fishdat,pch=21,cex=1.5,fill='#00BFC4') +
coord_fixed(xlim=c(-1,1),ylim=c(-0.5,1.5)) +
scale_x_continuous(breaks = c(-1, 0, 1)) +
#scale_y_continuous(breaks = c(-1, 0, 1)) +
theme(legend.position="none") +
labs(x='East-West (km)',y='North-South (km)')
ew <- list(NA)
ns <- list(NA)
EN <- list(NA)
for (i in 1:nind){
ew[[i]] <- dplyr::select(fit$All_estimates, dplyr::starts_with( paste("sx[",i,",", sep='') ) )
ns[[i]] <- dplyr::select(fit$All_estimates, dplyr::starts_with( paste("sy[",i,",", sep='') ) )
# Merge them into one dataframe
EN[[i]]=cbind(data.table::melt(ew[[i]]),data.table::melt(ns[[i]])[,2])
EN[[i]]$id=as.numeric(EN[[i]]$variable) # The ID field corresponds to the time step.
colnames(EN[[i]])=c('variable','x','y','time')
}
# You will get a warning about 'No id variables' - don't worry about it.
## Plot posterior estimates
# Select individual for plotting
post <- EN[[1]]
coa <- as.data.frame(fit$COAs[,,1])
plotCOAs <- ggplot(aes(x=x,y=y),data=post) +
geom_hex(alpha=1,bins=100) +
geom_point(aes(x=x,y=y),data=coa,pch=25,cex=1.5,alpha=.8,fill=NA) +
facet_wrap(~time,ncol=2) +
scale_fill_gradientn(colours=c("white","blue"),name = "Frequency",na.value=NA) +
geom_point(aes(x=east,y=north),data=rlocs,cex=1) +
geom_point(aes(x=east,y=north),data=fish.agg,pch=21,cex=1.5,fill='#00BFC4') +
# Unhash line below to include test tag location
geom_point(aes(x=east,y=north),data=testloc,cex=1.5,pch=21,fill='#F8766D') +
coord_fixed(xlim=c(-1,1),ylim=c(-0.5,1.5)) +
scale_x_continuous(breaks = c(-1, 0, 1)) +
#scale_y_continuous(breaks = c(-1, 0, 1)) +
theme(legend.position="none") +
labs(x='East-West (km)',y='North-South (km)')
plotCOAs
ew <- list(NA)
ns <- list(NA)
EN <- list(NA)
for (i in 1:nind){
ew[[i]] <- dplyr::select(fit_tag$All_estimates, dplyr::starts_with( paste("sx[",i,",", sep='') ) )
ns[[i]] <- dplyr::select(fit_tag$All_estimates, dplyr::starts_with( paste("sy[",i,",", sep='') ) )
# Merge them into one dataframe
EN[[i]]=cbind(data.table::melt(ew[[i]]),data.table::melt(ns[[i]])[,2])
EN[[i]]$id=as.numeric(EN[[i]]$variable) # The ID field corresponds to the time step.
colnames(EN[[i]])=c('variable','x','y','time')
}
# You will get a warning about 'No id variables' - don't worry about it.
## Plot posterior estimates
# Select individual for plotting
post <- EN[[1]]
coa <- as.data.frame(fit_tag$COAs[,,1])
plotTagInt <- ggplot(aes(x=x,y=y),data=post) +
geom_hex(alpha=1,bins=100) +
geom_point(aes(x=x,y=y),data=coa,pch=25,cex=1.5,alpha=.8,fill=NA) +
facet_wrap(~time,ncol=2) +
scale_fill_gradientn(colours=c("white","blue"),name = "Frequency",na.value=NA) +
geom_point(aes(x=east,y=north),data=rlocs,cex=1) +
geom_point(aes(x=east,y=north),data=fish.agg,pch=21,cex=1.5,fill='#00BFC4') +
# Unhash line below to plot test tag location
geom_point(aes(x=east,y=north),data=testloc,cex=1.5,pch=21,fill='#F8766D') +
coord_fixed(xlim=c(-1,1),ylim=c(-0.5,1.5)) +
scale_x_continuous(breaks = c(-1, 0, 1)) +
#scale_y_continuous(breaks = c(-1, 0, 1)) +
theme(legend.position="none") +
labs(x='East-West (km)',y='North-South (km)')
plotTagInt
mw
peez = ggarrange(mw, plotCOAs, plotTagInt,
labels = c("(a) Mean-weighted", "(b) Spatial point process", "(c) Test tag-integrated"),
ncol = 3, nrow = 1,
align = "hv", hjust = -0.15,
#widths = c(8,8,8), heights = c(2,2,2),
common.legend = F)
tiff(file = "TestTag_COA_examples.tiff", width = 8, height = 7.5, units = "in", res = 800)
peez
dev.off()
library(TelemetrySpace)
rlocs # Receiver locations
testloc # Test tag location
head(testdat) # Hourly detection data from the test tag
head(fishdat)
fishdat$rec <- as.numeric(substr(fishdat$Station, 3, 4))
fishdat$count <- 1 # Add a column that indicates each record corresponds to 1 detection
# Aggregate the number of detections for each individual at each receiver in each time step
fish.agg <- aggregate(count ~ Transmitter + rec + east + north + hour, fishdat, sum)
# Create a numeric identifier for each transmitter
fish.agg$tag <- as.numeric(as.factor(fish.agg$Transmitter))
# Rename hour to time for consistency when plotting below
fish.agg$time <- fish.agg$hour
fish.agg
m1 <- plyr::count(testdat, c('Station','hour','D','east','north'))
m1 <- plyr::count(testdat, c('Station','hour','D','east','north'))
head(fishdat)
m1 <- plyr::count(fishdat, c('Station','hour',))
m1 <- plyr::count(fishdat, c('Station','hour'))
head(m1)
head(fish.agg)
table(fishdat$Station,fishdat$hour)
table(fish$hour,fishdat$Station)
table(fishdat$hour,fishdat$Station)
fishtab = table(fishdat$hour,fishdat$Station)
fishtab
head(fishtab)
write.csv(fishtab,'seabasstally.csv')
if (!require(devtools)) {
install.packages("devtools")
library(devtools)
}
install_github("meganwinton/TelemetrySpace", build_vignettes = FALSE)
if (!require(devtools)) {
install.packages("devtools")
library(devtools)
}
install_github("meganwinton/TelemetrySpace", build_vignettes = TRUE)
if (!require(devtools)) {
install.packages("devtools")
library(devtools)
}
install_github("meganwinton/TelemetrySpace", build_vignettes = FALSE)
library(TelemetrySpace)
?COA_Standard
?COA_TagInt
library(TelemetrySpace)
rlocs # Receiver locations
testloc # Test tag location
head(testdat) # Hourly detection data from the test tag
head(fishdat) # Ho
buffer=1
xlim=c(min(rlocs$east-buffer),max(rlocs$east+buffer))
ylim=c(min(rlocs$north-buffer),max(rlocs$north+buffer))
D<-NULL
# Loop over each hour
for( i in 1:nrow(testdat) ){
D[i]=distf( testloc[,c('east','north')], testdat[i,c('east','north')] )
}
testdat$D <- D # Assign to testdat data frame
# Plot to examine variation in detection rate over time
m1 <- plyr::count(testdat, c('Station','hour','D','east','north'))
#Round distance for plotting
m1$dist=round(m1$D*1000)
#Create label by merging station name and distance
m1$label=paste(m1$dist,"m","(",m1$Station,")")
#Plot
library(ggplot2)
ggplot(data=m1,aes(x=hour,y=freq))+
geom_bar(stat="identity",fill='#008080')+
facet_wrap(~as.factor(label))+
theme(text = element_text(size = 16)) +
scale_y_continuous(breaks=seq(0,30,10)) +
labs(x="Hours since deployment",y="Number of detections")
rs <- rlocs
# Add column for each time interval to indicate whether receiver was operational or not
rs[,c(4:( max(fishdat$hour)+3 ) )] <- 1
rs <- plyr::rename( rs, replace = c("V4"=1,
"V5"=2,
"V6"=3,
"V7"=4,
"V8"=5,
"V9"=6,
"V10"=7,
"V11"=8,
"V12"=9,
"V13"=10) )
# Create vector of the number of sampling occasions for each receiver
tsteps <- apply(rs[,4:ncol(rs)],1,sum)
tsteps # Will all be the same here beca
testdat$rec <- as.numeric(substr(testdat$Station, 3, 4))
testdat$count <- 1 # Add a column that indicates each record corresponds to 1 detection
# Aggregate the number of detections for each individual at each receiver in each time step
test.agg <- aggregate(count ~ Transmitter + rec + east + north + hour, testdat, sum)
# Create a numeric identifier for each transmitter
test.agg$tag <- as.numeric(as.factor(test.agg$Transmitter))
# Rename hour to time for consistency when plotting below
test.agg$time <- test.agg$hour
# Specify quantities for indexing
ntest <- length( unique(test.agg$Transmitter) ) # number of individual tags (here just the one test tag)
nrec <- nrow(rlocs) # number of receivers
tsteps <- max(test.agg$hour)
# This chunk saves the total number of encounters of each individual (rows) in each trap (cols) at each sampling occasion (array elements)
testY=array( NA, dim=c( ntest, nrec, tsteps ) )
for( t in 1:max(tsteps) ){
for ( i in 1:nrow(testY) ){
h1 <- test.agg[test.agg$tag==i,]
for( j in 1:nrow(rlocs) ){
# If there are no detections at that receiver in that time period, set to 0; otherwise set to the number of detections
testY[i,j,t]=ifelse(identical(h1[h1$hour==t & h1$rec==j,6], numeric(0)),0, h1[h1$hour==t & h1$rec==j,6])
}
}
}
## Now do the same for each tagged fish
fishdat$rec <- as.numeric(substr(fishdat$Station, 3, 4))
fishdat$count <- 1 # Add a column that indicates each record corresponds to 1 detection
# Aggregate the number of detections for each individual at each receiver in each time step
fish.agg <- aggregate(count ~ Transmitter + rec + east + north + hour, fishdat, sum)
# Create a numeric identifier for each transmitter
fish.agg$tag <- as.numeric(as.factor(fish.agg$Transmitter))
# Rename hour to time for consistency when plotting below
fish.agg$time <- fish.agg$hour
# Specify quantities for indexing
nind <- length( unique(fish.agg$Transmitter) ) # number of individual tags (here just the one test tag)
# This chunk saves the total number of encounters of each individual (rows) in each trap (cols) at each sampling occasion (array elements)
Y=array( NA, dim=c( nind, nrec, tsteps ) )
for( t in 1:max(tsteps) ){
for ( i in 1:nrow(Y) ){
h1 <- fish.agg[fish.agg$tag==i,]
for( j in 1:nrow(rlocs) ){
# If there are no detections at that receiver in that time period, set to 0; otherwise set to the number of detections
Y[i,j,t]=ifelse(identical(h1[h1$hour==t & h1$rec==j,6], numeric(0)),0, h1[h1$hour==t & h1$rec==j,6])
}
}
}
Y
tail(Y)
summary(Y)
summary(testY)
tsteps
fit <- COA_Standard(nind=nind, # number of individuals
nrec=nrec, # number of receivers
ntime=tsteps, # number of time steps
ntrans=30, # number of expected transmissions per tag per time interval
y=Y, # array of detections
recX=as.vector(rlocs$east), # E-W receiver coordinates
recY=as.vector(rlocs$north), # N-S receiver coordinates
xlim=xlim, # E-W boundary of spatial extent (receiver array + buffer)
ylim=ylim) # N-S boundary of spatial extent (receiver array + buffer)
library(TelemetrySpace)
rlocs # Receiver locations
testloc # Test tag location
head(testdat) # Hourly detection data from the test tag
head(fishdat)
buffer=1
xlim=c(min(rlocs$east-buffer),max(rlocs$east+buffer))
ylim=c(min(rlocs$north-buffer),max(rlocs$north+buffer))
# Set up a blank vector for storage
D<-NULL
# Loop over each hour
for( i in 1:nrow(testdat) ){
D[i]=distf( testloc[,c('east','north')], testdat[i,c('east','north')] )
}
testdat$D <- D # Assign to testdat data frame
# Plot to examine variation in detection rate over time
m1 <- plyr::count(testdat, c('Station','hour','D','east','north'))
#Round distance for plotting
m1$dist=round(m1$D*1000)
#Create label by merging station name and distance
m1$label=paste(m1$dist,"m","(",m1$Station,")")
#Plot
library(ggplot2)
ggplot(data=m1,aes(x=hour,y=freq))+
geom_bar(stat="identity",fill='#008080')+
facet_wrap(~as.factor(label))+
theme(text = element_text(size = 16)) +
scale_y_continuous(breaks=seq(0,30,10)) +
labs(x="Hours since deployment",y="Number of detections")
rs <- rlocs
# Add column for each time interval to indicate whether receiver was operational or not
rs[,c(4:( max(fishdat$hour)+3 ) )] <- 1
rs <- plyr::rename( rs, replace = c("V4"=1,
"V5"=2,
"V6"=3,
"V7"=4,
"V8"=5,
"V9"=6,
"V10"=7,
"V11"=8,
"V12"=9,
"V13"=10) )
# Create vector of the number of sampling occasions for each receiver
tsteps <- apply(rs[,4:ncol(rs)],1,sum)
tsteps # Will all be the same here b
testdat$rec <- as.numeric(substr(testdat$Station, 3, 4))
testdat$count <- 1 # Add a column that indicates each record corresponds to 1 detection
# Aggregate the number of detections for each individual at each receiver in each time step
test.agg <- aggregate(count ~ Transmitter + rec + east + north + hour, testdat, sum)
# Create a numeric identifier for each transmitter
test.agg$tag <- as.numeric(as.factor(test.agg$Transmitter))
# Rename hour to time for consistency when plotting below
test.agg$time <- test.agg$hour
# Specify quantities for indexing
ntest <- length( unique(test.agg$Transmitter) ) # number of individual tags (here just the one test tag)
nrec <- nrow(rlocs) # number of receivers
tsteps <- max(test.agg$hour)
# This chunk saves the total number of encounters of each individual (rows) in each trap (cols) at each sampling occasion (array elements)
testY=array( NA, dim=c( ntest, nrec, tsteps ) )
for( t in 1:max(tsteps) ){
for ( i in 1:nrow(testY) ){
h1 <- test.agg[test.agg$tag==i,]
for( j in 1:nrow(rlocs) ){
# If there are no detections at that receiver in that time period, set to 0; otherwise set to the number of detections
testY[i,j,t]=ifelse(identical(h1[h1$hour==t & h1$rec==j,6], numeric(0)),0, h1[h1$hour==t & h1$rec==j,6])
}
}
}
## Now do the same for each tagged fish
fishdat$rec <- as.numeric(substr(fishdat$Station, 3, 4))
fishdat$count <- 1 # Add a column that indicates each record corresponds to 1 detection
# Aggregate the number of detections for each individual at each receiver in each time step
fish.agg <- aggregate(count ~ Transmitter + rec + east + north + hour, fishdat, sum)
# Create a numeric identifier for each transmitter
fish.agg$tag <- as.numeric(as.factor(fish.agg$Transmitter))
# Rename hour to time for consistency when plotting below
fish.agg$time <- fish.agg$hour
# Specify quantities for indexing
nind <- length( unique(fish.agg$Transmitter) ) # number of individual tags (here just the one test tag)
# This chunk saves the total number of encounters of each individual (rows) in each trap (cols) at each sampling occasion (array elements)
Y=array( NA, dim=c( nind, nrec, tsteps ) )
for( t in 1:max(tsteps) ){
for ( i in 1:nrow(Y) ){
h1 <- fish.agg[fish.agg$tag==i,]
for( j in 1:nrow(rlocs) ){
# If there are no detections at that receiver in that time period, set to 0; otherwise set to the number of detections
Y[i,j,t]=ifelse(identical(h1[h1$hour==t & h1$rec==j,6], numeric(0)),0, h1[h1$hour==t & h1$rec==j,6])
}
}
}
fit <- COA_Standard(nind=nind, # number of individuals
nrec=nrec, # number of receivers
ntime=tsteps, # number of time steps
ntrans=30, # number of expected transmissions per tag per time interval
y=Y, # array of detections
recX=as.vector(rlocs$east), # E-W receiver coordinates
recY=as.vector(rlocs$north), # N-S receiver coordinates
xlim=xlim, # E-W boundary of spatial extent (receiver array + buffer)
ylim=ylim) # N-S boun
fit$Time
fit$Time/60
fit$Time/60/4
?scale
setwd("C:/Users/mwinton/Documents/TelemetrySpace")
list.files(all.files = TRUE)
roxygen2::roxygenise(clean=TRUE)
devtools::install(local=FALSE)