jmsigner / amt

37 stars 13 forks source link

extract_covariates from raster with where = "both" throws Error in `[<-`: ! Can't recycle `x_all` (size 2) to size 0. #89

Open kerencrum opened 1 year ago

kerencrum commented 1 year ago

I am able to to do an extract_covariates for where = "start" and where = "end" but it throws an error when I try to do where = "both". I've been following the code in the Format and Generate Random Steps Appendix B in SSF Examples and it appears that "both" becomes a problem after I've applied the steps_by_burst() function to my track. The extract covariate with "Both" works on my tracks prior to that step. I had used this code a few months ago without issue, so I'm not sure why it's not working this time, although I did install the lasted amt package update today.

My work around has been to do the following:

ssf_dat <- tk_1637 %>% 
  track_resample(rate = minutes(30), tolerance = minutes(5)) %>% 
  steps_by_burst() %>% 
  random_steps() %>%
  extract_covariates(VEDUCont, where = 'start') %>%
  rename(Band_1_start = Band_1) %>%
  extract_covariates(VEDUCont, where = 'end') %>%
  rename(Band_1_end = Band_1) %>%
  mutate(VEDU_start = scale(Band_1_start), 
         VEDU_end = scale(Band_1_end),
         cos_ta_ = cos(ta_), 
         log_sl_ = log(sl_)
  ) %>% 
  filter(!is.na(ta_))
jmsigner commented 1 year ago

Thank @kerencrum for reporting. Would you mind double-checking the following example:

library(amt)
#> 
#> Attaching package: 'amt'
#> The following object is masked from 'package:stats':
#> 
#>     filter
library(tidyverse)

data(deer)
forest <- get_sh_forest()

deer |> 
  slice(1:10) |> 
  steps_by_burst() |> 
  random_steps() |> 
  extract_covariates(forest, where = "both")
#> # A tibble: 88 × 14
#>    burst_      x1_      x2_      y1_      y2_    sl_    ta_ t1_                
#>     <dbl>    <dbl>    <dbl>    <dbl>    <dbl>  <dbl>  <dbl> <dttm>             
#>  1      1 4314053. 4314105. 3445768. 3445859. 104.    3.00  2008-03-30 06:00:54
#>  2      1 4314053. 4313387. 3445768. 3445558. 698.   -0.883 2008-03-30 06:00:54
#>  3      1 4314053. 4313923. 3445768. 3445743. 132.   -0.997 2008-03-30 06:00:54
#>  4      1 4314053. 4314045. 3445768. 3445713.  56.3   0.238 2008-03-30 06:00:54
#>  5      1 4314053. 4314045. 3445768. 3445780.  14.1  -2.15  2008-03-30 06:00:54
#>  6      1 4314053. 4314050. 3445768. 3445767.   3.21 -0.632 2008-03-30 06:00:54
#>  7      1 4314053. 4314172. 3445768. 3446558. 799.   -2.91  2008-03-30 06:00:54
#>  8      1 4314053. 4314055. 3445768. 3445911. 142.   -2.78  2008-03-30 06:00:54
#>  9      1 4314053. 4313975. 3445768. 3445762.  78.0  -1.11  2008-03-30 06:00:54
#> 10      1 4314053. 4313991. 3445768. 3445732.  71.3  -0.657 2008-03-30 06:00:54
#> # ℹ 78 more rows
#> # ℹ 6 more variables: t2_ <dttm>, dt_ <drtn>, case_ <lgl>, step_id_ <dbl>,
#> #   forest_start <dbl>, forest_end <dbl>

Created on 2023-04-19 with reprex v2.0.2

kerencrum commented 1 year ago

Hello,

I was unable to run the code as you requested because the get_sh_forest() throws the following error: Error: 'unwrap' is not an exported object from 'namespace:terra' I did realize though that I was running an older version of amt on my computer, even though I thought I had updated it. Unfortunately updating the amt package and terra packages didn't fix the get_sh_forest() or my original issue with extract_covariates with where = "both"

> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22621)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] arcgisbinding_1.0.1.244 rgdal_1.5-28            raster_3.5-15           sp_1.4-6                sf_1.0-6               
 [6] terra_1.5-12            lubridate_1.9.2         forcats_1.0.0           stringr_1.5.0           dplyr_1.1.2            
[11] purrr_1.0.1             readr_2.1.4             tidyr_1.3.0             tibble_3.2.1            ggplot2_3.4.2          
[16] tidyverse_2.0.0         amt_0.2.1.0