forked from sreelakshmi617/WaggleDance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSSWaggle2024v2from_ESM_4_2019_linden_Beekeeping_Lab.R
2692 lines (2111 loc) · 116 KB
/
SSWaggle2024v2from_ESM_4_2019_linden_Beekeeping_Lab.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
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
######## This script has been adapted for a new dataset by Douglas Sponsler and Reed Johnson ([email protected]).
######## Unused portions of the original script have been commented out but not deleted. Additions are followed by the tag # Sponsler:
# Electronic Supplementary Material 4 - R script to simulate dances from known waggle
# dance durations and headings.
# ------------------------------------------------------------------------------------
# Article title: Incorporating variability in honey bee waggle dance decoding improves
# the mapping of communicated resource locations
# Journal: Journal of Comparative Physiology A
# Authors: Roger Schurch, Margaret J. Couvillon, Dominic D. R. Burns, Kiah
# Tasman, David Waxman and Francis L. W. Ratnieks
# Corresponding author: Roger Schurch, Evolution, Behaviour and
# Environment, School of Life Sciences, University of Sussex, Brighton,
# BN1 9QG, United Kingdom, [email protected]
# script files that go with this script:
# ESM_3.jag
# data files that go with this script:
# ESM_5.csv
# the file will output a comma separated value file and an ASC raster file
# you must create a "data" folder within the folder from which you are running the scripts
# or adapt the paths for the instructions below
# Load Ubuntu packages
# sudo apt install jags r-cran-rgdal r-recommended
# Load CRAN packages needed in this script
# install.packages(c("circular", "rjags", "sp", "rgdal", "raster", "png", "googlesheets", "magrittr", "oce"))
## Clear everything
rm(list=ls())
library('circular') # for circular stats
library('rjags') # interface with JAGS
library('sp') # spatial stats, coordinates etc
library('rgdal') # convert between different coodrinate systems, spTransform
library('raster') # for plotting spatial data
library('png') # to save figures to jpeg
library('googlesheets4') # Read from GoogleDocs
library('magrittr') # Pipes
library('oce') # Calculation of azimuth
## Set local working directory (uncomment and change)
setwd("/Users/dolezallab-macpro/Desktop/WaggleDance") # Sponsler: path to the working directory within the Git repository
## Create data subfolder if it doesn't already exist
dir.create("data", showWarnings = FALSE)
## Import dance data file
gs_title("SureshDance_Thesis") %>%
gs_download(ws = "Data", to = "SureshDance_Thesis.csv", overwrite = TRUE)
#waggleFile <- "SureshDance_Thesis.csv" # Set the name for the file to use
## Download data from GoogleSheets using 'googlesheets' library. Store as csv file specified in "wagglefile"
# "SD Tracks" https://docs.google.com/spreadsheets/d/1ZXxYSRK74wCT7xULdF4hyReSCYsl2kUqe04kiROzPUs/edit?usp=sharing
#gs_title("SureshDance_Thesis.csv") %>%
# gs_download(ws = "Data", to = waggleFile, overwrite = TRUE)
waggleData <- read.csv("SureshDance_Thesis.csv") # Sponsler: path to our dance data
## Remove flagged lines
waggleData <- subset(waggleData, is.na(waggleData$flag)) # Sponsler: a flag field removes empty or incomplete lines
## Fix dates/times and use them to calculate azimuth
waggleData$date <- paste(waggleData$year, sprintf("%02d", waggleData$month), sprintf("%02d", waggleData$day), sep="-")
waggleData$time <- paste(sprintf("%02d", waggleData$hour), sprintf("%02d", waggleData$min), sep=":")
waggleData$dateTime <- as.POSIXct(strptime(paste(waggleData$date, waggleData$time, sep=" "), "%Y-%m-%d %H:%M"), tz="US/Central")
attr(waggleData$dateTime, "tzone") <- "UTC" ## Convert to UTC
waggleData$azimuth <- sunAngle(waggleData$dateTime, waggleData$lon, waggleData$lat, useRefraction=TRUE)$azimuth
## Calculate heading in radians
waggleData[is.na(waggleData$skew),]$skew <- 0
waggleData$heading.degrees <- (((waggleData$mean_angle-waggleData$skew+waggleData$azimuth)+90) %% 360) ## Something not concordant with GoogleDoc calculation here
waggleData$heading.radians <- (waggleData$heading.degrees*pi)/180
## Designate Bloom vs. Non-Bloom dates
#waggleData$bloom <- FALSE
#waggleData[waggleData$dateTime >= "2016-06-27" & waggleData$dateTime <= "2016-08-04",]$bloom <- TRUE
## Get summary of dances recorded by hive and by date
table(waggleData[,c("hive", "date")])
## Synthetic variable with hive and date
#waggleData$hiveDate <- paste(waggleData$hive, waggleData$date, sep=".")
##################################
## Set calibration
#################################
#read the calibration data from ESM_5.csv
calibDataAgg <- read.csv("ESM_5.csv", row.names = 1)
calibDataAgg$heading <- circular(calibDataAgg$heading,
type = "angle",
unit = "radian",
rotation = "clock",
zero = pi/2)
## make the data properly circular
waggleData$heading <- circular(waggleData$heading.radians,
type = "angle",
unit = "radian",
rotation = "clock",
zero = pi/2)
## how many samples per dance; think carefully about how many samples you really need:
## the more samples, the longer it will take to simulate your dances
finalSampleSize <- 1000
thinning <- 100
noJagsSamples <- thinning*finalSampleSize
## preparations to calculate point coords from angle and distance (get from https://epsg.io/)
#hiveEasting <- 534939 # the UK grid easting of the hives in meters
hiveEasting <- 422363.59 # Sponsler: the UTM 17N (EPSG:26917) easting of the hives in meters
#hiveNorthing <- 108900 # the UK grid northing of the hives in meters
hiveNorthing <- 4514220.26 # Sponsler: the UTM 17N (EPSG:26917) northing of the hives in meters
## to calculate the rasters
distanceToHives <- 10000 # how far should the rasters extend from the hives in meters
gridCellSize <- 25 # grid size in meters
noCells <- 2*distanceToHives/gridCellSize # the number of cols and rows needed to get grid of meters
defaultMatrix <- matrix(data = 0, # create a default matrix as a basis for our dance counts
ncol = noCells, nrow = noCells)
## prepare an georeferenced extent (in GIS terms); adapt to fit your coordinate system
coordPanel <- data.frame(cbind(easting = c(hiveEasting, hiveEasting - distanceToHives,
hiveEasting + distanceToHives),
northing = c(hiveNorthing, hiveNorthing - distanceToHives,
hiveNorthing + distanceToHives)))
coordinates(coordPanel) <- c("easting", "northing")
proj4string(coordPanel) = CRS("+init=epsg:26917") # Sponsler: our CRS is UTM 17N (EPSG:26917)
## prepare a raster with the extent; adapt to fit your coordinate system
numCoordPanel <- as.data.frame(coordPanel)
temp.rast <- raster(ncols = noCells, nrows = noCells)
extent(temp.rast) <- extent(c(numCoordPanel[2:3,1], numCoordPanel[2:3,2]))
proj4string(temp.rast) = CRS("+init=epsg:26917") # Sponsler: our CRS is UTM 17N (EPSG:26917)
## create a raster that will store the actual probability-visited data
total.temp.rast <- temp.rast
## only select tagged bees for calibration dances
calibDataAggBees <- calibDataAgg[!is.na(calibDataAgg$bee.id),]
## prepare the variables for the calibration model
N1 <- length(calibDataAggBees$duration)
x <- calibDataAggBees$distance
y <- calibDataAggBees$duration
K <- length(unique(calibDataAggBees$bee.id))
bee <- factor(calibDataAggBees$bee.id)
#############################################
## Process
##################################################
## All together
###########################
## Process by date
###########################
#datePick <- "2018-06-25"
#datePick <- "2018-06-28"
datePick <- "2018-07-12"
waggleDataDate <- subset(waggleData, date == datePick)
waggleDataDate
##########################
## Process by date and color for collected bees
############################
#waggleCollected <- waggleData[waggleData$color != "",]
#waggleCollected$dateColor <- paste(waggleCollected$date, waggleCollected$color,sep=".")
waggleCollected <- waggleData
waggleCollected$dateColor <- paste(waggleCollected$date)
# loop through all dateColors
for (j in unique(waggleCollected$dateColor)) {
waggleDataDate <- waggleCollected[waggleCollected$dateColor == j,]
# loop through all the dances
for(i in 1:length(waggleDataDate$dancer.id)){
cat(paste(i, "of", length(waggleDataDate$dancer.id), "\n"))
# choose only the i^th dance
tempData <- waggleDataDate[i,]
# prepare the variables for the prediction model
N2 <- length(tempData$mean_duration.sec)
x2 <- rep(NA, length(tempData$mean_duration.sec))
y2 <- tempData$mean_duration.sec
# load the model from file and submit the data
jags <- jags.model('ESM_3.jag',
data = list('x' = x, 'y' = y,
'N1' = N1, 'K' = K, 'bee' = bee, 'N2' = N2, 'x2' = x2, 'y2' = y2),
n.chains = 1,
n.adapt = 100)
# update for the burn-in
update(jags, 100000)
# sample from the posterior
samples <- coda.samples(jags, c('x2'), noJagsSamples, thin = thinning)
# save the samples in a handy variable
sim.distances <- samples[,'x2'][[1]]
# the 1000 draws have to be taken according to what is in the posterior samples for distance
sim.heading <- rvonmises(finalSampleSize, mu = tempData$heading,
kappa = 24.9, control.circular = list("radians"))
# calculate the coordinates from the vector with origin of the hives
rel.dance.easting <- as.numeric(hiveEasting + cos(-(sim.heading- pi/2))*sim.distances)
rel.dance.northing <- as.numeric(hiveNorthing + sin(-(sim.heading - pi/2))*sim.distances)
# save as points for further use
temp.points <- data.frame(cbind(id = rep(tempData$id, length(rel.dance.easting)),
easting = as.numeric(rel.dance.easting),
northing = as.numeric(rel.dance.northing)))
# save the points in a comma seperated value file
# csv points can be imported into GIS for further processing
write.csv(temp.points, paste("data/sim.dance_", tempData$id, ".csv", sep = ""), row.names = FALSE)
if(i <= 1){ # on the first pass create a new file, else add the data to the existing file
write.csv(temp.points, "data/simAllDances.csv", row.names = FALSE)
}else{
# save the total counts
tempData2 <- read.csv("data/simAllDances.csv")
tempData2 <- rbind(tempData2, temp.points)
write.csv(tempData2, "data/simAllDances.csv", row.names = FALSE)
}
# georeference the points on the UK grid
coordinates(temp.points) = c("easting", "northing")
#proj4string(temp.points) = CRS("+init=epsg:27700")
proj4string(coordPanel) = CRS("+init=epsg:26917") # Sponsler: our CRS is UTM 17N (EPSG:26917)
# create a new raster with the temp.rast extent and sample the points on the raster / final sample size
# e.g. probability that a dance has been on a certain raster square
#temp.rast.UKGRID <- rasterize(temp.points, temp.rast, fun = "count", background = 0)/finalSampleSize
temp.rast.UTM17N <- rasterize(temp.points, temp.rast, fun = "count", background = 0)/finalSampleSize # Sponsler:
# convert the raster to a SpatialGridDataFrame
#g <- as(temp.rast.UKGRID, 'SpatialGridDataFrame')
g <- as(temp.rast.UTM17N, 'SpatialGridDataFrame') # Sponsler:
# save the file to disk (can be imported into GIS)
currentFileName <- paste("data/raster_", tempData$id, ".asc", sep = "")
write.asciigrid(g, currentFileName)
if(i <= 1){ # on the first pass create a new file, else add the data to the existing file
#total.temp.rast <- temp.rast.UKGRID
total.temp.rast <- temp.rast.UTM17N # Sponsler:
}else{
# calculate the probability that a field has been visited
#total.temp.rast <- 1 - (1 - total.temp.rast)*(1 - temp.rast.UKGRID)
total.temp.rast <- 1 - (1 - total.temp.rast)*(1 - temp.rast.UTM17N)
}
}
# save the combined dances as one raster file ready to be imported in ArcGIS
# total.temp.rast <- total.temp.rast$id
# g.total <- as(total.temp.rast, 'SpatialGridDataFrame')
# write.asciigrid(g.total, "data/totalRaster.asc")
# for plotting, we can crop the extent to our needs
# prepare an georeferenced extent (in GIS terms) for the cropping
coordPanel.crop <- data.frame(cbind(easting = c(hiveEasting, hiveEasting - 10000,
hiveEasting + 10000),
northing = c(hiveNorthing, hiveNorthing - 10000,
hiveNorthing + 10000)))
coordinates(coordPanel.crop) <- c("easting", "northing")
#proj4string(coordPanel.crop) = CRS("+init=epsg:27700")
proj4string(coordPanel.crop) = CRS("+init=epsg:26917") # Sponsler:
# prepare a raster with the extent to crop the aerial photography
numCoordPanel.crop <- as.data.frame(coordPanel.crop)
crop.rast <- raster(ncols = noCells, nrows = noCells)
extent(crop.rast) <- extent(c(numCoordPanel.crop[2:3,1], numCoordPanel.crop[2:3,2]))
#proj4string(crop.rast) = CRS("+init=epsg:27700")
proj4string(crop.rast) = CRS("+init=epsg:26917") # Sponsler:
# we crop the data raster to size
new.data.rast <- crop(total.temp.rast, crop.rast)
writeRaster(new.data.rast, filename = paste("data/Wooster_Linden_",j,".tif", sep=""), format = "GTiff", overwrite = T) # Sponsler: this geotiff can be loaded in QGIS to overlay on landscape layer
# writeRaster(new.data.rast, filename = paste("data/Wooster_Linden_",datePick,".tif", sep=""), format = "GTiff", overwrite = T) # Sponsler: this geotiff can be loaded in QGIS to overlay on landscape layer
} # End of by day
#########################################
#########################################
### PLOTTING WITHOUT AERIAL PHOTOGRAPHY
# choose colours of your liking, and symbol size that matched the figure
myAlpha <- c(0, seq(0.005, 0.5, 0.005) + 0.3)
myCols <- rev(rainbow(100, alpha = rev(myAlpha)))
charEx <- 2
# Sponsler: plot vector landscape layer -- this doesn't quite work yet; alignment of the two plots is off
#mooreman <- readOGR("/Users/dougsponsler/Documents/Research/CDRC_dance_analysis", "2015_sites_1500m_buffer_squares_site__MO_clip")
#plot(mooreman, col = c("yellow", "green", "gray"))
#par(new = T)
# plot the data raster
plot(new.data.rast, col = myCols, legend.width = charEx/2,
smallplot=c(.875,.9,0.33,0.9), axis.args = list(cex.axis = charEx/2))
# plot the hive location
#points(hiveEasting, hiveNorthing, pch = 17, col = "white", cex = 2*charEx)
points(hiveEasting, hiveNorthing, pch = 20, col = "black", cex = charEx/2) # Sponsler:
#points(hiveEasting, hiveNorthing, pch = 17, col = "red", cex = charEx)
# plot the feeder location
#points(533999, 109254, pch = 19, col = "white", cex = 2*charEx)
#points(533999, 109254, pch = 19, col = "black", cex = charEx)
# Sponsler: still need to work out the aerial photography part, though we may end up plotting with the vector layers instead
### PLOTTING WITH AERIAL PHOTOGRAPHY
# if you have access to aerial photography (i.e. a GeoTIFF), you can load
# that now and crop it to size
# new.raster <- brick("someAerialPhotography.tif")
# new.raster <- crop(new.raster, crop.rast)
# plot the aerial photography
# plotRGB(new.raster, r=1, g=2, b=3)
# plot a background for the colour scale
# polygon(c(hiveEasting + 50, hiveEasting + 50, hiveEasting + 250, hiveEasting + 250, hiveEasting + 50),
# c(hiveNorthing + 50, hiveNorthing + 1075, hiveNorthing + 1075, hiveNorthing + 50, hiveNorthing + 50), col = "white", border = "black", lwd = 2)
# plot the data raster
# plot(new.data.rast, add = TRUE, col = myCols, legend.width = charEx/2,
# smallplot=c(.875,.9,0.33,0.9), axis.args = list(cex.axis = charEx/2))
# plot the hive location
#points(hiveEasting, hiveNorthing, pch = 17, col = "white", cex = 2*charEx)
# points(hiveEasting, hiveNorthing, pch = 20, col = "black", cex = charEx/2) # Sponsler:
#points(hiveEasting, hiveNorthing, pch = 17, col = "red", cex = charEx)
# plot the feeder location
#points(533999, 109254, pch = 19, col = "white", cex = 2*charEx)
#points(533999, 109254, pch = 19, col = "black", cex = charEx)
# get rid of the simulation data in memory
rm(sim.distances)
rm(sim.heading)
rm(rel.dance.easting)
rm(rel.dance.northing)
rm(samples)
rm(temp.points)
rm(g.total)
rm(total.temp.rast)
rm(temp.rast.UKGRID)
############################
##### BROKEN UP BY DAY #####
############################
##### MAY 4 ###############################################################################################################
# loading packages needed in this script
library('circular') # for circular stats
library('rjags') # interface with JAGS
library('sp') # spatial stats, coordinates etc
library('rgdal') # convert between different coodrinate systems, spTransform
library('raster') # for plotting spatial data
library('png') # to save figures to jpeg
setwd("Users/dolezallab-macpro/Desktop/WaggleDance") # Sponsler: path to the working directory on my laptop
#read the calibration data from ESM_5.csv
calibDataAgg <- read.csv("ESM_5.csv", row.names = 1)
calibDataAgg$heading <- circular(calibDataAgg$heading,
type = "angle",
unit = "radian",
rotation = "clock",
zero = pi/2)
# prepare your own data, i.e. create or read from file a
# data frame with "duration" in sec and "heading" in radians
# DO ONE DANCE AT A TIME, else the prior will overwhelm the data
# as an example, we will use our own data for dances that have gone to
# feeder at 1 km, using only the first dance of every bee.
# dance ids to that feeder
#dance.ids <- c(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, 285, 286, 268, 269, 270)
# store the subset of dances going to that feeder in waggleData
#waggleData <- calibDataAgg[calibDataAgg$dance.id %in% dance.ids,]
waggleData <- read.csv("SureshDance_Thesis.csv") # Sponsler: path to our dance data
waggleData <- subset(waggleData, flag == 1 & day == 4) # Sponsler: a flag field removes empty or incomplete lines
# we only want the first dance of every individual bee, and we prepare a function to achieve that
#getFirstElement <- function(pVector){
#reslt <- NA
#if(length(pVector) > 0){
#reslt <- pVector[1]
#}
#reslt
#}
# now only select the first dance of every bee
#waggleData <- aggregate(cbind(dance.id = waggleData$dance.id,
# duration = waggleData$duration,
#distance = waggleData$distance,
# heading = waggleData$heading), #by = list(bee.id = waggleData$bee.id),
# getFirstElement)
# make the data properly circular
waggleData$heading <- circular(waggleData$heading,
type = "angle",
unit = "radian",
rotation = "clock",
zero = pi/2)
# how many samples per dance; think carefully about how many samples you really need:
# the more samples, the longer it will take to simulate your dances
finalSampleSize <- 1000
thinning <- 100
noJagsSamples <- thinning*finalSampleSize
# preparations to calculate point coords from angle and distance
#hiveEasting <- 534939 # the UK grid easting of the hives in meters
hiveEasting <- 292263.656365 # Sponsler: the UTM 17N (EPSG:26917) easting of the hives in meters
#hiveNorthing <- 108900 # the UK grid northing of the hives in meters
hiveNorthing <- 4426271.356893 # Sponsler: the UTM 17N (EPSG:26917) northing of the hives in meters
# to calculate the rasters
distanceToHives <- 10000 # how far should the rasters extend from the hives in meters
gridCellSize <- 25 # grid size in meters
noCells <- 2*distanceToHives/gridCellSize # the number of cols and rows needed to get grid of meters
defaultMatrix <- matrix(data = 0, # create a default matrix as a basis for our dance counts
ncol = noCells, nrow = noCells)
# prepare an georeferenced extent (in GIS terms); adapt to fit your coordinate system
coordPanel <- data.frame(cbind(easting = c(hiveEasting, hiveEasting - distanceToHives,
hiveEasting + distanceToHives),
northing = c(hiveNorthing, hiveNorthing - distanceToHives,
hiveNorthing + distanceToHives)))
coordinates(coordPanel) <- c("easting", "northing")
#proj4string(coordPanel) = CRS("+init=epsg:27700")
proj4string(coordPanel) = CRS("+init=epsg:26917") # Sponsler: our CRS is UTM 17N (EPSG:26917)
# prepare a raster with the extent; adapt to fit your coordinate system
numCoordPanel <- as.data.frame(coordPanel)
temp.rast <- raster(ncols = noCells, nrows = noCells)
extent(temp.rast) <- extent(c(numCoordPanel[2:3,1], numCoordPanel[2:3,2]))
#proj4string(temp.rast) = CRS("+init=epsg:27700")
proj4string(temp.rast) = CRS("+init=epsg:26917") # Sponsler: our CRS is UTM 17N (EPSG:26917)
# create a raster that will store the actual probability-visited data
total.temp.rast <- temp.rast
# only select tagged bees for calibration dances
calibDataAggBees <- calibDataAgg[!is.na(calibDataAgg$bee.id),]
# prepare the variables for the calibration model
N1 <- length(calibDataAggBees$duration)
x <- calibDataAggBees$distance
y <- calibDataAggBees$duration
K <- length(unique(calibDataAggBees$bee.id))
bee <- factor(calibDataAggBees$bee.id)
# loop through all the dances
for(i in 1:length(waggleData$dance.id)){
cat(paste(i, "of", length(waggleData$dance.id), "\n"))
# choose only the i^th dance
tempData <- waggleData[i,]
# prepare the variables for the prediction model
N2 <- length(tempData$duration)
x2 <- rep(NA, length(tempData$duration))
y2 <- tempData$duration
# load the model from file and submit the data
jags <- jags.model('ESM_3.jag',
data = list('x' = x, 'y' = y,
'N1' = N1, 'K' = K, 'bee' = bee, 'N2' = N2, 'x2' = x2, 'y2' = y2),
n.chains = 1,
n.adapt = 100)
# update for the burn-in
update(jags, 100000)
# sample from the posterior
samples <- coda.samples(jags, c('x2'), noJagsSamples, thin = thinning)
# save the samples in a handy variable
sim.distances <- samples[,'x2'][[1]]
# the 1000 draws have to be taken according to what is in the posterior samples for distance
sim.heading <- rvonmises(finalSampleSize, mu = tempData$heading,
kappa = 24.9, control.circular = list("radians"))
# calculate the coordinates from the vector with origin of the hives
rel.dance.easting <- as.numeric(hiveEasting + cos(-(sim.heading- pi/2))*sim.distances)
rel.dance.northing <- as.numeric(hiveNorthing + sin(-(sim.heading - pi/2))*sim.distances)
# save as points for further use
temp.points <- data.frame(cbind(dance.id = rep(tempData$dance.id, length(rel.dance.easting)),
easting = as.numeric(rel.dance.easting),
northing = as.numeric(rel.dance.northing)))
# save the points in a comma seperated value file
# csv points can be imported into GIS for further processing
write.csv(temp.points, paste("data/sim.dance_", tempData$dance.id, ".csv", sep = ""), row.names = FALSE)
if(i <= 1){ # on the first pass create a new file, else add the data to the existing file
write.csv(temp.points, "data/simAllDances.csv", row.names = FALSE)
}else{
# save the total counts
tempData2 <- read.csv("data/simAllDances.csv")
tempData2 <- rbind(tempData2, temp.points)
write.csv(tempData2, "data/simAllDances.csv", row.names = FALSE)
}
# georeference the points on the UK grid
coordinates(temp.points) = c("easting", "northing")
#proj4string(temp.points) = CRS("+init=epsg:27700")
proj4string(coordPanel) = CRS("+init=epsg:26917") # Sponsler: our CRS is UTM 17N (EPSG:26917)
# create a new raster with the temp.rast extent and sample the points on the raster / final sample size
# e.g. probability that a dance has been on a certain raster square
#temp.rast.UKGRID <- rasterize(temp.points, temp.rast, fun = "count", background = 0)/finalSampleSize
temp.rast.UTM17N <- rasterize(temp.points, temp.rast, fun = "count", background = 0)/finalSampleSize # Sponsler:
# convert the raster to a SpatialGridDataFrame
#g <- as(temp.rast.UKGRID, 'SpatialGridDataFrame')
g <- as(temp.rast.UTM17N, 'SpatialGridDataFrame') # Sponsler:
# save the file to disk (can be imported into GIS)
currentFileName <- paste("data/raster_", tempData$dance.id, ".asc", sep = "")
write.asciigrid(g, currentFileName)
if(i <= 1){ # on the first pass create a new file, else add the data to the existing file
#total.temp.rast <- temp.rast.UKGRID
total.temp.rast <- temp.rast.UTM17N # Sponsler:
}else{
# calculate the probability that a field has been visited
#total.temp.rast <- 1 - (1 - total.temp.rast)*(1 - temp.rast.UKGRID)
total.temp.rast <- 1 - (1 - total.temp.rast)*(1 - temp.rast.UTM17N)
}
}
# save the combined dances as one raster file ready to be imported in ArcGIS
total.temp.rast <- total.temp.rast$dance.id
g.total <- as(total.temp.rast, 'SpatialGridDataFrame')
write.asciigrid(g.total, "data/totalRaster.asc")
# for plotting, we can crop the extent to our needs
# prepare an georeferenced extent (in GIS terms) for the cropping
coordPanel.crop <- data.frame(cbind(easting = c(hiveEasting, hiveEasting - 10000,
hiveEasting + 10000),
northing = c(hiveNorthing, hiveNorthing - 10000,
hiveNorthing + 10000)))
coordinates(coordPanel.crop) <- c("easting", "northing")
#proj4string(coordPanel.crop) = CRS("+init=epsg:27700")
proj4string(coordPanel.crop) = CRS("+init=epsg:26917") # Sponsler:
# prepare a raster with the extent to crop the aerial photography
numCoordPanel.crop <- as.data.frame(coordPanel.crop)
crop.rast <- raster(ncols = noCells, nrows = noCells)
extent(crop.rast) <- extent(c(numCoordPanel.crop[2:3,1], numCoordPanel.crop[2:3,2]))
#proj4string(crop.rast) = CRS("+init=epsg:27700")
proj4string(crop.rast) = CRS("+init=epsg:26917") # Sponsler:
# we crop the data raster to size
new.data.rast <- crop(total.temp.rast, crop.rast)
writeRaster(new.data.rast, filename = "fsr_may4.tif", format = "GTiff", overwrite = T) # Sponsler: this geotiff can be loaded in QGIS to overlay on landscape layer
### PLOTTING WITHOUT AERIAL PHOTOGRAPHY
# choose colours of your liking, and symbol size that matched the figure
myAlpha <- c(0, seq(0.005, 0.5, 0.005) + 0.3)
myCols <- rev(rainbow(100, alpha = rev(myAlpha)))
charEx <- 2
# Sponsler: plot vector landscape layer -- this doesn't quite work yet; alignment of the two plots is off
#mooreman <- readOGR("/Users/dougsponsler/Documents/Research/CDRC_dance_analysis", "2015_sites_1500m_buffer_squares_site__MO_clip")
#plot(mooreman, col = c("yellow", "green", "gray"))
#par(new = T)
# plot the data raster
plot(new.data.rast, col = myCols, legend.width = charEx/2,
smallplot=c(.875,.9,0.33,0.9), axis.args = list(cex.axis = charEx/2))
# plot the hive location
#points(hiveEasting, hiveNorthing, pch = 17, col = "white", cex = 2*charEx)
points(hiveEasting, hiveNorthing, pch = 20, col = "black", cex = charEx/2) # Sponsler:
#points(hiveEasting, hiveNorthing, pch = 17, col = "red", cex = charEx)
# plot the feeder location
#points(533999, 109254, pch = 19, col = "white", cex = 2*charEx)
#points(533999, 109254, pch = 19, col = "black", cex = charEx)
# Sponsler: still need to work out the aerial photography part, though we may end up plotting with the vector layers instead
### PLOTTING WITH AERIAL PHOTOGRAPHY
# if you have access to aerial photography (i.e. a GeoTIFF), you can load
# that now and crop it to size
# new.raster <- brick("someAerialPhotography.tif")
# new.raster <- crop(new.raster, crop.rast)
# plot the aerial photography
# plotRGB(new.raster, r=1, g=2, b=3)
# plot a background for the colour scale
# polygon(c(hiveEasting + 50, hiveEasting + 50, hiveEasting + 250, hiveEasting + 250, hiveEasting + 50),
# c(hiveNorthing + 50, hiveNorthing + 1075, hiveNorthing + 1075, hiveNorthing + 50, hiveNorthing + 50), col = "white", border = "black", lwd = 2)
# plot the data raster
# plot(new.data.rast, add = TRUE, col = myCols, legend.width = charEx/2,
# smallplot=c(.875,.9,0.33,0.9), axis.args = list(cex.axis = charEx/2))
# plot the hive location
#points(hiveEasting, hiveNorthing, pch = 17, col = "white", cex = 2*charEx)
# points(hiveEasting, hiveNorthing, pch = 20, col = "black", cex = charEx/2) # Sponsler:
#points(hiveEasting, hiveNorthing, pch = 17, col = "red", cex = charEx)
# plot the feeder location
#points(533999, 109254, pch = 19, col = "white", cex = 2*charEx)
#points(533999, 109254, pch = 19, col = "black", cex = charEx)
# get rid of the simulation data in memory
rm(sim.distances)
rm(sim.heading)
rm(rel.dance.easting)
rm(rel.dance.northing)
rm(samples)
rm(temp.points)
rm(g.total)
rm(total.temp.rast)
rm(temp.rast.UKGRID)
rm(list = ls())
##### MAY 5 ###############################################################################################################
# loading packages needed in this script
library('circular') # for circular stats
library('rjags') # interface with JAGS
library('sp') # spatial stats, coordinates etc
library('rgdal') # convert between different coodrinate systems, spTransform
library('raster') # for plotting spatial data
library('png') # to save figures to jpeg
setwd("/Users/dolezallab-macpro/Desktop/WaggleDance") # Sponsler: path to the working directory on my laptop
#read the calibration data from ESM_5.csv
calibDataAgg <- read.csv("ESM_5.csv", row.names = 1)
calibDataAgg$heading <- circular(calibDataAgg$heading,
type = "angle",
unit = "radian",
rotation = "clock",
zero = pi/2)
# prepare your own data, i.e. create or read from file a
# data frame with "duration" in sec and "heading" in radians
# DO ONE DANCE AT A TIME, else the prior will overwhelm the data
# as an example, we will use our own data for dances that have gone to
# feeder at 1 km, using only the first dance of every bee.
# dance ids to that feeder
#dance.ids <- c(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, 285, 286, 268, 269, 270)
# store the subset of dances going to that feeder in waggleData
#waggleData <- calibDataAgg[calibDataAgg$dance.id %in% dance.ids,]
waggleData <- read.csv("SureshDance_Thesis.csv") # Sponsler: path to our dance data
waggleData <- subset(waggleData, flag == 1 & day == 5) # Sponsler: a flag field removes empty or incomplete lines
# we only want the first dance of every individual bee, and we prepare a function to achieve that
#getFirstElement <- function(pVector){
#reslt <- NA
#if(length(pVector) > 0){
#reslt <- pVector[1]
#}
#reslt
#}
# now only select the first dance of every bee
#waggleData <- aggregate(cbind(dance.id = waggleData$dance.id,
# duration = waggleData$duration,
#distance = waggleData$distance,
# heading = waggleData$heading), #by = list(bee.id = waggleData$bee.id),
# getFirstElement)
# make the data properly circular
waggleData$heading <- circular(waggleData$heading,
type = "angle",
unit = "radian",
rotation = "clock",
zero = pi/2)
# how many samples per dance; think carefully about how many samples you really need:
# the more samples, the longer it will take to simulate your dances
finalSampleSize <- 1000
thinning <- 100
noJagsSamples <- thinning*finalSampleSize
# preparations to calculate point coords from angle and distance
#hiveEasting <- 534939 # the UK grid easting of the hives in meters
hiveEasting <- 292263.656365 # Sponsler: the UTM 17N (EPSG:26917) easting of the hives in meters
#hiveNorthing <- 108900 # the UK grid northing of the hives in meters
hiveNorthing <- 4426271.356893 # Sponsler: the UTM 17N (EPSG:26917) northing of the hives in meters
# to calculate the rasters
distanceToHives <- 10000 # how far should the rasters extend from the hives in meters
gridCellSize <- 25 # grid size in meters
noCells <- 2*distanceToHives/gridCellSize # the number of cols and rows needed to get grid of meters
defaultMatrix <- matrix(data = 0, # create a default matrix as a basis for our dance counts
ncol = noCells, nrow = noCells)
# prepare an georeferenced extent (in GIS terms); adapt to fit your coordinate system
coordPanel <- data.frame(cbind(easting = c(hiveEasting, hiveEasting - distanceToHives,
hiveEasting + distanceToHives),
northing = c(hiveNorthing, hiveNorthing - distanceToHives,
hiveNorthing + distanceToHives)))
coordinates(coordPanel) <- c("easting", "northing")
#proj4string(coordPanel) = CRS("+init=epsg:27700")
proj4string(coordPanel) = CRS("+init=epsg:26917") # Sponsler: our CRS is UTM 17N (EPSG:26917)
# prepare a raster with the extent; adapt to fit your coordinate system
numCoordPanel <- as.data.frame(coordPanel)
temp.rast <- raster(ncols = noCells, nrows = noCells)
extent(temp.rast) <- extent(c(numCoordPanel[2:3,1], numCoordPanel[2:3,2]))
#proj4string(temp.rast) = CRS("+init=epsg:27700")
proj4string(temp.rast) = CRS("+init=epsg:26917") # Sponsler: our CRS is UTM 17N (EPSG:26917)
# create a raster that will store the actual probability-visited data
total.temp.rast <- temp.rast
# only select tagged bees for calibration dances
calibDataAggBees <- calibDataAgg[!is.na(calibDataAgg$bee.id),]
# prepare the variables for the calibration model
N1 <- length(calibDataAggBees$duration)
x <- calibDataAggBees$distance
y <- calibDataAggBees$duration
K <- length(unique(calibDataAggBees$bee.id))
bee <- factor(calibDataAggBees$bee.id)
# loop through all the dances
for(i in 1:length(waggleData$dance.id)){
cat(paste(i, "of", length(waggleData$dance.id), "\n"))
# choose only the i^th dance
tempData <- waggleData[i,]
# prepare the variables for the prediction model
N2 <- length(tempData$duration)
x2 <- rep(NA, length(tempData$duration))
y2 <- tempData$duration
# load the model from file and submit the data
jags <- jags.model('ESM_3.jag',
data = list('x' = x, 'y' = y,
'N1' = N1, 'K' = K, 'bee' = bee, 'N2' = N2, 'x2' = x2, 'y2' = y2),
n.chains = 1,
n.adapt = 100)
# update for the burn-in
update(jags, 100000)
# sample from the posterior
samples <- coda.samples(jags, c('x2'), noJagsSamples, thin = thinning)
# save the samples in a handy variable
sim.distances <- samples[,'x2'][[1]]
# the 1000 draws have to be taken according to what is in the posterior samples for distance
sim.heading <- rvonmises(finalSampleSize, mu = tempData$heading,
kappa = 24.9, control.circular = list("radians"))
# calculate the coordinates from the vector with origin of the hives
rel.dance.easting <- as.numeric(hiveEasting + cos(-(sim.heading- pi/2))*sim.distances)
rel.dance.northing <- as.numeric(hiveNorthing + sin(-(sim.heading - pi/2))*sim.distances)
# save as points for further use
temp.points <- data.frame(cbind(dance.id = rep(tempData$dance.id, length(rel.dance.easting)),
easting = as.numeric(rel.dance.easting),
northing = as.numeric(rel.dance.northing)))
# save the points in a comma seperated value file
# csv points can be imported into GIS for further processing
write.csv(temp.points, paste("data/sim.dance_", tempData$dance.id, ".csv", sep = ""), row.names = FALSE)
if(i <= 1){ # on the first pass create a new file, else add the data to the existing file
write.csv(temp.points, "data/simAllDances.csv", row.names = FALSE)
}else{
# save the total counts
tempData2 <- read.csv("data/simAllDances.csv")
tempData2 <- rbind(tempData2, temp.points)
write.csv(tempData2, "data/simAllDances.csv", row.names = FALSE)
}
# georeference the points on the UK grid
coordinates(temp.points) = c("easting", "northing")
#proj4string(temp.points) = CRS("+init=epsg:27700")
proj4string(coordPanel) = CRS("+init=epsg:26917") # Sponsler: our CRS is UTM 17N (EPSG:26917)
# create a new raster with the temp.rast extent and sample the points on the raster / final sample size
# e.g. probability that a dance has been on a certain raster square
#temp.rast.UKGRID <- rasterize(temp.points, temp.rast, fun = "count", background = 0)/finalSampleSize
temp.rast.UTM17N <- rasterize(temp.points, temp.rast, fun = "count", background = 0)/finalSampleSize # Sponsler:
# convert the raster to a SpatialGridDataFrame
#g <- as(temp.rast.UKGRID, 'SpatialGridDataFrame')
g <- as(temp.rast.UTM17N, 'SpatialGridDataFrame') # Sponsler:
# save the file to disk (can be imported into GIS)
currentFileName <- paste("data/raster_", tempData$dance.id, ".asc", sep = "")
write.asciigrid(g, currentFileName)
if(i <= 1){ # on the first pass create a new file, else add the data to the existing file
#total.temp.rast <- temp.rast.UKGRID
total.temp.rast <- temp.rast.UTM17N # Sponsler:
}else{
# calculate the probability that a field has been visited
#total.temp.rast <- 1 - (1 - total.temp.rast)*(1 - temp.rast.UKGRID)
total.temp.rast <- 1 - (1 - total.temp.rast)*(1 - temp.rast.UTM17N)
}
}
# save the combined dances as one raster file ready to be imported in ArcGIS
total.temp.rast <- total.temp.rast$dance.id
g.total <- as(total.temp.rast, 'SpatialGridDataFrame')
write.asciigrid(g.total, "data/totalRaster.asc")
# for plotting, we can crop the extent to our needs
# prepare an georeferenced extent (in GIS terms) for the cropping
coordPanel.crop <- data.frame(cbind(easting = c(hiveEasting, hiveEasting - 10000,
hiveEasting + 10000),
northing = c(hiveNorthing, hiveNorthing - 10000,
hiveNorthing + 10000)))
coordinates(coordPanel.crop) <- c("easting", "northing")
#proj4string(coordPanel.crop) = CRS("+init=epsg:27700")
proj4string(coordPanel.crop) = CRS("+init=epsg:26917") # Sponsler:
# prepare a raster with the extent to crop the aerial photography
numCoordPanel.crop <- as.data.frame(coordPanel.crop)
crop.rast <- raster(ncols = noCells, nrows = noCells)
extent(crop.rast) <- extent(c(numCoordPanel.crop[2:3,1], numCoordPanel.crop[2:3,2]))
#proj4string(crop.rast) = CRS("+init=epsg:27700")
proj4string(crop.rast) = CRS("+init=epsg:26917") # Sponsler:
# we crop the data raster to size
new.data.rast <- crop(total.temp.rast, crop.rast)
writeRaster(new.data.rast, filename = "fsr_may5.tif", format = "GTiff", overwrite = T) # Sponsler: this geotiff can be loaded in QGIS to overlay on landscape layer
### PLOTTING WITHOUT AERIAL PHOTOGRAPHY
# choose colours of your liking, and symbol size that matched the figure
myAlpha <- c(0, seq(0.005, 0.5, 0.005) + 0.3)
myCols <- rev(rainbow(100, alpha = rev(myAlpha)))
charEx <- 2
# Sponsler: plot vector landscape layer -- this doesn't quite work yet; alignment of the two plots is off
#mooreman <- readOGR("/Users/dougsponsler/Documents/Research/CDRC_dance_analysis", "2015_sites_1500m_buffer_squares_site__MO_clip")
#plot(mooreman, col = c("yellow", "green", "gray"))
#par(new = T)
# plot the data raster
plot(new.data.rast, col = myCols, legend.width = charEx/2,
smallplot=c(.875,.9,0.33,0.9), axis.args = list(cex.axis = charEx/2))
# plot the hive location
#points(hiveEasting, hiveNorthing, pch = 17, col = "white", cex = 2*charEx)
points(hiveEasting, hiveNorthing, pch = 20, col = "black", cex = charEx/2) # Sponsler:
#points(hiveEasting, hiveNorthing, pch = 17, col = "red", cex = charEx)
# plot the feeder location
#points(533999, 109254, pch = 19, col = "white", cex = 2*charEx)
#points(533999, 109254, pch = 19, col = "black", cex = charEx)
# Sponsler: still need to work out the aerial photography part, though we may end up plotting with the vector layers instead
### PLOTTING WITH AERIAL PHOTOGRAPHY
# if you have access to aerial photography (i.e. a GeoTIFF), you can load
# that now and crop it to size
# new.raster <- brick("someAerialPhotography.tif")
# new.raster <- crop(new.raster, crop.rast)
# plot the aerial photography
# plotRGB(new.raster, r=1, g=2, b=3)
# plot a background for the colour scale
# polygon(c(hiveEasting + 50, hiveEasting + 50, hiveEasting + 250, hiveEasting + 250, hiveEasting + 50),
# c(hiveNorthing + 50, hiveNorthing + 1075, hiveNorthing + 1075, hiveNorthing + 50, hiveNorthing + 50), col = "white", border = "black", lwd = 2)
# plot the data raster
# plot(new.data.rast, add = TRUE, col = myCols, legend.width = charEx/2,
# smallplot=c(.875,.9,0.33,0.9), axis.args = list(cex.axis = charEx/2))
# plot the hive location
#points(hiveEasting, hiveNorthing, pch = 17, col = "white", cex = 2*charEx)
# points(hiveEasting, hiveNorthing, pch = 20, col = "black", cex = charEx/2) # Sponsler:
#points(hiveEasting, hiveNorthing, pch = 17, col = "red", cex = charEx)
# plot the feeder location
#points(533999, 109254, pch = 19, col = "white", cex = 2*charEx)
#points(533999, 109254, pch = 19, col = "black", cex = charEx)
# get rid of the simulation data in memory
rm(sim.distances)
rm(sim.heading)
rm(rel.dance.easting)
rm(rel.dance.northing)
rm(samples)
rm(temp.points)
rm(g.total)
rm(total.temp.rast)
rm(temp.rast.UKGRID)
rm(list = ls())
##### MAY 6 ###############################################################################################################
# loading packages needed in this script
library('circular') # for circular stats
library('rjags') # interface with JAGS
library('sp') # spatial stats, coordinates etc
library('rgdal') # convert between different coodrinate systems, spTransform
library('raster') # for plotting spatial data
library('png') # to save figures to jpeg
setwd("Users/dolezallab-macpro/Desktop/WaggleDance") # Sponsler: path to the working directory on my laptop
#read the calibration data from ESM_5.csv
calibDataAgg <- read.csv("ESM_5.csv", row.names = 1)
calibDataAgg$heading <- circular(calibDataAgg$heading,
type = "angle",
unit = "radian",
rotation = "clock",
zero = pi/2)
# prepare your own data, i.e. create or read from file a
# data frame with "duration" in sec and "heading" in radians
# DO ONE DANCE AT A TIME, else the prior will overwhelm the data
# as an example, we will use our own data for dances that have gone to
# feeder at 1 km, using only the first dance of every bee.
# dance ids to that feeder
#dance.ids <- c(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, 285, 286, 268, 269, 270)
# store the subset of dances going to that feeder in waggleData
#waggleData <- calibDataAgg[calibDataAgg$dance.id %in% dance.ids,]
waggleData <- read.csv("SureshDance_Thesis.csv") # Sponsler: path to our dance data
waggleData <- subset(waggleData, flag == 1 & day == 6) # Sponsler: a flag field removes empty or incomplete lines
# we only want the first dance of every individual bee, and we prepare a function to achieve that
#getFirstElement <- function(pVector){
#reslt <- NA
#if(length(pVector) > 0){
#reslt <- pVector[1]
#}
#reslt
#}