amt
This vignette briefly introduces how one can fit a Step-Selection Function (SSF) 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. For a more through discussion see also Fieberg et al. 20201 and supplement B.
First we load the required libraries and the relocation data (called deer
)
library(lubridate)
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
In order to continue, we need a regular sampling rate. To check the current sampling rate, we use summarize_sampling_rate
:
summarize_sampling_rate(deer)
## # A tibble: 1 × 9
## min q1 median mean q3 max sd n unit
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int> <chr>
## 1 5.96 6.00 6.01 11.5 6.02 3924. 137. 825 hour
The median sampling rate is 6h, which is what we aimed for.
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 SSF we have to do some data preparation. First, we change from a point representation to a step representation, using the function steps_by_burst
, which in contrast to the steps
function accounts for bursts.
<- deer %>% steps_by_burst() ssf1
The generic function random_steps
provides a methods for a track_xy*
, where each observed step is paired with n_control
control steps (i.e., steps that share the same starting location but have different turn angles and step lengths). The distributions for drawing step lengths and turning angles are usually obtained by fitting known parametric distribution to the observed step length and turn angles.
The function random_steps
has seven arguments. For most use cases the defaults are just fine, but there might situation where the user wants to adjust some of the arguments. The arguments are:
x
: This is the track_xy*
for which the random steps are created. That is, for each step in x
n_control
random steps are created.n_control
: The number of random steps that should be created for each observed step.sl_distr
: This is the distribution of the step lengths. By default a gamma distribution is fit to the observed step lengths of the x
. But any amt_distr
is suitable here. 2ta_distr
: This is the turn angle distribution, with the default being a von Mises distribution.rand_sl
: These are the random step lengths, by default 1e5 random numbers from the distribution fitted in 33.rand_ta
: These are the random turn angles, by default 1e4 random numbers from the distribution fitted in 4.include_observed
: This argument is by default TRUE
and indicates if the observed steps should be included or not.In most situations the following code snippet should work4.
<- ssf1 %>% random_steps(n_control = 15) ssf1
todo
As a last step, we have to extract the covariates at the end point of each step. We can do this with extract_covariates
.
<- ssf1 %>% extract_covariates(sh_forest) ssf1
Since the forest layers is coded as 1 = forest
and 2 != forest
, we create a factor with appropriate levels. We also calculate the log of the step length and the cosine of the turn angle, which we may use later for a integrated step selection function.
<- ssf1 %>%
ssf1 mutate(forest = factor(sh.forest, levels = 1:2, labels = c("forest", "non-forest")),
cos_ta = cos(ta_),
log_sl = log(sl_))
Now all pieces are there to fit a SSF. We will use fit_clogit
, which is a wrapper around survival::clogit
.
<- ssf1 %>% fit_clogit(case_ ~ forest + strata(step_id_))
m0 <- ssf1 %>% fit_clogit(case_ ~ forest + forest:cos_ta + forest:log_sl + log_sl * cos_ta + strata(step_id_))
m1 <- ssf1 %>% fit_clogit(case_ ~ forest + forest:cos_ta + forest:log_sl + log_sl + cos_ta + strata(step_id_))
m2 summary(m0)
## Call:
## coxph(formula = Surv(rep(1, 12656L), case_) ~ forest + strata(step_id_),
## data = data, method = "exact")
##
## n= 12656, number of events= 791
##
## coef exp(coef) se(coef) z Pr(>|z|)
## forestnon-forest -0.5456 0.5795 0.1059 -5.152 2.58e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## exp(coef) exp(-coef) lower .95 upper .95
## forestnon-forest 0.5795 1.726 0.4709 0.7132
##
## Concordance= 0.531 (se = 0.007 )
## Likelihood ratio test= 25.73 on 1 df, p=4e-07
## Wald test = 26.54 on 1 df, p=3e-07
## Score (logrank) test = 26.67 on 1 df, p=2e-07
summary(m1)
## Call:
## coxph(formula = Surv(rep(1, 12656L), case_) ~ forest + forest:cos_ta +
## forest:log_sl + log_sl * cos_ta + strata(step_id_), data = data,
## method = "exact")
##
## n= 12624, number of events= 759
## (32 observations deleted due to missingness)
##
## coef exp(coef) se(coef) z Pr(>|z|)
## forestnon-forest 0.91288 2.49148 0.33487 2.726 0.00641 **
## log_sl 0.20543 1.22805 0.05134 4.001 6.30e-05 ***
## cos_ta -0.40320 0.66818 0.20330 -1.983 0.04734 *
## forestnon-forest:cos_ta -0.06846 0.93383 0.11754 -0.582 0.56029
## forestnon-forest:log_sl -0.27871 0.75676 0.05914 -4.712 2.45e-06 ***
## log_sl:cos_ta 0.01559 1.01571 0.03397 0.459 0.64630
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## exp(coef) exp(-coef) lower .95 upper .95
## forestnon-forest 2.4915 0.4014 1.2924 4.8029
## log_sl 1.2281 0.8143 1.1105 1.3581
## cos_ta 0.6682 1.4966 0.4486 0.9953
## forestnon-forest:cos_ta 0.9338 1.0709 0.7417 1.1758
## forestnon-forest:log_sl 0.7568 1.3214 0.6739 0.8498
## log_sl:cos_ta 1.0157 0.9845 0.9503 1.0857
##
## Concordance= 0.608 (se = 0.012 )
## Likelihood ratio test= 96.93 on 6 df, p=<2e-16
## Wald test = 96.96 on 6 df, p=<2e-16
## Score (logrank) test = 99.16 on 6 df, p=<2e-16
summary(m2)
## Call:
## coxph(formula = Surv(rep(1, 12656L), case_) ~ forest + forest:cos_ta +
## forest:log_sl + log_sl + cos_ta + strata(step_id_), data = data,
## method = "exact")
##
## n= 12624, number of events= 759
## (32 observations deleted due to missingness)
##
## coef exp(coef) se(coef) z Pr(>|z|)
## forestnon-forest 0.91853 2.50560 0.33442 2.747 0.00602 **
## log_sl 0.20526 1.22784 0.05134 3.998 6.38e-05 ***
## cos_ta -0.32146 0.72509 0.09794 -3.282 0.00103 **
## forestnon-forest:cos_ta -0.07174 0.93077 0.11729 -0.612 0.54078
## forestnon-forest:log_sl -0.28003 0.75576 0.05906 -4.741 2.12e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## exp(coef) exp(-coef) lower .95 upper .95
## forestnon-forest 2.5056 0.3991 1.3009 4.8258
## log_sl 1.2278 0.8144 1.1103 1.3578
## cos_ta 0.7251 1.3791 0.5984 0.8785
## forestnon-forest:cos_ta 0.9308 1.0744 0.7396 1.1713
## forestnon-forest:log_sl 0.7558 1.3232 0.6732 0.8485
##
## Concordance= 0.606 (se = 0.012 )
## Likelihood ratio test= 96.72 on 5 df, p=<2e-16
## Wald test = 96.47 on 5 df, p=<2e-16
## Score (logrank) test = 98.72 on 5 df, p=<2e-16
See the discussion in Fieberg et al 2020.
All steps described above, could easily be wrapped into one piped workflow:
<- deer %>%
m1 steps_by_burst() %>% random_steps(n = 15) %>%
extract_covariates(sh_forest) %>%
mutate(forest = factor(sh.forest, levels = 1:2, labels = c("forest", "non-forest")),
cos_ta = cos(ta_),
log_sl = log(sl_)) %>%
fit_clogit(case_ ~ forest + forest:cos_ta + forest:sl_ + sl_ * cos_ta + strata(step_id_))
summary(m1)
## Call:
## coxph(formula = Surv(rep(1, 12656L), case_) ~ forest + forest:cos_ta +
## forest:sl_ + sl_ * cos_ta + strata(step_id_), data = data,
## method = "exact")
##
## n= 12624, number of events= 759
## (32 observations deleted due to missingness)
##
## coef exp(coef) se(coef) z Pr(>|z|)
## forestnon-forest -0.1853638 0.8308020 0.1369323 -1.354 0.1758
## sl_ 0.0005926 1.0005927 0.0001477 4.011 6.04e-05 ***
## cos_ta -0.4711862 0.6242613 0.1098044 -4.291 1.78e-05 ***
## forestnon-forest:cos_ta -0.0332544 0.9672924 0.1173285 -0.283 0.7768
## forestnon-forest:sl_ -0.0008546 0.9991458 0.0001917 -4.458 8.27e-06 ***
## sl_:cos_ta 0.0003312 1.0003313 0.0001306 2.537 0.0112 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## exp(coef) exp(-coef) lower .95 upper .95
## forestnon-forest 0.8308 1.2037 0.6352 1.0866
## sl_ 1.0006 0.9994 1.0003 1.0009
## cos_ta 0.6243 1.6019 0.5034 0.7742
## forestnon-forest:cos_ta 0.9673 1.0338 0.7686 1.2174
## forestnon-forest:sl_ 0.9991 1.0009 0.9988 0.9995
## sl_:cos_ta 1.0003 0.9997 1.0001 1.0006
##
## Concordance= 0.606 (se = 0.013 )
## Likelihood ratio test= 101.5 on 6 df, p=<2e-16
## Wald test = 104.3 on 6 df, p=<2e-16
## Score (logrank) test = 109.1 on 6 df, p=<2e-16
::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)
## boot 1.3-28 2021-05-03 [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)
## circular 0.4-93 2017-06-29 [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)
## fitdistrplus 1.1-6 2021-09-28 [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)
## mvtnorm 1.1-3 2021-10-08 [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
##
## ──────────────────────────────────────────────────────────────────────────────
https://www.biorxiv.org/content/10.1101/2020.11.12.379834v4↩︎
See also ?fit_distr
.↩︎
Note, this possible because of the Glivenko-Cantelli theorem and works as long as the sample from the original distribution (the sample you provide here) is large enough.↩︎
And how it was implemented in amt
up to version 0.0.6. This should be backward compatible and not break existing code.↩︎