Skip to contents

jitter_weights() adds random noise (jitter) to a selected numeric column in a movenet-format movement tibble. By default, the weight column is selected.

Usage

jitter_weights(data, range, column = movenetenv$options$movedata_cols$weight)

Arguments

data

A movenet-format movement tibble.

range

A positive number, indicating the maximum amount of jitter to apply to the numeric data (see Details).

column

Name of a single numeric column to jitter. By default this is the weight column (as specified in the loaded movement configurations).

Value

A movement tibble like data, but with jitter applied to the selected numeric column.

Details

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

The data in the selected column are modified by addition of an amount of noise between -range and range, following a uniform distribution. If this were to result in a data point becoming <= 0, the amount of jitter for this data point is resampled, until the resulting data point becomes positive. This is to capture that any movement in a livestock movement database is assumed to have a positive weight (quantity of animals moved).

See also

base::jitter(), which this function is based on.

Other Privacy-enhancing functions: anonymise(), create_anonymisation_effect_analysis_report(), jitter_dates(), round_dates(), round_weights()

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 random noise from -3 to +3 to movement weights
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_weights <- jitter_weights(example_movement_data, 3)
head(moves_w_jittered_weights)
#> # 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.4             304781
#> 2 69/196/5890   71/939/3228 2019-08-15        169.              229759
#> 3 52/577/5349   82/501/8178 2019-09-15        115.               36413
#> 4 39/103/5541   13/282/1763 2019-10-26        126.              488616
#> 5 41/788/6464   57/418/6011 2019-10-17        107.              581785
#> 6 69/393/9398   39/947/2201 2019-10-06         69.8             564911

# Add random noise from -3 to +3 to movement reference numbers
moves_w_jittered_refs <- jitter_weights(example_movement_data,
                                        range = 3,
                                        column = "movement_reference")
head(moves_w_jittered_refs)
#> # 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            304783.
#> 2 69/196/5890   71/939/3228 2019-08-15          167            229759.
#> 3 52/577/5349   82/501/8178 2019-09-15          115             36416.
#> 4 39/103/5541   13/282/1763 2019-10-26          125            488618.
#> 5 41/788/6464   57/418/6011 2019-10-17          109            581784.
#> 6 69/393/9398   39/947/2201 2019-10-06           72            564909.

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