jmsigner / amt

37 stars 13 forks source link

Issue in random_steps function - Error: Expecting a single value: [extent=0]. #85

Closed leilafouda closed 1 year ago

leilafouda commented 1 year ago

Hello!

I am trying to run the random_steps() function on individual animal movement tracks in UTM.

I recently updated the package and am now getting two different errors:

Either: Error: Expecting a single value: [extent=0]. or Error in out[[1]] : subscript out of bounds

I tried to run the same process on the pre-loaded data from the amt vignette and Fieberg et al 2021

Vignette:

data(sh)
tr2 <- sh %>% filter(complete.cases(.)) %>% 
  mutate(
    ts = as.POSIXct(lubridate::ymd(day) + lubridate::hms(time)), 
    id = "Animal 1", 
    month = lubridate::month(ts)
  ) %>% 
  filter(!duplicated(ts)) %>% 
  make_track(x_epsg31467, y_epsg31467, ts, id = id, month = month, 
             crs = 'epsg:31467')

transform_coords(tr2, 'epsg:4326')

tr2 <- tr2 %>% mutate(sl_ = step_lengths(.))
summary(tr2$sl_)

summarize_sampling_rate(tr2)
tr3 <- tr2 %>% track_resample(rate = hours(6), tolerance = minutes(20))

tr3_burst <- tr3 %>% steps_by_burst()
tr3_burst_ssf1 <- tr3_burst %>% random_steps(n_control = 20)

Fieberg et al 2021

fisher <- amt_fisher
landuse <- amt_fisher_covar$landuse
elevation <- amt_fisher_covar$elevation
popden <- amt_fisher_covar$popden

dat <- fisher %>% 
  filter(name == "Lupe") %>% 
  arrange(t_)

# data(dat)
ssf_dat <- dat %>% 
  track_resample(rate = minutes(2), tolerance = seconds(20)) %>% 
  steps_by_burst() %>% 
  random_steps() %>% 
  extract_covariates(landuse, where = "both") %>% 
  extract_covariates(elevation, where = "both") %>% 
  extract_covariates(popden, where = "both") %>% 
  mutate(elevation_start = scale(elevation_start), 
         elevation_end = scale(elevation_end),
         popden_start = scale(popden_start),
         popden_end = scale(popden_end),
         landuseC_start = reclass_landuse(landuse_start), 
         landuseC_end = reclass_landuse(landuse_end), 
         forest_start = landuseC_start == "forest",
         forest_end = landuseC_end == "forest",
         cos_ta_ = cos(ta_), 
         log_sl_ = log(sl_)) %>%
 filter(!is.na(ta_))

With both above examples, I get: Error: Expecting a single value: [extent=0].

I recently updated my packages: install.packages(c("broom", "class", "codetools", "colorspace", "dplyr", "e1071", "effectsize", "fansi", "fontawesome", "forcats", "fs", "future", "gargle", "ggrepel", "gsl", "insight", "knitr", "lubridate", "markdown", "MASS", "nlme", "parameters", "Rcpp", "rmarkdown", "Rmpfr", "sass", "sourcetools", "spatial", "spatstat", "spatstat.explore", "spatstat.geom", "spatstat.linnet", "spatstat.model", "spatstat.random", "terra", "tidyr", "tinytex", "utf8", "vctrs", "vroom", "xfun", "yaml"))

and amt.

The Error in out[[1]] : subscript out of bounds appeared when I played around with the track_resample making it too long too short!

Would you know the source and a fix for either of these errors?

Thank you for your time!

leilafouda commented 1 year ago

Just to note that when the package version from CRAN (0.1.7) is used rather than the current development version from GitHub the random_steps() function works but anything that was updated to use terra rather than raster has reverted to utilising raster functions and features!

jmsigner commented 1 year ago

Thanks for reporting. I am preparing the new CRAN submission and will fix the problems you encounted.

leilafouda commented 1 year ago

That is great thank you @jmsigner! I look forward to it. Is there a rough timeline?

jmsigner commented 1 year ago

Hi @leilafouda, in the current Github version it worked for me, would you mind giving it an other try?

library(tidyverse)
library(amt)
#> 
#> Attaching package: 'amt'
#> The following object is masked from 'package:stats':
#> 
#>     filter
data(sh)
tr2 <- sh %>% filter(complete.cases(.)) %>% 
  mutate(
    ts = as.POSIXct(lubridate::ymd(day) + lubridate::hms(time)), 
    id = "Animal 1", 
    month = lubridate::month(ts)
  ) %>% 
  filter(!duplicated(ts)) %>% 
  make_track(x_epsg31467, y_epsg31467, ts, id = id, month = month, 
             crs = 'epsg:31467')

transform_coords(tr2, 'epsg:4326')
#> # A tibble: 1,493 × 5
#>       x_    y_ t_                  id       month
#>  * <dbl> <dbl> <dttm>              <chr>    <dbl>
#>  1  9.89  54.1 2008-03-30 00:01:47 Animal 1     3
#>  2  9.89  54.1 2008-03-30 06:00:54 Animal 1     3
#>  3  9.89  54.1 2008-03-30 12:01:47 Animal 1     3
#>  4  9.89  54.1 2008-03-30 18:01:24 Animal 1     3
#>  5  9.89  54.1 2008-03-30 18:25:56 Animal 1     3
#>  6  9.89  54.1 2008-03-30 18:26:05 Animal 1     3
#>  7  9.89  54.1 2008-03-30 18:26:14 Animal 1     3
#>  8  9.89  54.1 2008-03-30 18:26:24 Animal 1     3
#>  9  9.89  54.1 2008-03-30 18:26:33 Animal 1     3
#> 10  9.89  54.1 2008-03-30 18:38:01 Animal 1     3
#> # … with 1,483 more rows

tr2 <- tr2 %>% mutate(sl_ = step_lengths(.))
summary(tr2$sl_)
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
#>    0.00   35.01  105.33  249.07  297.75 4727.86       1

summarize_sampling_rate(tr2)
#> # A tibble: 1 × 9
#>      min    q1 median  mean    q3   max    sd     n unit 
#>    <dbl> <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl> <int> <chr>
#> 1 0.0025  2.00   2.01  6.34  6.00 3924.  102.  1492 hour
tr3 <- tr2 %>% track_resample(rate = hours(6), tolerance = minutes(20))

tr3_burst <- tr3 %>% steps_by_burst()
tr3_burst_ssf1 <- tr3_burst %>% random_steps(n_control = 20)
tr3_burst_ssf1
#> # A tibble: 15,939 × 12
#>    burst_     x1_      x2_     y1_      y2_    sl_    ta_ t1_                
#>  *  <dbl>   <int>    <dbl>   <int>    <dbl>  <dbl>  <dbl> <dttm>             
#>  1      1 3558513 3558564  5999055 5999146   104.   3.00  2008-03-30 06:00:54
#>  2      1 3558513 3558670. 5999055 5999867.  827.  -2.97  2008-03-30 06:00:54
#>  3      1 3558513 3558610. 5999055 5998970.  129.   1.22  2008-03-30 06:00:54
#>  4      1 3558513 3558675. 5999055 5999002.  170.   1.62  2008-03-30 06:00:54
#>  5      1 3558513 3558693. 5999055 5999112.  189.   2.24  2008-03-30 06:00:54
#>  6      1 3558513 3558280. 5999055 5998793.  350.  -0.360 2008-03-30 06:00:54
#>  7      1 3558513 3558478. 5999055 5999003.   62.2 -0.223 2008-03-30 06:00:54
#>  8      1 3558513 3558302. 5999055 5998836.  304.  -0.399 2008-03-30 06:00:54
#>  9      1 3558513 3559020. 5999055 6000316. 1359.   3.13  2008-03-30 06:00:54
#> 10      1 3558513 3558678. 5999055 5999176.  204.   2.57  2008-03-30 06:00:54
#> # … with 15,929 more rows, and 4 more variables: t2_ <dttm>, dt_ <drtn>,
#> #   case_ <lgl>, step_id_ <int>

Created on 2023-02-12 with reprex v2.0.2

leilafouda commented 1 year ago

Hi @jmsigner, sorry for my delay in getting back to you. It is now working thank you so much for the update.