Skip to contents

jitter_dates() adds random noise (jitter) to movement dates in a movenet-format movement tibble.

Usage

jitter_dates(data, range)

Arguments

data

A movenet-format movement tibble.

range

A positive integer, indicating the maximum amount of jitter (in days) to apply to the movement dates (see Details).

Value

A movement tibble like data, but with jittered movement dates.

Details

Requires that an appropriate movement config file is loaded, to correctly identify the date column in data.

Movement dates are modified by addition of a number of days between -range and range, following a discrete uniform distribution. If this were to result in a date being moved beyond the original date range, the amount of jitter for this date is resampled, until the resulting date is within the original date range.

See also

Examples

# Set-up: Save movenet environment with current configurations
movenetenv <- movenet:::movenetenv
old_config <- movenetenv$options

# Load a movement config file
load_config(system.file("configurations", "ScotEID.yml",
                        package = "movenet"))
#> Successfully loaded config file: C:/Users/cboga/AppData/Local/Temp/Rtmp25xhub/temp_libpath11484970381d/movenet/configurations/ScotEID.yml

# Add -3 to +3 days of jitter to movement dates
head(example_movement_data)
#> # A tibble: 6 x 5
#>   departure_cph dest_cph    departure_date qty_pigs movement_reference
#>   <chr>         <chr>       <date>            <dbl>              <dbl>
#> 1 95/216/1100   19/818/9098 2019-02-08           97             304781
#> 2 69/196/5890   71/939/3228 2019-08-15          167             229759
#> 3 52/577/5349   82/501/8178 2019-09-15          115              36413
#> 4 39/103/5541   13/282/1763 2019-10-26          125             488616
#> 5 41/788/6464   57/418/6011 2019-10-17          109             581785
#> 6 69/393/9398   39/947/2201 2019-10-06           72             564911
moves_w_jittered_dates <- jitter_dates(example_movement_data, 3)
head(moves_w_jittered_dates)
#> # A tibble: 6 x 5
#>   departure_cph dest_cph    departure_date qty_pigs movement_reference
#>   <chr>         <chr>       <date>            <dbl>              <dbl>
#> 1 95/216/1100   19/818/9098 2019-02-10           97             304781
#> 2 69/196/5890   71/939/3228 2019-08-15          167             229759
#> 3 52/577/5349   82/501/8178 2019-09-15          115              36413
#> 4 39/103/5541   13/282/1763 2019-10-23          125             488616
#> 5 41/788/6464   57/418/6011 2019-10-18          109             581785
#> 6 69/393/9398   39/947/2201 2019-10-09           72             564911

# Clean-up: Reinstate previous configurations
movenetenv$options <- old_config
rm("old_config", "movenetenv", "moves_w_jittered_dates")