tests/testthat/test-add_noise_laplace.R

# specify default arguments for add_noise_laplace
# note that NULL values may be supplied to the function and left unused
model <- NULL
new_data <- NULL
conf_model_data <- NULL
outcome_var <- NULL
col_schema <- NULL
pred <- 1:100

test_that("add_noise_lapalce reproduces noise", {
  
  set.seed(1)
  noisy_preds1 <- add_noise_laplace(
    model = model,
    new_data = new_data,
    conf_model_data,
    outcome_var = outcome_var,
    col_schema = col_schema,
    pred = pred,
    variance = 3
  )
  
  set.seed(1)
  noisy_preds2 <- add_noise_laplace(
    model = model,
    new_data = new_data,
    conf_model_data,
    outcome_var = outcome_var,
    col_schema = col_schema,
    pred = pred,
    variance = 3
  )
  
  expect_equal(noisy_preds1, noisy_preds2)
  
})

test_that("add_noise_laplace reproduces with either direct var or eDP", {
  
  set.seed(1)
  noisy_preds1 <- add_noise_laplace(
    model = model,
    new_data = new_data,
    conf_model_data,
    outcome_var = outcome_var,
    col_schema = col_schema,
    pred = pred,
    variance = 8
  )
  
  set.seed(1)
  noisy_preds2 <- add_noise_laplace(
    model = model,
    new_data = new_data,
    conf_model_data,
    outcome_var = outcome_var,
    col_schema = col_schema,
    pred = pred,
    epsilon = 1,
    sensitivity = 2
  )
  
  expect_equal(noisy_preds1, noisy_preds2)
  
})

test_that("add_noise_laplace error handling", {
  
  # no variance parameters specified
  expect_error(
    add_noise_laplace(
      model = model,
      new_data = new_data,
      conf_model_data,
      outcome_var = outcome_var,
      col_schema = col_schema,
      pred = pred
    ),
    regexp = "Must specify either `variance` or both `epsilon` and `sensitivity`."
  )
  
  # too many variance parameters specified
  expect_error(
    add_noise_laplace(
      model = model,
      new_data = new_data,
      conf_model_data,
      outcome_var = outcome_var,
      col_schema = col_schema,
      pred = pred,
      variance = 1,
      epsilon = 2
    ),
    regexp = "Cannot use non-null variance with non-null epsilon or sensitivity.",
    fixed = TRUE
  )
  
  # not enough variance parameters specified
  expect_error(
    add_noise_laplace(
      model = model,
      new_data = new_data,
      conf_model_data,
      outcome_var = outcome_var,
      col_schema = col_schema,
      pred = pred,
      epsilon = 2
    ),
    regexp = "Must specify either `variance` or both `epsilon` and `sensitivity`.",
    fixed = TRUE
  )
})

Try the tidysynthesis package in your browser

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

tidysynthesis documentation built on March 17, 2026, 1:06 a.m.