-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprepare_distance_sampling_data_v01.R
166 lines (134 loc) · 3.99 KB
/
prepare_distance_sampling_data_v01.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
library(here)
library(janitor)
library(tidyverse)
library(sf)
library(lubridate)
setwd(here::here("data"))
ds_shape <- sf::st_read(dsn = "./Shapefiles/DS", layer = "DS_10kmpersite")
min_coords <- sf::st_coordinates(sf::st_transform(ds_shape, 4326)) |>
tibble::as_tibble() |>
dplyr::group_by(L2) |>
dplyr::summarise(x = min(X),
y = min(Y))
ds_offset <- ds_shape |>
tibble::add_column(minX = min_coords$x) |>
dplyr::mutate(site = 1:length(unique(X10kmsiteID)) ,
area = ( as.numeric(sf::st_length(geometry)) * 1000 * 2 ) / 1E6,
region = ifelse(minX > 35.1, 1, 0)) |>
sf::st_drop_geometry() |>
dplyr::select(site, area, region)
ds <- readr::read_csv("Herbivore Utilization Complete.csv") |>
janitor::clean_names() |>
dplyr::filter(animal %in% c(
"Buffalo",
"Eland",
"Elephant",
"Giraffe",
"Grants",
"Hartebeest",
"Impala",
"Thomsons",
"Topi",
"Warthog",
"Waterbuck")) |>
dplyr::filter(! is.na(count)) |>
dplyr::filter(! count == 0) |>
# new base pipe doesn't have placeholder: https://tinyurl.com/2p8zarxr
(\(x) sf::st_as_sf(x,
coords = c("adj_easting", "adj_northing"),
crs = sf::st_crs(ds_shape)))()
ds_matrix <- sf::st_distance(ds, ds_shape)
# assign transect to each observation
ds$site <- apply(ds_matrix, 1, which.min)
# add corrected distances
ds$dst <- apply(ds_matrix, 1, min)
ds <- ds |>
dplyr::filter(dst <= 1000)
#-----------------------#
# Assign distance classes #
#-----------------------#
#Number of observations for distance sampling
nobs <- NULL
nobs[1] <- dim(ds)[1]
#ID for distance class
di <- seq(0, 1000, 25)
#Distance class
dclass <- rep(NA, nobs[1])
#Number of distance classes
nG <- length(di) - 1
#Minimum distance to assigned transect
dst <- ds$dst
for(i in 1:nobs[1]){
for(k in 1:nG){
if(di[k] < dst[i] && dst[i] <= di[k+1])
dclass[i] <- k
}
}
ds$dclass <- dclass
#Replicate
ds$reps[ds$territory == "North"] <- dplyr::filter(ds, territory == "North") |>
dplyr::group_by(year, month )|>
dplyr::group_indices()
ds$reps[ds$territory == "South"] <- dplyr::filter(ds, territory =="South") |>
dplyr::group_by(year, month) |>
dplyr::group_indices()
ds$reps[ds$territory == "West"] <- dplyr::filter(ds, territory == "West") |>
dplyr::group_by(year, month) |>
dplyr::group_indices()
final <- ds |>
sf::st_drop_geometry() |>
dplyr::mutate(date = lubridate::ymd(paste(year, month, day, sep = "-"))) |>
dplyr::select(date, animal, site, rep = reps, count, dclass) |>
dplyr::arrange(animal, site, rep) |>
dplyr::mutate(animal = factor(animal)) |>
dplyr::mutate(spec = as.numeric(animal)) |>
dplyr::select(date,
sp_name = animal,
sp = spec,
site,
rep,
gs = count,
dclass) |>
dplyr::group_by(sp, site, rep) |>
dplyr::mutate(ng = n())
site_key <- final |>
dplyr::ungroup() |>
dplyr::select(site, rep, date) |>
dplyr::distinct() |>
dplyr::group_by(site, rep) |>
dplyr::summarise(date = min(date))
sp_key <- final |>
dplyr::ungroup() |>
dplyr::select(sp, sp_name) |>
dplyr::distinct() |>
dplyr::mutate(sp_name = tolower(sp_name))
#Width of distance classes
v <- 25 # meters
# Transect half-width
b <- 1000 # meters
#Distance class midpoint ID
mdpt <- seq( v/2, b, v)
#area of transects (m^2)
area <- as.numeric( sf::st_length(ds_shape)*1000*2)
#set baseline unit as 1 km^2
offset <- area / 1E6
final2 <- expand.grid(
sp = sort(unique(final$sp)),
site = sort(unique(final$site)),
rep = sort(unique(final$rep))) |>
tibble::as_tibble() |>
dplyr::full_join(
dplyr::select(
dplyr::ungroup(final),
sp:ng)
) |>
dplyr::full_join(ds_offset) |>
dplyr::full_join(site_key) |>
dplyr::full_join(sp_key) |>
dplyr::mutate(ng = ifelse(is.na(ng) & (!is.na(date)), 0, ng))
save(
final2,
v,
b,
mdpt,
file = "distance_sampling_data_v01.RData")