-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivity-patterns.Rmd
233 lines (181 loc) · 6.11 KB
/
activity-patterns.Rmd
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
---
title: "Activity patterns"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Activity patterns}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
This vignette demonstrates how to infer and plot species activity patterns.
```{r setup, message=FALSE, results='hide'}
library(camtrapviz)
library(dplyr)
library(ggplot2)
library(activity)
library(ggiraph)
# library(hms)
```
## Import and prepare data
```{r}
data(recordTableSample, package = "camtrapR")
# Convert hours to times format
recordTableSample <- recordTableSample |>
mutate(Time = chron::times(Time))
# Convert dates to POSIX
recordTableSample <- recordTableSample |>
mutate(DateTimeOriginal = as.POSIXct(DateTimeOriginal,
tz = "Etc/GMT-8"))
```
We also need the coordinates if we want to use solar time (not run here):
```{r}
# data(camtraps, package = "camtrapR")
#
# camtraps_sf <- sf::st_as_sf(camtraps,
# coords = c("utm_x", "utm_y"),
# crs = 32650)
# # Reproject in WGS84 (a.k.a. EPSG:4326)
# camtraps_sf <- sf::st_transform(camtraps_sf, 4326)
#
# # Get coordinates of centroid
# sf::st_combine(camtraps_sf) |>
# sf::st_centroid() |>
# sf::st_coordinates(camtraps_sf)
# # X Y
# # [1,] 117.2227 5.479598
```
Then, we need to convert time to radians (and possibly solar time too, i.e. a time in radians where times are transformed relative to the sunrise and sunset times).
```{r}
# Convert times to radians
recordTableSample <- recordTableSample |>
mutate(time_radians = as.numeric(Time)*2*pi,
.after = Time)
solartime_rec <- solartime(recordTableSample$DateTimeOriginal,
lon = 117.2227, # mean longitude
lat = 5.479598, # mean latitude
tz = 8)
recordTableSample <- recordTableSample |>
mutate(time_solar = solartime_rec$solar,
.after = Time)
```
For this example, we will use the species PBE from the example dataset.
```{r}
# Select the desired species
PBE_records <- recordTableSample[recordTableSample$Species == "PBE", ]
```
## Plot histogram of observed data
First, let's plot the observed data.
By default `freq` is `TRUE` and the histogram bar height represents the observed number of individuals in this category.
```{r}
plot_activity(dfrec = PBE_records,
time_dfrec = "Time",
unit = "clock")
```
Using `interactive = TRUE`, the graph can also be made interactive with `ggiraph`.
```{r}
p <- plot_activity(dfrec = PBE_records,
time_dfrec = "Time",
unit = "clock",
interactive = TRUE)
girafe(ggobj = p)
```
It is also possible to plot the density by setting `freq` to `FALSE`. In that case, the bar area represents the frequency of individuals in the category. Hence, the total area under the histogram is 1.
```{r}
p <- plot_activity(dfrec = PBE_records,
time_dfrec = "Time",
unit = "clock",
freq = FALSE,
interactive = TRUE)
girafe(ggobj = p)
```
The scale can also be set to radians.
```{r}
p <- plot_activity(dfrec = PBE_records,
time_dfrec = "Time",
unit = "radians",
freq = FALSE,
interactive = TRUE)
girafe(ggobj = p)
```
## Infer activity patterns
We fit a density function using the `activity` package. For that, we use the function `fitact` that adjusts a density curve using von Mises functions as kernel.
This function takes as input at least a vector of times (in radians):
```{r}
vm <- activity::fitact(PBE_records$time_radians)
```
This object is of class `actmod` and contains slots:
+ `@data` (original data)
+ `@wt`, `@bw` and `@adj` (slots related to the model parameters)
+ `@pdf` which is the probability density function for $x$ in $[0; 2\pi]$
+ `@act` which is the proportion of time spent active (between 0 and 1)
```{r}
class(vm)
getClass("actmod")
```
## Plot density curve
To plot the density curve, we need to extract the data frame corresponding to the probability density function from the `actmod` object.
```{r}
pdf_vm <- as.data.frame(vm@pdf)
```
```{r}
plot_activity(dffit = pdf_vm,
time_dffit = "x",
y_fit = "y",
unit = "radians",
freq = FALSE)
```
Plotting the predicted counts is also possible. In that case, the integral under the curve **scaled to hours** (even if the scale is in radians) represents the number of observations.
```{r}
plot_activity(dffit = pdf_vm,
time_dffit = "x",
y_fit = "y",
unit = "radians",
freq = TRUE,
n = nrow(PBE_records))
```
It is also possible to plot the scale in hours:
```{r}
plot_activity(dffit = pdf_vm,
time_dffit = "x",
y_fit = "y",
unit = "clock",
freq = TRUE,
n = nrow(PBE_records))
```
## Plot density curve and histogram
It is also possible to plot the data and the density, combining various parameters illustrated before (radians or hours, frequency or count...) Below is an example:
```{r}
p <- plot_activity(dffit = pdf_vm,
time_dffit = "x",
y_fit = "y",
dfrec = PBE_records,
time_dfrec = "Time",
unit = "clock",
freq = TRUE,
interactive = TRUE)
girafe(ggobj = p)
```
## Infer activity patterns with sun times
We can also use the sun-adjusted times to infer the activity times:
```{r}
vm_sun <- activity::fitact(PBE_records$time_solar)
```
```{r}
pdf_vm_sum <- as.data.frame(vm_sun@pdf)
```
```{r}
p <- plot_activity(dffit = pdf_vm_sum,
time_dffit = "x",
y_fit = "y",
dfrec = PBE_records,
time_dfrec = "Time",
unit = "clock",
freq = TRUE,
interactive = TRUE)
girafe(ggobj = p)
```