amt
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.
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 RasterLayer
.
data("sh_forest")
sh_forest
## class : RasterLayer
## dimensions : 720, 751, 540720 (nrow, ncol, ncell)
## resolution : 25, 25 (x, y)
## extent : 4304725, 4323500, 3437725, 3455725 (xmin, xmax, ymin, ymax)
## crs : +proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
## source : memory
## names : sh.forest
## values : 1, 2 (min, max)
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.
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.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.SpatialPolygons*
-object or sf
-object. The latter must contain POLYGON
s or MULTIPOLYGON
s 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
<- random_points(deer)
r1 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).
<- random_points(deer, n = 100)
r1 plot(r1)
Next, we can create random point within a home range, that we estimated before.
<- hr_mcp(deer)
hr <- random_points(hr, n = 500)
r1 plot(r1)
Here, we can also add the observed points:
<- hr_mcp(deer)
hr <- random_points(hr, n = 500, presence = deer)
r1 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_mcp(deer) %>% hr_isopleths() %>%
hr ::st_buffer(dist =3e4) # add a 30km buffer
sf<- random_points(hr, n = 500)
r1 plot(r1)
And we can also add the observed points.
<- hr_mcp(deer) %>% hr_isopleths() %>%
hr ::st_buffer(dist =3e4) # add a 30km buffer
sf<- random_points(hr, n = 500, presence = deer)
r1 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.
As the next step we have to extract the covariates at point. We can do this with extract_covariates
.
<- deer %>% random_points() %>%
rsf1 extract_covariates(sh_forest) %>%
mutate(forest = sh.forest == 1)
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")
.
%>% fit_rsf(case_ ~ forest) %>%
rsf1 summary()
##
## Call:
## stats::glm(formula = formula, family = stats::binomial(link = "logit"),
## data = data)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -0.5361 -0.4054 -0.4054 -0.4054 2.2538
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -2.45760 0.04403 -55.817 < 2e-16 ***
## forestTRUE 0.59044 0.07924 7.451 9.25e-14 ***
## ---
## 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: 5483.6 on 9084 degrees of freedom
## AIC: 5487.6
##
## Number of Fisher Scoring iterations: 5
::session_info() sessioninfo
## ─ Session info ───────────────────────────────────────────────────────────────
## setting value
## version R version 4.1.2 (2021-11-01)
## os Ubuntu 20.04.3 LTS
## system x86_64, linux-gnu
## ui X11
## language (EN)
## collate C
## ctype en_US.UTF-8
## tz Europe/Berlin
## date 2022-02-22
## pandoc 2.14.0.3 @ /usr/lib/rstudio/bin/pandoc/ (via rmarkdown)
##
## ─ Packages ───────────────────────────────────────────────────────────────────
## package * version date (UTC) lib source
## amt * 0.1.7 2022-02-22 [1] local
## assertthat 0.2.1 2019-03-21 [3] CRAN (R 4.1.1)
## backports 1.4.1 2021-12-13 [3] CRAN (R 4.1.2)
## bslib 0.3.1 2021-10-06 [5] CRAN (R 4.1.1)
## checkmate 2.0.0 2020-02-06 [3] CRAN (R 4.1.1)
## class 7.3-20 2022-01-13 [3] CRAN (R 4.1.2)
## classInt 0.4-3 2020-04-07 [3] CRAN (R 4.1.1)
## cli 3.1.1 2022-01-20 [3] CRAN (R 4.1.2)
## codetools 0.2-18 2020-11-04 [3] CRAN (R 4.1.2)
## colorspace 2.0-2 2021-06-24 [3] CRAN (R 4.1.1)
## crayon 1.4.2 2021-10-29 [3] CRAN (R 4.1.1)
## DBI 1.1.2 2021-12-20 [3] CRAN (R 4.1.2)
## digest 0.6.29 2021-12-01 [3] CRAN (R 4.1.2)
## dplyr * 1.0.7 2021-06-18 [3] CRAN (R 4.1.1)
## e1071 1.7-9 2021-09-16 [3] CRAN (R 4.1.1)
## ellipsis 0.3.2 2021-04-29 [3] CRAN (R 4.1.1)
## evaluate 0.14 2019-05-28 [3] CRAN (R 4.1.1)
## fansi 1.0.2 2022-01-14 [3] CRAN (R 4.1.2)
## farver 2.1.0 2021-02-28 [3] CRAN (R 4.1.1)
## fastmap 1.1.0 2021-01-25 [3] CRAN (R 4.1.1)
## generics 0.1.1 2021-10-25 [3] CRAN (R 4.1.1)
## ggforce 0.3.3 2021-03-05 [3] CRAN (R 4.1.1)
## ggplot2 * 3.3.5 2021-06-25 [3] CRAN (R 4.1.1)
## ggraph * 2.0.5 2021-02-23 [3] CRAN (R 4.1.1)
## ggrepel 0.9.1 2021-01-15 [3] CRAN (R 4.1.1)
## glue 1.6.1 2022-01-22 [3] CRAN (R 4.1.2)
## graphlayouts 0.8.0 2022-01-03 [3] CRAN (R 4.1.2)
## gridExtra 2.3 2017-09-09 [3] CRAN (R 4.1.1)
## gtable 0.3.0 2019-03-25 [3] CRAN (R 4.1.1)
## highr 0.9 2021-04-16 [3] CRAN (R 4.1.1)
## htmltools 0.5.2 2021-08-25 [3] CRAN (R 4.1.1)
## igraph 1.2.11 2022-01-04 [3] CRAN (R 4.1.2)
## jquerylib 0.1.4 2021-04-26 [3] CRAN (R 4.1.1)
## jsonlite 1.7.3 2022-01-17 [3] CRAN (R 4.1.2)
## KernSmooth 2.23-20 2021-05-03 [3] CRAN (R 4.1.2)
## knitr 1.37 2021-12-16 [3] CRAN (R 4.1.2)
## labeling 0.4.2 2020-10-20 [3] CRAN (R 4.1.1)
## lattice 0.20-45 2021-09-22 [6] CRAN (R 4.1.1)
## lifecycle 1.0.1 2021-09-24 [3] CRAN (R 4.1.1)
## lubridate 1.8.0 2021-10-07 [3] CRAN (R 4.1.1)
## magrittr 2.0.1 2020-11-17 [3] CRAN (R 4.1.1)
## MASS 7.3-55 2022-01-13 [3] CRAN (R 4.1.2)
## Matrix 1.4-0 2021-12-08 [6] CRAN (R 4.1.2)
## munsell 0.5.0 2018-06-12 [3] CRAN (R 4.1.1)
## pillar 1.6.4 2021-10-18 [3] CRAN (R 4.1.1)
## pkgconfig 2.0.3 2019-09-22 [3] CRAN (R 4.1.1)
## polyclip 1.10-0 2019-03-14 [3] CRAN (R 4.1.1)
## proxy 0.4-26 2021-06-07 [3] CRAN (R 4.1.1)
## purrr 0.3.4 2020-04-17 [3] CRAN (R 4.1.1)
## R6 2.5.1 2021-08-19 [3] CRAN (R 4.1.1)
## raster 3.5-15 2022-01-22 [3] CRAN (R 4.1.2)
## rbibutils 2.2.7 2021-12-07 [3] CRAN (R 4.1.2)
## Rcpp 1.0.8 2022-01-13 [3] CRAN (R 4.1.2)
## Rdpack 2.1.3 2021-12-08 [3] CRAN (R 4.1.2)
## rgdal 1.5-28 2021-12-15 [3] CRAN (R 4.1.2)
## rgeos 0.5-9 2021-12-15 [3] CRAN (R 4.1.2)
## rlang 0.4.12 2021-10-18 [3] CRAN (R 4.1.1)
## rmarkdown 2.11 2021-09-14 [3] CRAN (R 4.1.1)
## rstudioapi 0.13 2020-11-12 [3] CRAN (R 4.1.2)
## sass 0.4.0 2021-05-12 [3] CRAN (R 4.1.2)
## scales 1.1.1 2020-05-11 [3] CRAN (R 4.1.1)
## sessioninfo 1.2.2 2021-12-06 [3] CRAN (R 4.1.2)
## sf 1.0-5 2021-12-17 [3] CRAN (R 4.1.2)
## sp 1.4-6 2021-11-14 [3] CRAN (R 4.1.2)
## stringi 1.7.6 2021-11-29 [3] CRAN (R 4.1.2)
## stringr 1.4.0 2019-02-10 [3] CRAN (R 4.1.1)
## survival 3.2-13 2021-08-24 [6] CRAN (R 4.1.1)
## terra 1.5-12 2022-01-13 [3] CRAN (R 4.1.2)
## tibble 3.1.6 2021-11-07 [3] CRAN (R 4.1.2)
## tidygraph * 1.2.0 2020-05-12 [3] CRAN (R 4.1.1)
## tidyr 1.1.4 2021-09-27 [3] CRAN (R 4.1.1)
## tidyselect 1.1.1 2021-04-30 [3] CRAN (R 4.1.1)
## tweenr 1.0.2 2021-03-23 [3] CRAN (R 4.1.1)
## units 0.7-2 2021-06-08 [3] CRAN (R 4.1.1)
## utf8 1.2.2 2021-07-24 [3] CRAN (R 4.1.1)
## vctrs 0.3.8 2021-04-29 [3] CRAN (R 4.1.1)
## viridis 0.6.2 2021-10-13 [3] CRAN (R 4.1.1)
## viridisLite 0.4.0 2021-04-13 [3] CRAN (R 4.1.1)
## withr 2.4.3 2021-11-30 [3] CRAN (R 4.1.2)
## xfun 0.29 2021-12-14 [3] CRAN (R 4.1.2)
## yaml 2.2.1 2020-02-01 [3] CRAN (R 4.1.1)
##
## [1] /tmp/RtmpWI98rT/Rinst14b2c38667b80
## [2] /tmp/RtmpmbHhqG/temp_libpath9844cc8f32d
## [3] /home/jsigner/R/x86_64-pc-linux-gnu-library/4.1
## [4] /usr/local/lib/R/site-library
## [5] /usr/lib/R/site-library
## [6] /usr/lib/R/library
##
## ──────────────────────────────────────────────────────────────────────────────