Fitting Step-Selection Functions with amt

Johannes Signer

2020-03-22

About

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.

Getting the data ready

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

library(lubridate)
library(raster)
library(amt)
data("deer")
deer
## # A tibble: 826 x 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 x 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)

Prepare Data for SSF

Steps

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.

Control/random steps

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:

  1. 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.
  2. n_control: The number of random steps that should be created for each observed step.
  3. 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. 1
  4. ta_distr: This is the turn angle distribution, with the default being a von Mises distribution.
  5. rand_sl: These are the random step lengths, by default 1e5 random numbers from the distribution fitted in 32.
  6. rand_ta: These are the random turn angles, by default 1e4 random numbers from the distribution fitted in 4.
  7. include_observed: This argument is by default TRUE and indicates if the observed steps should be included or not.

The default situation

In most situations the following code snippet should work3.

A exponential distribution for step lengths

todo

Extract covariates

As a last step, we have to extract the covariates at the end point of each step. We can do this with extract_covariates.

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.

Fitting SSF

Now all pieces are there to fit a SSF. We will use fit_clogit, which is a wrapper around survival::clogit.

m0 <- ssf1 %>% fit_clogit(case_ ~ forest + strata(step_id_))
m1 <- ssf1 %>% fit_clogit(case_ ~ forest + forest:cos_ta + forest:log_sl + log_sl * cos_ta + strata(step_id_))
m2 <- ssf1 %>% fit_clogit(case_ ~ forest + forest:cos_ta + forest:log_sl + log_sl + cos_ta + strata(step_id_))
summary(m0)
## Call:
## coxph(formula = Surv(rep(1, 12144L), case_) ~ forest + strata(step_id_), 
##     data = data, method = "exact")
## 
##   n= 12144, number of events= 759 
## 
##                     coef exp(coef) se(coef)      z Pr(>|z|)    
## forestnon-forest -0.5284    0.5895   0.1092 -4.837 1.32e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                  exp(coef) exp(-coef) lower .95 upper .95
## forestnon-forest    0.5895      1.696    0.4759    0.7303
## 
## Concordance= 0.53  (se = 0.007 )
## Likelihood ratio test= 22.64  on 1 df,   p=2e-06
## Wald test            = 23.4  on 1 df,   p=1e-06
## Score (logrank) test = 23.48  on 1 df,   p=1e-06
summary(m1)
## Call:
## coxph(formula = Surv(rep(1, 12144L), case_) ~ forest + forest:cos_ta + 
##     forest:log_sl + log_sl * cos_ta + strata(step_id_), data = data, 
##     method = "exact")
## 
##   n= 12144, number of events= 759 
## 
##                             coef exp(coef) se(coef)      z Pr(>|z|)    
## forestnon-forest         0.66392   1.94240  0.32276  2.057 0.039683 *  
## log_sl                   0.16871   1.18378  0.04889  3.451 0.000559 ***
## cos_ta                  -0.41923   0.65756  0.20121 -2.084 0.037203 *  
## forestnon-forest:cos_ta -0.09176   0.91233  0.11766 -0.780 0.435477    
## forestnon-forest:log_sl -0.23071   0.79397  0.05661 -4.075 4.59e-05 ***
## log_sl:cos_ta            0.02218   1.02243  0.03354  0.661 0.508470    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                         exp(coef) exp(-coef) lower .95 upper .95
## forestnon-forest           1.9424     0.5148    1.0318    3.6565
## log_sl                     1.1838     0.8448    1.0756    1.3028
## cos_ta                     0.6576     1.5208    0.4433    0.9754
## forestnon-forest:cos_ta    0.9123     1.0961    0.7244    1.1490
## forestnon-forest:log_sl    0.7940     1.2595    0.7106    0.8871
## log_sl:cos_ta              1.0224     0.9781    0.9574    1.0919
## 
## Concordance= 0.606  (se = 0.012 )
## Likelihood ratio test= 90.7  on 6 df,   p=<2e-16
## Wald test            = 90.23  on 6 df,   p=<2e-16
## Score (logrank) test = 92.3  on 6 df,   p=<2e-16
summary(m2)
## Call:
## coxph(formula = Surv(rep(1, 12144L), case_) ~ forest + forest:cos_ta + 
##     forest:log_sl + log_sl + cos_ta + strata(step_id_), data = data, 
##     method = "exact")
## 
##   n= 12144, number of events= 759 
## 
##                             coef exp(coef) se(coef)      z Pr(>|z|)    
## forestnon-forest         0.67443   1.96292  0.32233  2.092  0.03640 *  
## log_sl                   0.16776   1.18266  0.04889  3.431  0.00060 ***
## cos_ta                  -0.30301   0.73860  0.09788 -3.096  0.00196 ** 
## forestnon-forest:cos_ta -0.09554   0.90888  0.11753 -0.813  0.41629    
## forestnon-forest:log_sl -0.23253   0.79252  0.05656 -4.111 3.94e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                         exp(coef) exp(-coef) lower .95 upper .95
## forestnon-forest           1.9629     0.5094    1.0436    3.6921
## log_sl                     1.1827     0.8456    1.0746    1.3016
## cos_ta                     0.7386     1.3539    0.6097    0.8948
## forestnon-forest:cos_ta    0.9089     1.1002    0.7219    1.1443
## forestnon-forest:log_sl    0.7925     1.2618    0.7094    0.8854
## 
## Concordance= 0.606  (se = 0.012 )
## Likelihood ratio test= 90.26  on 5 df,   p=<2e-16
## Wald test            = 89.45  on 5 df,   p=<2e-16
## Score (logrank) test = 91.58  on 5 df,   p=<2e-16
#AIC(m0$model)
#AIC(m1$model)
#AIC(m2$model)

Interpretation of coefficients

To be done.

A note on piping

All steps described above, could easily be wrapped into one piped workflow:

m1 <- deer %>% 
  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, 12144L), case_) ~ forest + forest:cos_ta + 
##     forest:sl_ + sl_ * cos_ta + strata(step_id_), data = data, 
##     method = "exact")
## 
##   n= 12144, number of events= 759 
## 
##                               coef  exp(coef)   se(coef)      z Pr(>|z|)
## forestnon-forest        -0.1378926  0.8711922  0.1399027 -0.986  0.32431
## sl_                      0.0006001  1.0006003  0.0001496  4.012 6.01e-05
## cos_ta                  -0.4406344  0.6436280  0.1124034 -3.920 8.85e-05
## forestnon-forest:cos_ta -0.1218231  0.8853050  0.1191259 -1.023  0.30648
## forestnon-forest:sl_    -0.0008928  0.9991076  0.0001956 -4.564 5.02e-06
## sl_:cos_ta               0.0003960  1.0003961  0.0001333  2.970  0.00298
##                            
## forestnon-forest           
## sl_                     ***
## cos_ta                  ***
## forestnon-forest:cos_ta    
## forestnon-forest:sl_    ***
## sl_:cos_ta              ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                         exp(coef) exp(-coef) lower .95 upper .95
## forestnon-forest           0.8712     1.1479    0.6623    1.1460
## sl_                        1.0006     0.9994    1.0003    1.0009
## cos_ta                     0.6436     1.5537    0.5164    0.8023
## forestnon-forest:cos_ta    0.8853     1.1296    0.7010    1.1181
## forestnon-forest:sl_       0.9991     1.0009    0.9987    0.9995
## sl_:cos_ta                 1.0004     0.9996    1.0001    1.0007
## 
## Concordance= 0.606  (se = 0.013 )
## Likelihood ratio test= 106  on 6 df,   p=<2e-16
## Wald test            = 108.1  on 6 df,   p=<2e-16
## Score (logrank) test = 113.2  on 6 df,   p=<2e-16

Session

devtools::session_info()
## ─ Session info ──────────────────────────────────────────────────────────
##  setting  value                       
##  version  R version 3.6.3 (2020-02-29)
##  os       Ubuntu 18.04.4 LTS          
##  system   x86_64, linux-gnu           
##  ui       X11                         
##  language en_US:en                    
##  collate  C                           
##  ctype    en_US.UTF-8                 
##  tz       Europe/Berlin               
##  date     2020-03-22                  
## 
## ─ Packages ──────────────────────────────────────────────────────────────
##  package      * version      date       lib
##  amt          * 0.0.8        2020-03-22 [1]
##  assertthat     0.2.1        2019-03-21 [3]
##  backports      1.1.5        2019-10-02 [3]
##  boot           1.3-24       2019-12-20 [6]
##  callr          3.4.2        2020-02-12 [3]
##  checkmate      2.0.0        2020-02-06 [3]
##  circular       0.4-93       2017-06-29 [3]
##  class          7.3-15       2019-01-01 [6]
##  classInt       0.4-2        2019-10-17 [3]
##  cli            2.0.2        2020-02-28 [3]
##  codetools      0.2-16       2018-12-24 [6]
##  colorspace     1.4-1        2019-03-18 [3]
##  crayon         1.3.4        2017-09-16 [3]
##  DBI            1.1.0        2019-12-15 [3]
##  desc           1.2.0        2018-05-01 [3]
##  devtools       2.2.0        2019-09-07 [3]
##  digest         0.6.25       2020-02-23 [3]
##  dplyr        * 0.8.5        2020-03-07 [3]
##  DT             0.9          2019-09-17 [3]
##  e1071          1.7-3        2019-11-26 [3]
##  ellipsis       0.3.0        2019-09-20 [3]
##  evaluate       0.14         2019-05-28 [3]
##  fansi          0.4.1        2020-01-08 [3]
##  farver         2.0.3        2020-01-16 [3]
##  fitdistrplus   1.0-14       2019-01-23 [3]
##  fs             1.3.1        2019-05-06 [3]
##  ggplot2      * 3.3.0        2020-03-05 [3]
##  glue           1.3.2        2020-03-12 [3]
##  gtable         0.3.0        2019-03-25 [3]
##  htmltools      0.4.0        2019-10-04 [3]
##  htmlwidgets    1.5.1        2019-10-08 [3]
##  KernSmooth     2.23-15      2015-06-29 [6]
##  knitr        * 1.25         2019-09-18 [3]
##  labeling       0.3          2014-08-23 [3]
##  lattice        0.20-40      2020-02-19 [6]
##  lazyeval       0.2.2        2019-03-15 [3]
##  lifecycle      0.2.0        2020-03-06 [3]
##  lsei           1.2-0        2017-10-23 [3]
##  lubridate    * 1.7.4        2018-04-11 [3]
##  magrittr       1.5          2014-11-22 [3]
##  MASS           7.3-51.5     2019-12-20 [6]
##  Matrix         1.2-17       2019-03-22 [6]
##  memoise        1.1.0        2017-04-21 [3]
##  munsell        0.5.0        2018-06-12 [3]
##  mvtnorm        1.1-0        2020-02-24 [3]
##  npsurv         0.4-0        2017-10-14 [3]
##  pillar         1.4.3        2019-12-20 [3]
##  pkgbuild       1.0.6        2019-10-09 [3]
##  pkgconfig      2.0.3        2019-09-22 [3]
##  pkgload        1.0.2        2018-10-29 [3]
##  prettyunits    1.1.1        2020-01-24 [3]
##  processx       3.4.2        2020-02-09 [3]
##  ps             1.3.2        2020-02-13 [3]
##  purrr          0.3.3        2019-10-18 [3]
##  R6             2.4.1        2019-11-12 [3]
##  raster       * 3.0-12       2020-01-30 [3]
##  Rcpp           1.0.4        2020-03-17 [3]
##  remotes        2.1.0        2019-06-24 [3]
##  rgdal          1.4-8        2019-11-27 [3]
##  rlang          0.4.5        2020-03-01 [3]
##  rmarkdown      2.0          2019-12-12 [3]
##  rprojroot      1.3-2        2018-01-03 [3]
##  scales         1.1.0        2019-11-18 [3]
##  sessioninfo    1.1.1        2018-11-05 [3]
##  sf             0.8-1        2020-01-28 [3]
##  sp           * 1.4-1        2020-02-28 [3]
##  stringi        1.4.6        2020-02-17 [3]
##  stringr        1.4.0        2019-02-10 [3]
##  survival       3.1-8        2019-12-03 [3]
##  testthat       2.3.2        2020-03-02 [3]
##  tibble         2.99.99.9014 2020-03-21 [3]
##  tidyr          1.0.2        2020-01-24 [3]
##  tidyselect     1.0.0        2020-01-27 [3]
##  units          0.6-6        2020-03-16 [3]
##  usethis        1.5.1        2019-07-04 [3]
##  utf8           1.1.4        2018-05-24 [3]
##  vctrs          0.2.4        2020-03-10 [3]
##  withr          2.1.2        2018-03-15 [3]
##  xfun           0.12         2020-01-13 [3]
##  yaml           2.2.1        2020-02-01 [3]
##  source                           
##  local                            
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.2)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.0)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.0)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.5.0)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.2)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.2)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.3)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.3)                   
##  CRAN (R 3.6.3)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  Github (tidyverse/tibble@b15afcd)
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.1)                   
##  CRAN (R 3.6.3)                   
## 
## [1] /tmp/RtmpyGZB20/Rinst228c279302c1
## [2] /tmp/RtmpoXjBeW/temp_libpath7c1879197d75
## [3] /home/jsigner/R/x86_64-pc-linux-gnu-library/3.6
## [4] /usr/local/lib/R/site-library
## [5] /usr/lib/R/site-library
## [6] /usr/lib/R/library

  1. See also ?fit_distr.

  2. 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.

  3. And how it was implemented in amt up to version 0.0.6. This should be backward compatible and not break existing code.