Learn R Programming

tidysynthesis (version 0.1.2)

add_noise_cat_unif: Inject noise into a categorical random variable by mixing a sample of uniform records into the predictions.

Description

Inject noise into a categorical random variable by mixing a sample of uniform records into the predictions.

Usage

add_noise_cat_unif(
  model,
  new_data,
  conf_model_data,
  outcome_var,
  col_schema,
  pred,
  unif_prop,
  resample_props = NULL,
  observed_levels = FALSE
)

Value

A numeric vector with noise added to each prediction

Arguments

model

A model_spec or a list of model_specs from library(parsnip)

new_data

A data frame used to generate predictions

conf_model_data

A data frame for estimating the predictive model

outcome_var

A string name representing the outcome variable

col_schema

A list of column schema specifications for the new variable

pred

A vector of values predicted by the model

unif_prop

A proportion of records to resample with uniform noise

resample_props

An optional named vector of probabilities for resampling, defaults to uniform over all levels supplied in col_schema.

observed_levels

An optional Boolean to only resample from observed levels in the confidential data.

Examples

Run this code

conf_model_data <- mtcars|>
  dplyr::mutate(gear = factor(.data[["gear"]]))

col_schema <- list(
  "dtype" = "fct",
  "levels" = c("3", "4", "5"),
  "na_prop" = 0
)

add_noise_cat_unif(
  model = conf_model_data,
  new_data = NULL,
  conf_model_data = NULL,
  outcome_var = "gear",
  col_schema = col_schema,
  pred = factor(c(rep("3", 10), rep("4", 10), rep("5", 10))),
  unif_prop = 0.5
)

Run the code above in your browser using DataLab