jmsigner / amt

38 stars 13 forks source link

random_steps producing steps far outside data extent #126

Open camkie27 opened 3 days ago

camkie27 commented 3 days ago

Hi there,

I am attempting to conduct iSSF on data and am hitting a roadblock when trying to produce random steps. My data are in EPSG 4326, with long/lat coordinates usually beginning with -97.xxx and 26.xxx, respectively. I've checked that my crs of all of my objects remains consistent up to the point of using random_steps, and then the random step coordinates begin with numbers nowhere close to my data - some beginning with -108 or 15, for example. I've included sample code below that reproduces my problem. I have amt version ‘0.3.0.0’

#example data:
location.long=c(-97.47371, -97.47388, -97.47392, -97.47395, -97.47394, -97.47413, -97.47411, -97.47414)
location.lat=c(26.52136, 26.52141, 26.52137, 26.52169, 26.52171, 26.52156, 26.52159, 26.52156)
timestamp=c("2022-06-25 00:00:00", "2022-06-25 05:00:00", "2022-06-25 10:00:00", "2022-06-25 15:00:00", "2022-06-30 20:00:00", "2022-07-01 01:00:00", "2022-07-01 06:00:00", "2022-07-01 11:00:00")
timestamp<-as.POSIXct(timestamp, format = "%Y-%m-%d %H:%M:%S")

samp<-data.frame(location.long, location.lat, timestamp)

#make track and steps and random steps
samp.track<-samp %>% 
  dplyr::select(x = 'location.long', y = 'location.lat', t = 'timestamp') %>%
  make_track(x , y , t, crs = 4326) %>% track_resample(rate = hours(5), tolerance = minutes(0)) 

samp.steps<- samp.track %>%
  steps_by_burst(lonlat = TRUE) 

get_crs(samp.steps) #still in the proper crs

rand.steps<-samp.steps %>%
  mutate(sl_ = ifelse(sl_ == 0, 0.01, sl_)) %>% 
  random_steps(n_control=2)

rand.steps

The output of the above code with random steps: image

I've tried researching the issue and haven't found anything. Am I missing something?

Thanks in advance, c

jmsigner commented 1 day ago

I am not sure, that I fully understand the problem.

library(amt)
#> 
#> Attaching package: 'amt'
#> The following object is masked from 'package:stats':
#> 
#>     filter
  library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
set.seed(3)

#example data:
location.long=c(-97.47371, -97.47388, -97.47392, -97.47395, -97.47394, -97.47413, -97.47411, -97.47414)
location.lat=c(26.52136, 26.52141, 26.52137, 26.52169, 26.52171, 26.52156, 26.52159, 26.52156)
timestamp=c("2022-06-25 00:00:00", "2022-06-25 05:00:00", "2022-06-25 10:00:00", "2022-06-25 15:00:00", "2022-06-30 20:00:00", "2022-07-01 01:00:00", "2022-07-01 06:00:00", "2022-07-01 11:00:00")
timestamp<-as.POSIXct(timestamp, format = "%Y-%m-%d %H:%M:%S")

samp<-data.frame(location.long, location.lat, timestamp)

#make track and steps and random steps
samp.track<-samp %>% 
  dplyr::select(x = 'location.long', y = 'location.lat', t = 'timestamp') %>%
  make_track(x , y , t, crs = 4326) %>% track_resample(rate = hours(5), tolerance = minutes(0)) 

samp.steps<- samp.track %>%
  steps_by_burst(lonlat = TRUE) 

get_crs(samp.steps) #still in the proper crs
#> Coordinate Reference System:
#>   User input: EPSG:4326 
#>   wkt:
#> GEOGCRS["WGS 84",
#>     ENSEMBLE["World Geodetic System 1984 ensemble",
#>         MEMBER["World Geodetic System 1984 (Transit)"],
#>         MEMBER["World Geodetic System 1984 (G730)"],
#>         MEMBER["World Geodetic System 1984 (G873)"],
#>         MEMBER["World Geodetic System 1984 (G1150)"],
#>         MEMBER["World Geodetic System 1984 (G1674)"],
#>         MEMBER["World Geodetic System 1984 (G1762)"],
#>         MEMBER["World Geodetic System 1984 (G2139)"],
#>         ELLIPSOID["WGS 84",6378137,298.257223563,
#>             LENGTHUNIT["metre",1]],
#>         ENSEMBLEACCURACY[2.0]],
#>     PRIMEM["Greenwich",0,
#>         ANGLEUNIT["degree",0.0174532925199433]],
#>     CS[ellipsoidal,2],
#>         AXIS["geodetic latitude (Lat)",north,
#>             ORDER[1],
#>             ANGLEUNIT["degree",0.0174532925199433]],
#>         AXIS["geodetic longitude (Lon)",east,
#>             ORDER[2],
#>             ANGLEUNIT["degree",0.0174532925199433]],
#>     USAGE[
#>         SCOPE["Horizontal component of 3D system."],
#>         AREA["World."],
#>         BBOX[-90,-180,90,180]],
#>     ID["EPSG",4326]]

rand.steps<-samp.steps %>%
  mutate(sl_ = ifelse(sl_ == 0, 0.0001, sl_)) %>% 
  random_steps(n_control=2)

rand.steps
#> # A tibble: 12 × 12
#>    burst_   x1_    x2_   y1_   y2_   sl_      ta_ t1_                
#>  *  <dbl> <dbl>  <dbl> <dbl> <dbl> <dbl>    <dbl> <dttm>             
#>  1      1 -97.5  -97.5  26.5 26.5   5.97 -1.15    2022-06-25 05:00:00
#>  2      1 -97.5  -89.4  26.5 24.8   8.26 -3.06    2022-06-25 05:00:00
#>  3      1 -97.5  -92.7  26.5 20.2   7.89  2.51    2022-06-25 05:00:00
#>  4      1 -97.5  -97.5  26.5 26.5  35.7   2.32    2022-06-25 10:00:00
#>  5      1 -97.5 -111.   26.5 13.5  18.5  -0.00730 2022-06-25 10:00:00
#>  6      1 -97.5  -93.2  26.5 17.5   9.95  1.23    2022-06-25 10:00:00
#>  7      2 -97.5  -97.5  26.5 26.5   3.88  2.83    2022-07-01 01:00:00
#>  8      2 -97.5 -114.   26.5  3.15 28.6   0.291   2022-07-01 01:00:00
#>  9      2 -97.5  -95.8  26.5 16.2  10.4   1.06    2022-07-01 01:00:00
#> 10      2 -97.5  -97.5  26.5 26.5   4.48 -2.95    2022-07-01 06:00:00
#> 11      2 -97.5  -75.6  26.5 24.4  22.0  -1.08    2022-07-01 06:00:00
#> 12      2 -97.5  -97.5  26.5 30.3   3.77  0.602   2022-07-01 06:00:00
#> # ℹ 4 more variables: t2_ <dttm>, dt_ <drtn>, case_ <lgl>, step_id_ <dbl>

table(rand.steps$x1_) 
#> 
#> -97.47413 -97.47411 -97.47392 -97.47388 
#>         3         3         3         3
table(rand.steps$y1_) 
#> 
#> 26.52137 26.52141 26.52156 26.52159 
#>        3        3        3        3

table(rand.steps$x2_) 
#> 
#> -113.879248634899 -110.669223439406 -97.5283911944845         -97.47414 
#>                 1                 1                 1                 1 
#>         -97.47411         -97.47395         -97.47392 -95.8267003725921 
#>                 1                 1                 1                 1 
#> -93.2095752976797  -92.689222693138 -89.3983992952093 -75.5721597557808 
#>                 1                 1                 1                 1
table(rand.steps$y2_)
#> 
#> 3.14614656780088 13.5174193850682 16.2348580171546 17.5306353076121 
#>                1                1                1                1 
#> 20.2474726434914 24.4109954510294 24.8055718625155         26.52137 
#>                1                1                1                1 
#>         26.52156         26.52159         26.52169 30.2881405405134 
#>                1                1                1                1

For each starting point (x1 and y1) you have three times the same coordinate (observed + random steps) and all the end points are different. This is what you would expect? Do I miss something?

Created on 2024-11-26 with reprex v2.1.0

camkie27 commented 23 hours ago

That part seems fine, yes.

My main question is why are the coordinates of some of the random steps (x2, y2) in drastically different places? They should all start with -97 and 26.

jmsigner commented 23 hours ago

The starting points of the random steps should be the same as for the observed steps (column x1_ and y1_). The end points should be different. Step lengths (sl_) seem to be way too large, I will look into this.

camkie27 commented 23 hours ago

Thank you so much!

camkie27 commented 23 hours ago

Just as another note - I expect the units for sl to be meters, and the values themselves in the table above seem reasonable for my study species (turtles). I've mapped used steps in ArcGIS and confirmed that the sl distance between them is in fact in meters and is accurately measured.

Thank you again for your time and efforts looking into this - I really appreciate it.