-
Notifications
You must be signed in to change notification settings - Fork 0
/
06_attempt-defer.R
302 lines (219 loc) · 9.13 KB
/
06_attempt-defer.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
## Determining reproductive attempts or deferrals
## 2022-08-18
## Author: S. Cunningham
library(tidyverse)
library(sp)
library(adehabitatLT)
theme_set(theme_minimal())
#### Point-specific GPS locations ####
## Ornitela: Read files and summarize
orn.files <- list.files(path="data/GPSdata/Ornitela/", pattern=".csv", all.files=TRUE, full.names=TRUE)
orn <- lapply(orn.files, FUN=read.csv, header=TRUE, stringsAsFactors=FALSE)
for (i in 1:length(orn)) {
temp <- orn[[i]]
temp <- temp[,-c(1,3)]
# Remove missing points
temp <- temp[temp$Latitude>0,]
# Change time zone for North America
if (temp$device_id[1]==17701 | temp$device_id[1]==17709 | temp$device_id[1]==17719 |
temp$device_id[1]==17744 | temp$device_id[1]==17828 | temp$device_id[1]==17829) {
# Timestamp
temp <- unite(temp, "timestamp", 2:3, sep=" ", remove=TRUE)
# Convert to Central Time
temp$timestamp <- as.POSIXct(temp$timestamp, tz="UTC", format="%Y-%m-%d %H:%M:%S")
temp$timestamp <- format(temp$timestamp, tz="America/Chicago", usetz=TRUE)
# pull out dates and times
temp$timestamp <- as.character(temp$timestamp)
tdmat <- as.data.frame(matrix(unlist(strsplit(temp$timestamp, " ")), ncol=3, byrow=TRUE))[,1:2]
names(tdmat) <- c("date", "time")
temp <- cbind(temp, tdmat)
temp <- temp[,c(1,15,16,9,10)]
} else {
names(temp)[2:3] <- c("date", "time")
temp <- temp[,c(1:3,10,11)]
}
# Subset to ~31 July
temp$julian <- as.numeric(format(as.Date(temp$date), "%j"))
temp <- temp[temp$julian>=121 & temp$julian<=212,]
# Convert to equidistant projection
xy <- temp[,c(5,4)]
spdf <- SpatialPointsDataFrame(coords=xy, data=temp, proj4string=CRS("+proj=longlat +datum=WGS84"))
spdf <- spTransform(spdf, CRS("+proj=aeqd +lat_0=90 +lon_0=0"))
temp <- data.frame(spdf)
names(temp)[7:8] <- c("x_coord", "y_coord")
# Calculate median latitude and longitude
medY <- temp %>% group_by(date) %>% summarise(medlat=median(y_coord))
# Join median y coord
temp <- left_join(temp, medY, by="date")
# Store in the list
orn[[i]] <- temp
}
# Convert list to data frame
orn <- do.call(rbind, orn)
# Rename Jay's birds
orn$device_id <- as.character(orn$device_id)
orn$device_id[orn$device_id=="17701"] <- "RP20F"
orn$device_id[orn$device_id=="17709"] <- "RP19F"
orn$device_id[orn$device_id=="17719"] <- "RP17F"
orn$device_id[orn$device_id=="17744"] <- "LM39F"
orn$device_id[orn$device_id=="17828"] <- "RP23F"
orn$device_id[orn$device_id=="17829"] <- "RP22F"
## CTT: Read files and summarize
ctt.files <- list.files(path="data/GPSdata/CTT/", pattern=".csv", all.files=TRUE, full.names=TRUE)
ctt <- lapply(ctt.files, FUN=read.csv, header=TRUE, stringsAsFactors=FALSE)
for (i in 1:length(ctt)) {
temp <- ctt[[i]]
temp <- temp[,c(22, 4:6)]
# Timestamp
names(temp) <- c("device_id", "timestamp", "Latitude", "Longitude")
temp$device_id <- gsub("_", "", temp$device_id)
# Convert to Central Time
temp$timestamp <- as.POSIXct(temp$timestamp, tz="UTC", format="%Y-%m-%d %H:%M:%S")
temp$timestamp <- format(temp$timestamp, tz="America/Chicago", usetz=TRUE)
N.order <- order(temp$timestamp, decreasing=FALSE)
temp <- temp[N.order,]
# pull out dates and times
temp$timestamp <- as.character(temp$timestamp)
tdmat <- as.data.frame(matrix(unlist(strsplit(temp$timestamp, " ")), ncol=3, byrow=TRUE))[,1:2]
names(tdmat) <- c("date", "time")
temp <- cbind(temp, tdmat)
temp <- temp[,c(1,5,6,3,4)]
# Subset to ~31 July
temp$julian <- as.numeric(format(as.Date(temp$date), "%j"))
temp <- temp[temp$julian>=121 & temp$julian<=212,]
# Convert to equidistant projection
xy <- temp[,c(5,4)]
spdf <- SpatialPointsDataFrame(coords=xy, data=temp, proj4string=CRS("+proj=longlat +datum=WGS84"))
spdf <- spTransform(spdf, CRS("+proj=aeqd +lat_0=90 +lon_0=0"))
temp <- data.frame(spdf)
names(temp)[7:8] <- c("x_coord", "y_coord")
# Calculate median latitude and longitude
medY <- temp %>% group_by(date) %>% summarise(medlat=median(y_coord))
# Join median y coord
temp <- left_join(temp, medY, by="date")
# Store in the list
ctt[[i]] <- temp
}
# Convert list to data frame
ctt <- do.call(rbind, ctt)
gps <- rbind(orn, ctt)
names(gps)[1] <- "animal_id"
gps <- gps[,-9]
# Create year column and remove individuals that have data in >1 year
gps$year <- format(as.Date(gps$date), "%Y")
gps <- unite(gps, "key", c(1,10), sep="_", remove=FALSE)
gps <- gps[gps$key!="RP01F_2016" & gps$key!="RP15F_2018",]
# Calculate difference from each point to the median point
gps <- mutate(gps, latdiff=abs(y_coord-medlat))
sdLat <- gps %>% group_by(animal_id, date) %>% summarize(dld=sd(latdiff))
# Plot difference from daily median point
un.id <- unique(sdLat$animal_id)
for (i in 1:length(un.id)) {
temp <- sdLat[sdLat$animal_id==un.id[i],]
plot(as.Date(temp$date), temp$dld, type="l", pch=16, main=un.id[i], ylim=c(0, 2500))
abline(h=25.4, col="red")
# abline(h=89.4, col="red", lty=3)
}
## Examine daily median ODBA ##
# Read in summarized ACC data
acc <- read.csv("files_for_models/daily_odba_behavior.csv")
# get rid of extra columns
acc <- acc[,c(1:4,18)]
# subset to May-July
acc <- acc[acc$julian>=121 & acc$julian<=212,]
# Plot median ODBA
un.id <- unique(acc$animal_id)
for (i in 1:length(un.id)) {
temp <- acc[acc$animal_id==un.id[i],]
plot(as.Date(temp$date), temp$median.odba, type="l", pch=16, main=un.id[i], ylim=c(0, 1))
# abline(h=25.4, col="red")
}
# Join ACC and latitude SD
sdLat <- left_join(sdLat,acc, by=c("animal_id", "date"))
sdLat <- sdLat[,c(1,4,2,5,3,6)]
names(sdLat)[5] <- "sdDIST"
# Log-transform ODBA and SD of latitude
sdLat$lnSDdist <- log(sdLat$sdDIST)
sdLat$lnODBA <- log(sdLat$median.odba)
# Replicate Fig. 4 from Shreven et al. 2021
un.id <- unique(sdLat$animal_id)
for (i in 1:length(un.id)) {
temp <- sdLat[sdLat$animal_id==un.id[i],]
p <- ggplot(temp, aes(x=lnSDdist, y=lnODBA, col=julian)) + geom_point(size=3) +
ggtitle(un.id[i]) +
coord_cartesian(xlim=c(-1, 15), ylim=c(-4.5, 0))
print(p)
}
ggplot(sdLat, aes(x=lnSDdist, y=lnODBA, color=animal_id)) + geom_point() +
coord_cartesian(xlim=c(-1, 15))
## Assign defer/attempt for birds with collars
acc <- read.csv("files_for_models/daily_odba_behavior.csv")
acc <- acc[acc$tag!="EOBS",]
# Add new column
acc$defer <- 0
acc$defer[acc$animal_id=="17769" | acc$animal_id=="17814" |
acc$animal_id=="RP23F" | acc$animal_id=="LM39F" | acc$animal_id=="RP19F" |
acc$animal_id=="RP20F" | acc$animal_id=="17812"] <- 1
acc <- acc[,c(1,8,19)]
acc <- distinct(acc)
write_csv(acc, "files_for_models/attempt_defer_collars.csv")
#### e-obs geese ####
acc <- read.csv("files_for_models/daily_odba_behavior.csv")
# what happens if compare ODBA to distance from previous day's location?
acc <- acc[acc$year==2012 | acc$year==2013,]
acc <- acc[,c(1:6,18)]
un.id <- unique(acc$animal_id)
eobs <- data.frame()
for (i in 1:length(un.id)) {
temp <- acc[acc$animal_id==un.id[i],]
xy <- temp[,c(6,5)]
spdf <- SpatialPointsDataFrame(coords=xy, data=temp, proj4string=CRS("+proj=longlat +datum=WGS84"))
spdf <- spTransform(spdf, CRS("+proj=aeqd +lat_0=90 +lon_0=0"))
spdf$date <- as.POSIXct(spdf$date)
# Create ltraj object
rawtraj <- as.ltraj(coordinates(spdf), date=spdf$date, id=spdf$animal_id, burst=spdf$key, typeII=TRUE)
# Check if locations are regularly spaced
adehabitatLT::is.regular(rawtraj)
# Regularize trajectories, round values
refda <- min(spdf$date)
tag <- setNA(rawtraj, refda,1,units="day")
ttraj <- sett0(tag, refda,1,units="day")
ttraj <- mindistkeep(ttraj, 5)
tag <- ld(ttraj)
tag$date <- as.character(tag$date)
tag <- tag[,c(3,1,2,4:6)]
names(tag)[2:3] <- c("x_coord", "y_coord")
tag$date <- gsub(" 00:00:00", "", tag$date)
tag$julian <- as.numeric(format(as.Date(tag$date), "%j"))
temp <- left_join(temp, tag, by="julian")
temp <- temp[temp$julian>=121 & temp$julian<=212,]
temp <- temp[,c(1:7,9:13)]
names(temp)[3] <- "date"
eobs <- rbind(eobs, temp)
}
# Are there missing locations
eobs[is.na(eobs$dy),]
eobs <- eobs[!is.na(eobs$dy),]
# Log-transform ODBA and difference in latitude
eobs$lnDIST <- log(abs(eobs$dist))
eobs$lnDISTy <- log(abs(eobs$dy))
eobs$lnODBA <- log(eobs$median.odba)
un.id <- unique(eobs$animal_id)
for (i in 1:length(un.id)) {
temp <- eobs[eobs$animal_id==un.id[i],]
p <- ggplot(temp, aes(x=lnDISTy, y=lnODBA, col=julian)) + geom_point(size=3) +
ggtitle(un.id[i]) +
coord_cartesian(xlim=c(-5, 15), ylim=c(-5, 0))
print(p)
}
ggplot(eobs, aes(x=lnDISTy, y=lnODBA, col=animal_id)) + geom_point(size=3) +
coord_cartesian(xlim=c(0, 15))
ggplot(eobs, aes(x=julian, y=median.odba, col=animal_id)) + geom_line()
ggplot(eobs, aes(x=julian, y=abs(dy), col=animal_id)) + geom_line() +
coord_cartesian(ylim=c(0, 2000))
ggplot(eobs, aes(x=x_coord, y=y_coord, col=animal_id)) + geom_line() +
coord_cartesian(ylim=c(-1750000, -1000000), xlim=c(-2100000, -1500000)) +
theme(panel.border=element_rect(color="black", fill=NA))
b1 <- eobs[eobs$animal_id=='2838',]
quantile(b1$lnDIST, probs=0.25)
ggplot(b1, aes(x=julian, y=lnDIST)) + geom_line() + geom_hline(aes(yintercept=5.62))