-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.CleaningData_Bocas2021-2022_gh.Rmd
More file actions
167 lines (127 loc) · 7.52 KB
/
1.CleaningData_Bocas2021-2022_gh.Rmd
File metadata and controls
167 lines (127 loc) · 7.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
---
title: "Cleaning data Bocas 2021-2022"
output: html_document
date: '2024-07-22'
author: "Camila Calderon"
---
## load packages
```{r setup}
pacman::p_load(tidyverse,data.table, move, lubridate, cowplot, ggplot2, doBy, dplyr, ggpubr, spatialrisk, rgdal, janitor, ISLR)
```
## Loading movebank data from 2021-2022
```{r data from movebank}
# download from movebank
me <- movebankLogin(username="", password="")
studyId <- getMovebankID("Greater spear-nosed bat (Phyllostomus hastatus) in Bocas del Toro 2021-2022",me)
batsmove<- getMovebankData(study=studyId, login=me)
batsdf <- as.data.frame(batsmove)
#unique(batsdf$tag_local_identifier)
```
## Changingm the names of some tag ids
```{r editing data, results='hide'}
# change the ST of the tag local identifier of some individuals to TS
batsdf$tag_local_identifier <- gsub('ST', 'TS', batsdf$tag_local_identifier)
unique(batsdf$tag_local_identifier)
```
# Setting timestamp as posixct class
```{r remove outliers, include=TRUE}
batsdf$timestamp <- as.POSIXct(batsdf$timestamp, format = "%Y-%m-%d %H:%M:%S", tz ="UTC")
# to check if there is outliers
plot(batsdf$location_long, batsdf$location_lat)
```
## Removing some individuals with no good data
```{r}
# splitting data frame to a list
bocas.split <- split(batsdf, batsdf$tag_local_identifier)
# plot to see individuals
plot(bocas.split[["0C506E35_C"]]$location_long,bocas.split[["0C506E35_C"]]$location_lat)
plot(bocas.split[["5C500E4E_C"]]$location_long,bocas.split[["5C500E4E_C"]]$location_lat)
plot(bocas.split[["31507235_C"]]$location_long,bocas.split[["31507235_C"]]$location_lat)
plot(bocas.split[["53507135_C"]]$location_long,bocas.split[["53507135_C"]]$location_lat)
plot(bocas.split[["38506D37_C"]]$location_long,bocas.split[["38506D37_C"]]$location_lat)
plot(bocas.split[["38500337_C"]]$location_long,bocas.split[["38500337_C"]]$location_lat)
plot(bocas.split[["PH_TS_030"]]$location_long,bocas.split[["PH_TS_030"]]$location_lat)
# remove individuals where data is very incomplete
bocas.splitnew <- bocas.split[names(bocas.split) %in% c("5C500E4E_C","PH_TS_030", "31507235_C", "53507135_C", "32501535_C")== FALSE]
# create data frame
batsbocas.clean <- do.call(rbind, bocas.splitnew)
# split data frame
batsbocas.clean$ID <- paste0(batsbocas.clean$tag_local_identifier, "_", date(batsbocas.clean$timestamp))
allbats.batday <- split(batsbocas.clean, batsbocas.clean$ID)
# remove more individual days from march where data was not good
batsclean <- allbats.batday[names(allbats.batday) %in% c("0C506E35_C_2021-12-10", "0C506E35_C_2021-12-11", "22507B4E_D_2022-02-01", "2C500235_D_2022-02-13", "38506D37_D_2022-02-03", "0D501435_C_2021−12−09", "2D507235_G_2022-01-31", "37507537_D_2022-02-01", "38506D37_C_2021-12-09", "38506737_G_2022-01-30", "39500E37_D_2022-02-01", "39500E37_D_2022-02-02", "PH_TS_016_2022-03-13", "PH_TS_016_2022-03-15", "PH_TS_018_2022-03-17", "PH_TS_018_2022-03-18", "PH_TS_018_2022-03-19", "PH_TS_024_2022-03-08", "PH_TS_024_2022-03-11", "PH_TS_024_2022-03-12", "PH_TS_024_2022-03-13", "PH_TS_024_2022-03-14", "PH_TS_024_2022-03-15", "PH_TS_039_2022-03-15", "PH_TS_049_2022-03-11", "PH_TS_049_2022-03-12", "PH_TS_049_2022-03-13", "PH_TS_052_2022-03-13", "PH_TS_052_2022-03-14", "PH_TS_056_2022-03-10", "PH_TS_056_2022-03-12", "PH_TS_056_2022-03-15", "PH_TS_056_2022-03-16", "PH_TS_056_2022-03-17", "PH_TS_062_2022-03-15", "PH_TS_062_2022-03-16", "PH_TS_062_2022-03-17", "PH_TS_072_2022-03-14", "PH_TS_072_2022-03-15", "PH_TS_072_2022-03-17", "PH_TS_072_2022-03-18", "PH_TS_074_2022-03-13", "PH_TS_074_2022-03-14", "PH_TS_081_2022-03-14", "PH_TS_081_2022-03-15", "PH_TS_081_2022-03-17", "PH_TS_083_2022-03-12", "PH_TS_083_2022-03-14", "PH_TS_083_2022-03-15", "PH_TS_085_2022-03-09", "PH_TS_085_2022-03-10", "PH_TS_085_2022-03-13", "PH_TS_085_2022-03-15", "PH_TS_085_2022-03-16", "PH_TS_096_2022-03-13", "PH_TS_096_2022-03-14", "PH_TS_096_2022-03-15", "PH_TS_098_2022-03-10", "PH_TS_098_2022-03-11", "PH_TS_100_2022-03-12", "PH_TS_100_2022-03-13", "PH_TS_100_2022-03-14", "PH_TS_100_2022-03-15", "PH_TS_103_2022-03-16", "PH_TS_113_2022-03-11", "PH_TS_113_2022-03-13", "PH_TS_113_2022-03-14", "PH_TS_113_2022-03-15", "PH_TS_114_2022-03-08", "PH_TS_114_2022-03-10", "PH_TS_121_2022-03-12","PH_TS_121_2022-03-13", "PH_TS_121_2022-03-15")== FALSE]
batsclean.df <- do.call(rbind, batsclean)
```
## Downsampling data from 2021 from the wet season
Now I am downsampling GPS data from December 2021 that was collected every 30 seconds. I will down sample to every 2 minutes to match the sampling rate from the data from February
```{r resampling data from december, results=FALSE}
# order by ID and timestamp
batsclean.df <- batsclean.df[order(batsclean.df$tag_local_identifier, batsclean.df$timestamp),]
# take data only from December
bats_wet <- batsclean.df %>%
filter(date(timestamp)>"2021-12-01" & date(timestamp)<"2021-12-31")
# split by ID
bats_wet_lst<- split(bats_wet, bats_wet$ID)
library(amt)
# convert data frame to track object
resample <- lapply(bats_wet_lst, function(x){
mk_track(tbl=x, all_cols=T,
.x=location_long, .y=location_lat, crs = 32617,
.t=timestamp, order_by_ts = T, check_duplicates = T)
})
# resample every two minutes
resample_2 <- lapply(resample, function(x){
track_resample(x, rate = minutes(2), tolerance = seconds(1), start = 1)
})
# check if resampling went well
lapply(resample_2, function(x){
ggplot(aes(x=x_, y=y_), data=x) + geom_point() + ggtitle(x$ID)
})
# convert to dataframe again
batsdfwet <- do.call(rbind.data.frame,resample_2)
batsdfwet <- as.data.frame(batsdfwet)
names(batsdfwet)[1] <- "location_long"
names(batsdfwet)[2] <- "location_lat"
names(batsdfwet)[3] <- "timestamp"
# remove burst columns
batsdfwet <- batsdfwet[,-64]
# data frame without wet season
batsclean_nowet <- batsclean.df %>%
filter(date(timestamp)>"2021-12-31")
# join to large data set of all bacas
bats_clean <- rbind(batsclean_nowet,batsdfwet)
# order data frame by id and timestamp
bats_clean[order(bats_clean$tag_local_identifier, bats_clean$timestamp),]
# it runs if the data is downloaded from movebank
movebats <- move(x=bats_clean$location_long, y=bats_clean$location_lat,
time=as.POSIXct(bats_clean$timestamp, format="%Y-%m-%d %H:%M:%S", tz="UTC"),
data=bats_clean, proj=CRS("+proj=longlat +ellps=WGS84"),
animal=bats_clean$tag_local_identifier, sensor="GPS")
#"proj=utm +zone=17 +ellps=GRS80 +datum=NAD83 +units=m +no_defs"
n.indiv(movebats)
move::plot(movebats)
# initial movement parameters pre-cleaning.
moveList <- lapply(split(movebats), function(myInd){
datechange <- c(0, abs(diff(as.numeric(as.factor(date(myInd@timestamps-(12*60*60)))))))
myInd$BatDay <- cumsum(datechange)+1
myInd$tlag <- c(NA,timeLag(myInd, units="secs"))
myInd$step <- c(NA, move::distance(myInd))
myInd$tag_ground_speed <- myInd$ground_speed
myInd$ground_speed <- c(NA, move::speed(myInd))
myInd$angle <- c(NA, angle(myInd))
return(myInd)
})
movebats <- moveStack(moveList, forceTz="UTC")
movebats<- spTransform(movebats, CRS("+proj=utm +zone=17 +datum=WGS84"))
crds <- as.data.frame(movebats@coords)
movebats$x <- crds$coords.x1
movebats$y <- crds$coords.x2
# movebats <- spTransform(movebats, CRS("+proj=longlat +datum=WGS84"))
batsdf_new<- as.data.frame(movebats)
# create a column with id and batday
batsdf_new$ID_batday <- paste(batsdf_new$tag_local_identifier, batsdf_new$BatDay, sep="_")
#create column with cave id
batsdf_new$cave <- sapply(strsplit(batsdf_new$group_id,split = '-'), "[", 2)
#saving data
save(batsdf_new, file="~/ownCloud/PhDLife/P.hastatus/Thesis/paper1/BiologyLetters/data/BatsBocas_nooutliers.RData")
```