Resource Selection Functions (RSF) with amt

Johannes Signer

2023-03-28

About

This vignette briefly introduces how one can fit a Resource-Selection Function (RSF) with the amt package. We will be using the example data of one red deer from northern Germany and one covariate: a forest cover map.

Getting the data ready

First we load the required libraries and the relocation data (called deer)

library(amt)
data("deer")
deer
## # A tibble: 826 × 4
##          x_       y_ t_                  burst_
##  *    <dbl>    <dbl> <dttm>               <dbl>
##  1 4314068. 3445807. 2008-03-30 00:01:47      1
##  2 4314053. 3445768. 2008-03-30 06:00:54      1
##  3 4314105. 3445859. 2008-03-30 12:01:47      1
##  4 4314044. 3445785. 2008-03-30 18:01:24      1
##  5 4313015. 3445858. 2008-03-31 00:01:23      1
##  6 4312860. 3445857. 2008-03-31 06:01:45      1
##  7 4312854. 3445856. 2008-03-31 12:01:11      1
##  8 4312858. 3445858. 2008-03-31 18:01:55      1
##  9 4312745. 3445862. 2008-04-01 00:01:24      1
## 10 4312651. 3446024. 2008-04-01 06:00:54      1
## # … with 816 more rows

Next, we have to get the environmental covariates. A forest layer is included in the package. Note, that this a regular SpatRast.

sh_forest <- get_sh_forest()

Prepare Data for RSF

Random Points

Before fitting a RSF we have to do some data preparation. We have to generate random points, points that we think the animal could have used. The random points define the availability domain. In amt the function random_points is designed to do just that. The function can be used in 3 different ways, depending to the type of object that is passed to the function call.

  1. A track_* (such as the deer object) can be passed to the function random_points. The function then calculates a home range (the home-range estimator can be controlled with argument hr). Within this home range n random points are generated. The default value of n is ten times the number of present points.
  2. If a hr-object (i.e., the result of a home-range estimation in amt) is passed to random_points, points are generated within the home range. This allows to generate random points within any home range that was previously estimated in amt. Note, that this could be a home range of multiple animals. In this case, the function random_points has one additional argument called presence. This argument takes a trk_* with the presence points and adds these points for convenience to the random points.
  3. A SpatialPolygons*-object or sf-object. The latter must contain POLYGONs or MULTIPOLYGONs as features. This can be useful in situation where a home range needs to be buffered, or when other geographical features are considered as the availability domain. As before, this method for random_points also takes the argument presence to optionally add the observed points to the output.

Lets now illustrate the three different situations. First we take random points from a track_xy

r1 <- random_points(deer)
plot(r1)

With the argument n we can control the number of random points (remember that the default is ten times as many points as we observed points).

r1 <- random_points(deer, n = 100)
plot(r1)

Next, we can create random point within a home range, that we estimated before.

hr <- hr_mcp(deer)
r1 <- random_points(hr, n = 500)
plot(r1)

Here, we can also add the observed points:

hr <- hr_mcp(deer)
r1 <- random_points(hr, n = 500, presence = deer)
plot(r1)

Finally, we can work with the home range and for example a buffer and then generate random points within the this new polygon.

hr <- hr_mcp(deer) |> hr_isopleths() |> 
  sf::st_buffer(dist =3e4) # add a 30km buffer
r1 <- random_points(hr, n = 500)
plot(r1)

And we can also add the observed points.

hr <- hr_mcp(deer) |> hr_isopleths() |> 
  sf::st_buffer(dist =3e4) # add a 30km buffer
r1 <- random_points(hr, n = 500, presence = deer)
plot(r1)

Of course we are not restricted to the sf::st_buffer function. All geometric operations from the sf package can be used to generate arbitrarily complex availability domains.

Extract covariates

As the next step we have to extract the covariates at point. We can do this with extract_covariates.

rsf1 <- deer |> random_points() |> 
  extract_covariates(sh_forest) 

Fitting RSF

Now all pieces are there to fit a RSF. We will use fit_rsf, which is just a wrapper around stats::glm with family = binomial(link = "logit").

rsf1 |> fit_rsf(case_ ~ forest) |> 
  summary()
## 
## Call:
## stats::glm(formula = formula, family = stats::binomial(link = "logit"), 
##     data = data)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -0.5292  -0.4068  -0.4068  -0.4068   2.2507  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept) -2.45008    0.04404  -55.63  < 2e-16 ***
## forest       0.55484    0.07915    7.01 2.38e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 5535.8  on 9085  degrees of freedom
## Residual deviance: 5489.5  on 9084  degrees of freedom
## AIC: 5493.5
## 
## Number of Fisher Scoring iterations: 5

Session

sessioninfo::session_info()
## ─ Session info ───────────────────────────────────────────────────────────────
##  setting  value
##  version  R version 4.2.2 Patched (2022-11-10 r83330)
##  os       Ubuntu 22.04.2 LTS
##  system   x86_64, linux-gnu
##  ui       X11
##  language (EN)
##  collate  C
##  ctype    en_US.UTF-8
##  tz       Europe/Berlin
##  date     2023-03-28
##  pandoc   2.19.2 @ /usr/lib/rstudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
## 
## ─ Packages ───────────────────────────────────────────────────────────────────
##  package      * version  date (UTC) lib source
##  amt          * 0.2.1.0  2023-03-28 [1] local
##  backports      1.4.1    2021-12-13 [5] CRAN (R 4.2.0)
##  bslib          0.4.2    2022-12-16 [3] CRAN (R 4.2.2)
##  cachem         1.0.7    2023-02-24 [5] CRAN (R 4.2.2)
##  checkmate      2.1.0    2022-04-21 [5] CRAN (R 4.2.0)
##  class          7.3-21   2023-01-23 [6] CRAN (R 4.2.2)
##  classInt       0.4-9    2023-02-28 [3] CRAN (R 4.2.2)
##  cli            3.6.0    2023-01-09 [5] CRAN (R 4.2.2)
##  codetools      0.2-19   2023-02-01 [6] CRAN (R 4.2.2)
##  colorspace     2.1-0    2023-01-23 [5] CRAN (R 4.2.2)
##  data.table     1.14.8   2023-02-17 [3] CRAN (R 4.2.2)
##  DBI            1.1.3    2022-06-18 [5] CRAN (R 4.2.1)
##  digest         0.6.31   2022-12-11 [3] CRAN (R 4.2.2)
##  dplyr        * 1.1.0    2023-01-29 [5] CRAN (R 4.2.2)
##  e1071          1.7-13   2023-02-01 [3] CRAN (R 4.2.2)
##  evaluate       0.20     2023-01-17 [5] CRAN (R 4.2.2)
##  fansi          1.0.4    2023-01-22 [5] CRAN (R 4.2.2)
##  farver         2.1.1    2022-07-06 [5] CRAN (R 4.2.1)
##  fastmap        1.1.1    2023-02-24 [5] CRAN (R 4.2.2)
##  generics       0.1.3    2022-07-05 [5] CRAN (R 4.2.1)
##  ggforce        0.4.1    2022-10-04 [3] CRAN (R 4.2.2)
##  ggplot2      * 3.4.1    2023-02-10 [3] CRAN (R 4.2.2)
##  ggraph       * 2.1.0    2022-10-09 [3] CRAN (R 4.2.2)
##  ggrepel        0.9.3    2023-02-03 [3] CRAN (R 4.2.2)
##  glue           1.6.2    2022-02-24 [5] CRAN (R 4.2.0)
##  graphlayouts   0.8.4    2022-11-24 [3] CRAN (R 4.2.2)
##  gridExtra      2.3      2017-09-09 [3] CRAN (R 4.2.2)
##  gtable         0.3.1    2022-09-01 [5] CRAN (R 4.2.1)
##  highr          0.10     2022-12-22 [3] CRAN (R 4.2.2)
##  htmltools      0.5.4    2022-12-07 [5] CRAN (R 4.2.2)
##  igraph         1.4.0    2023-02-10 [3] CRAN (R 4.2.2)
##  jquerylib      0.1.4    2021-04-26 [3] CRAN (R 4.2.2)
##  jsonlite       1.8.4    2022-12-06 [3] CRAN (R 4.2.2)
##  KernSmooth     2.23-20  2021-05-03 [6] CRAN (R 4.2.0)
##  knitr          1.42     2023-01-25 [5] CRAN (R 4.2.2)
##  labeling       0.4.2    2020-10-20 [5] CRAN (R 4.2.0)
##  lattice        0.20-45  2021-09-22 [6] CRAN (R 4.2.0)
##  lifecycle      1.0.3    2022-10-07 [5] CRAN (R 4.2.1)
##  lubridate      1.9.2    2023-02-10 [3] CRAN (R 4.2.2)
##  magrittr       2.0.3    2022-03-30 [5] CRAN (R 4.2.0)
##  MASS           7.3-58.2 2023-01-23 [6] CRAN (R 4.2.2)
##  Matrix         1.5-3    2022-11-11 [6] CRAN (R 4.2.2)
##  munsell        0.5.0    2018-06-12 [3] CRAN (R 4.2.2)
##  pillar         1.8.1    2022-08-19 [5] CRAN (R 4.2.1)
##  pkgconfig      2.0.3    2019-09-22 [3] CRAN (R 4.2.2)
##  polyclip       1.10-4   2022-10-20 [3] CRAN (R 4.2.2)
##  proxy          0.4-27   2022-06-09 [3] CRAN (R 4.2.2)
##  purrr          1.0.1    2023-01-10 [5] CRAN (R 4.2.2)
##  R6             2.5.1    2021-08-19 [5] CRAN (R 4.2.0)
##  rbibutils      2.2.13   2023-01-13 [3] CRAN (R 4.2.2)
##  Rcpp           1.0.10   2023-01-22 [5] CRAN (R 4.2.2)
##  Rdpack         2.4      2022-07-20 [3] CRAN (R 4.2.2)
##  rlang          1.0.6    2022-09-24 [5] CRAN (R 4.2.1)
##  rmarkdown      2.20     2023-01-19 [5] CRAN (R 4.2.2)
##  rstudioapi     0.14     2022-08-22 [5] CRAN (R 4.2.1)
##  sass           0.4.5    2023-01-24 [5] CRAN (R 4.2.2)
##  scales         1.2.1    2022-08-20 [5] CRAN (R 4.2.1)
##  sessioninfo    1.2.2    2021-12-06 [3] CRAN (R 4.2.2)
##  sf             1.0-9    2022-11-08 [3] CRAN (R 4.2.2)
##  survival       3.5-3    2023-02-12 [3] CRAN (R 4.2.2)
##  terra          1.7-12   2023-02-21 [3] Github (rspatial/terra@b79b4b0)
##  tibble         3.1.8    2022-07-22 [5] CRAN (R 4.2.2)
##  tidygraph    * 1.2.3    2023-02-01 [3] CRAN (R 4.2.2)
##  tidyr          1.3.0    2023-01-24 [5] CRAN (R 4.2.2)
##  tidyselect     1.2.0    2022-10-10 [5] CRAN (R 4.2.1)
##  timechange     0.2.0    2023-01-11 [5] CRAN (R 4.2.2)
##  tweenr         2.0.2    2022-09-06 [3] CRAN (R 4.2.2)
##  units          0.8-1    2022-12-10 [3] CRAN (R 4.2.2)
##  utf8           1.2.3    2023-01-31 [5] CRAN (R 4.2.2)
##  vctrs          0.5.2    2023-01-23 [5] CRAN (R 4.2.2)
##  viridis        0.6.2    2021-10-13 [5] CRAN (R 4.2.0)
##  viridisLite    0.4.1    2022-08-22 [5] CRAN (R 4.2.1)
##  withr          2.5.0    2022-03-03 [5] CRAN (R 4.2.0)
##  xfun           0.37     2023-01-31 [5] CRAN (R 4.2.2)
##  yaml           2.3.7    2023-01-23 [5] CRAN (R 4.2.2)
## 
##  [1] /tmp/RtmpEE1aXl/Rinst11aff69feb4c3
##  [2] /tmp/RtmpTZF65G/temp_libpathf4f475febbc9
##  [3] /home/jsigner/R/x86_64-pc-linux-gnu-library/4.2
##  [4] /usr/local/lib/R/site-library
##  [5] /usr/lib/R/site-library
##  [6] /usr/lib/R/library
## 
## ──────────────────────────────────────────────────────────────────────────────