simulate_conditionally.simbase_list: Add simulated values to a dataset conditionally, based on a...

View source: R/statistical_simulation.r

simulate_conditionally.simbase_listR Documentation

Add simulated values to a dataset conditionally, based on a simbase_list object

Description

Add simulated values to a dataset conditionally, based on a simbase_list object

Usage

## S3 method for class 'simbase_list'
simulate_conditionally(
  data,
  simbase,
  force_positive = TRUE,
  ...,
  error_when_groups_missing = TRUE
)

Arguments

data

The dataset where simulated values are added to.

simbase

Basic data object for the simulation, as calculated by simbase_list.

force_positive

If TRUE, the resulting values are forced to be ≥ 0.

...

further arguments passed to or from other methods.

error_when_groups_missing

Whether to raise an error if for a certain value combination in the grouping variables no dedicated simbase exists (see details).

Details

Simulating values based on a simbase_list object has some special aspects compared to that of other simbase_* objects, (see simulate_conditionally).

In particular, a simbase_list object stores simbases for specific value combinations within the grouping variables.

These grouping variables must also be present in data.

If there is a value combination in these grouping variables for which no dedicated simbase object exists, this will lead to NA values in the columns to be simulated and either to an error (if error_when_groups_missing = TRUE) or to a warning.

Due to the internal call to nest and subsequent call to unnest, the returned dataset will be ordered according to the grouping variables in the simbase, with any grouping variable combinations missing in the simbase coming last.

Value

The modified dataset data with simulated values.

Examples

# create a simbase_list object for the values of subsets = c('AT', 'DE')
dataset_0 <- simulate_dataset(subsets = c('AT', 'DE'));
simbase <- simbase_covar(dplyr::group_by(dataset_0, country), c('f', 'E', 'E_dyn'));

# simulate on another dataset
dataset <- data.frame(E_dyn = rnorm(n = 100, mean = 12500, sd = 2200), country = 'AT');
dataset_1 <- simulate_conditionally(dataset, simbase);
head(dataset_1);

# warning if for some value of country we don't have an entry in the simbase
dataset$country <- 'CH';
dataset_2 <- simulate_conditionally(dataset, simbase, error_when_groups_missing = FALSE);
head(dataset_2);


WoodSimulatR documentation built on June 20, 2022, 9:05 a.m.