extract_covariates_var_time()
with multiple animals:Simulate some data: 10 animals over a year with monthly covariates. Note the covariates do not influence movement during the simulations. This purely for illustration.
library(raster)
library(lubridate)
library(amt)
set.seed(123)
trks <- tibble(
id = 1:10,
trk = map(1:10, ~ tibble(x = cumsum(rnorm(100)), y = cumsum(rnorm(100)),
ts = ymd_hm("2019-01-01 00:00") + hours(0:99)))
)
dat <- unnest(trks, cols = trk)
Next lets create some covariates.
r <- stack(map(1:15,
~ raster(xmn = -100, xmx = 100, ymn = -100, ymx = 100,
res = 1, vals = runif(4e4, ., . + 1))))
r <- setZ(r, ymd_hm("2019-01-01 00:00") + hours(seq(0, by = 10, len = 15)))
We have now 10 animals, each with 100 relocations (one per hour) and one env covariate which changes every 10 hours. Now we can extract the covariates at the end of each step within an time window (specified with max_time
).
trk <- make_track(dat, x, y, ts, id = id)
# For one animal
trk %>% filter(id == 1) %>%
extract_covariates_var_time(r, max_time = hours(10 * 24)) -> t1
t1$time_var_covar
## [1] 1.859966 1.859966 1.207689 1.205935 1.205935 1.081416 2.504437
## [8] 2.759418 2.759418 2.064030 2.759418 2.554582 2.772285 2.772285
## [15] 2.772285 2.466056 3.992470 3.231361 3.957009 3.271404 3.695507
## [22] 3.250728 3.637535 3.013321 3.445043 3.792285 4.032129 4.032129
## [29] 4.439059 4.026711 4.932416 4.686803 4.932416 4.292410 4.460516
## [36] 4.097073 5.089216 5.862778 5.717599 5.191560 5.191560 5.810020
## [43] 5.136051 5.821677 5.982182 5.708564 6.729128 6.279375 6.011149
## [50] 6.460835 6.011149 6.011149 6.922410 6.441271 6.441271 6.134686
## [57] 7.128916 7.994925 7.128916 7.128916 7.438483 7.128916 7.994925
## [64] 7.754318 7.971250 7.971250 8.249095 8.240756 8.787252 8.219207
## [71] 8.129793 8.249095 8.787252 8.327917 8.444819 8.787252 9.549586
## [78] 9.271491 9.026545 9.271491 9.950249 9.026545 9.271491 9.925456
## [85] 9.950249 9.925456 10.003776 10.003776 10.206241 10.296934 10.404394
## [92] 10.398105 10.398105 10.671474 10.119090 10.398105 11.319751 11.707586
## [99] 11.531466 11.301397
Now lets do the the same for an animal at once:
trk %>%
nest(data = -c(id)) %>%
mutate(data = map(data, ~ .x %>% extract_covariates_var_time(r, max_time = hours(10 * 24)))) %>%
unnest(cols = data) -> trk2
trk2
## # A tibble: 1,000 x 5
## id x_ y_ t_ time_var_covar
## <int> <dbl> <dbl> <dttm> <dbl>
## 1 1 -0.560 -0.710 2019-01-01 00:00:00 1.86
## 2 1 -0.791 -0.454 2019-01-01 01:00:00 1.86
## 3 1 0.768 -0.700 2019-01-01 02:00:00 1.21
## 4 1 0.839 -1.05 2019-01-01 03:00:00 1.21
## 5 1 0.968 -2.00 2019-01-01 04:00:00 1.21
## 6 1 2.68 -2.04 2019-01-01 05:00:00 1.08
## 7 1 3.14 -2.83 2019-01-01 06:00:00 2.50
## 8 1 1.88 -4.50 2019-01-01 07:00:00 2.76
## 9 1 1.19 -4.88 2019-01-01 08:00:00 2.76
## 10 1 0.746 -3.96 2019-01-01 09:00:00 2.06
## # … with 990 more rows
## R version 3.6.3 (2020-02-29)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 18.04.4 LTS
##
## Matrix products: default
## BLAS: /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3
## LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.2.20.so
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=de_DE.UTF-8 LC_COLLATE=C
## [5] LC_MONETARY=de_DE.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=de_DE.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=de_DE.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] amt_0.1.0 lubridate_1.7.4 raster_3.0-12 sp_1.4-1
## [5] knitr_1.28
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.4.6 compiler_3.6.3 pillar_1.4.3 tools_3.6.3
## [5] digest_0.6.25 evaluate_0.14 tibble_3.0.1.9000 lifecycle_0.2.0
## [9] lattice_0.20-40 pkgconfig_2.0.3 rlang_0.4.5 Matrix_1.2-17
## [13] cli_2.0.2 yaml_2.2.1 xfun_0.13 dplyr_0.8.5
## [17] stringr_1.4.0 vctrs_0.2.4 grid_3.6.3 tidyselect_1.0.0
## [21] glue_1.4.0 R6_2.4.1 fansi_0.4.1 survival_3.1-8
## [25] rmarkdown_2.0 purrr_0.3.4 tidyr_1.0.2 magrittr_1.5
## [29] codetools_0.2-16 htmltools_0.4.0 ellipsis_0.3.0 splines_3.6.3
## [33] assertthat_0.2.1 utf8_1.1.4 stringi_1.4.6 lazyeval_0.2.2
## [37] crayon_1.3.4