Generate Bootstrap Sample with Added Noise
Source:R/synthetic_data_perturbation.R
generate_bootstrap_with_noise.RdCreates a bootstrap sample from a dataset and adds controlled noise.
Continuous columns receive Gaussian noise; categorical columns are changed
with a small probability. By default every numeric column except
id_var is continuous and every factor or logical column is
categorical. Numeric-coded categories (for example 0/1 indicators) are
therefore treated as continuous unless continuous_vars is given
without them; naming them in cat_vars alone adds the categorical
change without removing the noise. This function is useful for generating
synthetic datasets that maintain the general structure of the original data
while introducing controlled variation.
Usage
generate_bootstrap_with_noise(
data,
n = NULL,
continuous_vars = NULL,
cat_vars = NULL,
id_var = "pid",
seed = 123,
noise_level = 0.1
)Arguments
- data
A data frame containing the original dataset to bootstrap from.
- n
Integer. Number of observations in the output dataset. If NULL (default), uses the same number of rows as the input data.
- continuous_vars
Character vector of column names to treat as continuous variables. If NULL (default), every numeric column except
id_varis used, including numeric columns with few distinct values. A column named here must be numeric: a factor or character column stops the call with an error, and a logical column is returned as a numeric 0/1 column.- cat_vars
Character vector of column names to treat as categorical variables. If NULL (default), factor and logical columns are used, together with numeric columns that have 10 or fewer unique values; columns in
continuous_varsandid_varare left out, and character columns are not used. Whencontinuous_varsis also NULL, every numeric column other thanid_varis already in the continuous set, so no numeric column is treated as categorical. Whencat_varsis given andcontinuous_varsis NULL, a numeric column named here is also in the auto-detected continuous set and receives both kinds of change; givecontinuous_varsas well to avoid this.- id_var
Character string specifying the name of the ID variable column. This column will be reset to sequential values (1:n) in the output. Default is "pid".
- seed
Integer. Random seed for reproducibility. Default is 123.
- noise_level
Numeric between 0 and 1. Controls the amount of noise added. For continuous variables, this is multiplied by the standard deviation to determine noise magnitude. For categorical variables, this is divided by 2 to determine the probability of value changes. Default is 0.1.
Value
A data frame of n rows, with the columns of data in
the same order and row names 1:n, containing bootstrap sampled
observations with added noise. Column types are kept, except that every
column in continuous_vars is returned as double (an integer column
comes back double, holding whole numbers) and id_var, if present,
is replaced by the integer sequence 1:n.
Details
The function performs the following operations:
Continuous Variable Noise
Adds Gaussian noise with standard deviation = original SD times noise_level
Constrains values to remain within original variable bounds
Rounds to whole numbers the variables that appear to be integers (see below); the column is returned as double, not integer
Leaves missing values missing
Integer-valued columns (every non-missing value a whole number) are
rounded after the noise is added, so a 0/1 column treated as continuous is
usually unchanged at a small noise_level.
Categorical Variable Perturbation
Applies to the columns in cat_vars, given or auto-detected as
described for that argument.
Changes values with probability = noise_level / 2
Binary variables: flips to opposite value
Multi-level unordered: randomly selects from other levels
Ordered factors, or a column named
grade, with more than two levels: selection is weighted toward adjacent levelsPreserves factor levels and ordering from original data
Note
The function assumes that categorical variables with numeric encoding should maintain their numeric type unless they are factors in the input
Missing values (NA) in a column treated only as continuous (not also in
cat_vars) stay missing. In a categorical column a missing value is selected for change with the same probability,noise_level / 2, as any other value; if it is not selected it stays missing. A selected missing value in an ordered factor, or a column namedgrade, with more than two levels stops the call with the error "replacement has length zero". Otherwise, in a column with two levels (for a non-factor column, two distinct non-missing values indata) it is replaced by the first level (for a non-factor column, the smallest value); in a column with more than two levels, by a level drawn at random from all its levels; and in a column with fewer than two levels (for a non-factor column, fewer than two distinct non-missing values) it stays missing.For an ordered factor, or a column named
grade, that is processed as categorical and has more than two levels, the perturbation favors adjacent levels. A numericgradecolumn is processed as categorical only when it is named incat_vars, or whencat_varsis NULL,continuous_varsis given without it and it has 10 or fewer unique values.
Examples
if (FALSE) { # \dontrun{
# Load example dataset
gbsg <- survival::gbsg
# Basic usage with automatic variable detection
# (all gbsg columns are numeric, so all except pid, the id_var, are
# treated as continuous)
synthetic_data <- generate_bootstrap_with_noise(
data = gbsg,
seed = 123
)
# Specify variables explicitly
synthetic_data <- generate_bootstrap_with_noise(
data = gbsg,
n = 1000,
continuous_vars = c("age", "size", "nodes", "pgr", "er", "rfstime"),
cat_vars = c("meno", "grade", "hormon", "status"),
id_var = "pid",
seed = 456,
noise_level = 0.15
)
# Create multiple synthetic datasets
synthetic_list <- lapply(1:10, function(i) {
generate_bootstrap_with_noise(data = gbsg, seed = i)
})
} # }