jmsigner / amt

37 stars 13 forks source link

track_resample : unexpected behavior if tolerance parameter increases #112

Open VVanlan opened 3 months ago

VVanlan commented 3 months ago

Hi,

I have GPS data at 2 hours intervals for some indiviudal and 13 hours for others. I want to resample track for one point/day.

If i run track_resample with tolerance of 1 hours, everything is fine. -> Individual with 2 hours interval have one point each day at the exact same time. -> Individual with 13 hours interval have one point each day (but burst change)

If I run the code with tolerance of 2 hours (because i want the same burst for individual at 13 hours fix rate) -> I would except the same output for individual with 2 hours interval. But it seems that it selects the first points according to the rate and the tolerance rather than the closest to the rate.

Maybe the function should select points that has the smallest difference with the desired rate?

For exemple :

  ex<- tibble(Id = c(rep("a", 121), rep("b", 19)),
              date = c(seq.POSIXt(from = Sys.time(), to = Sys.time()+days(10), by ="2 hours"),
                       seq.POSIXt(from = Sys.time(), to = Sys.time()+days(10), by ="13 hours")),
              x = sample(seq(-71,-70, by=0.01), 140, replace = TRUE),
              y = sample(seq(49,50, by=0.01), 140, replace = TRUE))

  ex2<- ex %>%
    make_track(.x = x, .y=y, .t= date,  crs=4326, all_cols = T ) %>% 
    nest(data=-Id) %>% 
    mutate(rsample = map(data, \(x) track_resample(x, rate = days(1), tolerance = hours(2))),
           n = map_int(data, nrow)) 

## tolerance = 1 hour
ex2$rsample[[1]]
# A tibble: 11 × 4
      x_    y_ t_                  burst_
 * <dbl> <dbl> <dttm>               <dbl>
 1 -70.8  49.3 2024-06-12 15:27:42      1
 2 -70.2  50.0 2024-06-13 15:27:42      1
 3 -71.0  49.4 2024-06-14 15:27:42      1
 4 -70.9  49.0 2024-06-15 15:27:42      1
 5 -70.4  49.1 2024-06-16 15:27:42      1
 6 -70.9  49.7 2024-06-17 15:27:42      1
 7 -70.3  49.5 2024-06-18 15:27:42      1
 8 -70.0  49.6 2024-06-19 15:27:42      1
 9 -70.7  49.0 2024-06-20 15:27:42      1
10 -70.8  49.3 2024-06-21 15:27:42      1
11 -70.1  49.8 2024-06-22 15:27:42      1

## tolerance = 2 hours
ex2$rsample[[1]]
# A tibble: 11 × 4
      x_    y_ t_                  burst_
 * <dbl> <dbl> <dttm>               <dbl>
 1 -70.8  49.3 2024-06-12 15:27:42      1
 2 -70.5  49.9 2024-06-13 13:27:42      1
 3 -70.6  49.9 2024-06-14 11:27:42      1
 4 -70.8  49.6 2024-06-15 09:27:42      1
 5 -70.4  49.7 2024-06-16 07:27:42      1
 6 -70.3  49.4 2024-06-17 05:27:42      1
 7 -70.8  50.0 2024-06-18 03:27:42      1
 8 -70.0  49.6 2024-06-19 01:27:42      1
 9 -70.9  49.6 2024-06-19 23:27:42      1
10 -70.6  49.5 2024-06-20 21:27:42      1
11 -70.9  50.0 2024-06-21 19:27:42      1