Perturb Example"

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

Not all sensitive data is recorded as strings - features such as age, date of birth, or income could result in aspects of a data set being personally identifiable. To aid with these challenges we include methods for 'perturbing' numeric data (the addition of random noise).

Three types of random noise are included:

  1. adaptive_noise [default] - random noise which scales with the standard deviation of the variable transformed.
  2. white_noise - random noise at a set spread.
  3. lognorm_noise - random multiplicative noise at a set spread.

NB: we set a random seed using set.seed here for reproducibility. We recommend users avoid this step when using the package in production code.

library(deident)
set.seed(101)

perturb_pipe <- ShiftsWorked |>
  add_perturb(`Daily Pay`)

apply_deident(ShiftsWorked, perturb_pipe)

To change the noise, pass one of the functions including the desired level of noise.

perturb_pipe_white_noise <- ShiftsWorked |>
  add_perturb(`Daily Pay`, noise = white_noise(sd=0.3))

apply_deident(ShiftsWorked, perturb_pipe_white_noise)
perturb_pipe_heavy_adaptive_noise <- ShiftsWorked |>
  add_perturb(`Daily Pay`, noise = adaptive_noise(sd.ratio=0.4))

apply_deident(ShiftsWorked, perturb_pipe_heavy_adaptive_noise)


Try the deident package in your browser

Any scripts or data that you put into this service are public.

deident documentation built on April 3, 2025, 6:14 p.m.