knitr::opts_chunk$set( collapse = TRUE, comment = "#>", eval = TRUE, echo = TRUE, # echo code? message = TRUE, # Show messages warning = TRUE, # Show warnings fig.width = 8, # Default plot width fig.height = 6, # .... height dpi = 200, # Plot resolution fig.align = "center" ) knitr::opts_chunk$set() # Figure alignment library(DataFakeR) set.seed(123) options(tibble.width = Inf)
Most of the functionality offered by DataFakeR were described in the remaining vignettes. Goal of this section is to present additional options offered by DataFakeR that can make your data simulating workflow much easier.
It is quite common that values of the column are strongly related to the groups defined by the other column values.
Some of the examples may be the age of death that may differ between males and females.
In order to allow simulating values based on groups designed by the other columns, group_by
parameter was introduced.
Let's see the below schema structure:
# schema-patient.yml public: tables: patient: columns: treatment_id: type: varchar formula: !expr paste0(patient_id, line_number) patient_id: type: char(8) line_number: type: smallint gender: type: char(1) values: [F, M] biomarker: type: numeric range: [0, 1]
Let's simulate the data using the below definition:
sch <- schema_source(system.file("extdata", "schema-patient.yml", package = "DataFakeR")) sch <- schema_simulate(sch) schema_get_table(sch, "patient")
Now we'd like to extend the definition following the below rules:
patient_id
we usually want to have more than one row (it allows us to analyze the patient in multiple treatment stages). patient_id
, line_number
is the sequence starting from 1 to the number of patient rows.gender
should be unique for each patient_id
value.biomarker
value should mean = 0.5
for females and mean = 0.6
for males.Let's start with condition 1. We may simply achieve this by providing possible patient_id
values with unique number less than target number of rows.
# schema-patient_2.yml public: tables: patient: columns: treatment_id: type: varchar formula: !expr paste0(patient_id, line_number) patient_id: type: char(8) values: [PTNT01ID, PTNT02ID, PTNT03ID] line_number: type: smallint gender: type: char(1) values: [F, M] biomarker: type: numeric range: [0, 1]
Let's simulate the data using the below definition:
sch <- schema_update_source(sch, file = system.file("extdata", "schema-patient_2.yml", package = "DataFakeR")) sch <- schema_simulate(sch) schema_get_table(sch, "patient")
Now, we'd like to make sure line_number
is a sequence 1:n
, where n
is the number of rows for each patient.
If we define line_number
formula as !expr 1:dplyr::n()
, the resulting column would be a sequence 1:n
where n
is the number of table rows.
There is an obvious need to apply grouping by patient_id
.
How can we achieve this in DataFakeR?
It is enough to add group_by: patient_id
parameter to line_number
.
Let's see it in action:
# schema-patient_3.yml public: tables: patient: columns: treatment_id: type: varchar formula: !expr paste0(patient_id, line_number) patient_id: type: char(8) values: [PTNT01ID, PTNT02ID, PTNT03ID] line_number: type: smallint group_by: patient_id formula: !expr 1:dplyr::n() gender: type: char(1) values: [F, M] biomarker: type: numeric range: [0, 1]
Looking at the dependency graph:
sch <- schema_update_source(sch, file = system.file("extdata", "schema-patient_3.yml", package = "DataFakeR")) schema_plot_deps(sch, "patient")
we can see, DataFakeR detected dependency between patient_id
and line_numer
column.
line_number
was also created according to our needs:
sch <- schema_simulate(sch) schema_get_table(sch, "patient")
Now let's assure gender is unique for each patient.
To achieve this, we'll have to group by patient_id
and sample one value from c("F", "M)
and repeat this with desired number of rows.
We may again use the formula, but for the sake of the example we'll define custom restricted method.
The method will be executed whenever singular: true
parameter is provided to the column.
# schema-patient_4.yml public: tables: patient: columns: treatment_id: type: varchar formula: !expr paste0(patient_id, line_number) patient_id: type: char(8) values: [PTNT01ID, PTNT02ID, PTNT03ID] line_number: type: smallint group_by: patient_id formula: !expr 1:dplyr::n() gender: type: char(1) values: [F, M] singular: true group_by: patient_id biomarker: type: numeric range: [0, 1]
The method definition:
singular_vals <- function(n, values, singular, ...) { if (!missing(singular) && isTRUE(singular)) { val <- sample(values, 1) return(rep(val, n)) } return(NULL) }
and add it to schema options:
my_opts = set_faker_opts( opt_simul_restricted_character = opt_simul_restricted_character( singular = singular_vals ) )
Now we can start simulation:
sch <- schema_update_source( sch, file = system.file("extdata", "schema-patient_4.yml", package = "DataFakeR"), my_opts ) sch <- schema_simulate(sch) schema_get_table(sch, "patient")
Voila!
We'd like to sample from normal distribution with a specific mean dependent on the gender value.
Again we have a few options here. The simplest one would be to group biomarker
by gender
and use:
formula: ifelse(gender == "F", rnorm(dplyr::n(), 0.5, 0.01), rnorm(dplyr::n(), 0.6, 0.01))
but we'll do it using a special method instead.
The main issue we might have is how to access the group value from within the method.
We shouldn't be worried. DataFakeR automatically passes the value as a group_val
parameter.
So let's try it out.
Let's define spec method named dep_sampl
:
# schema-patient_5.yml public: tables: patient: columns: treatment_id: type: varchar formula: !expr paste0(patient_id, line_number) patient_id: type: char(8) values: [PTNT01ID, PTNT02ID, PTNT03ID] line_number: type: smallint group_by: patient_id formula: !expr 1:dplyr::n() gender: type: char(1) values: [F, M] singular: true group_by: patient_id biomarker: type: numeric range: [0, 1] spec: dep_sampl group_by: gender
Inside function definition let's add print line to see current group_key
value:
dep_sampl <- function(n, group_val, range, ...) { print(group_val) if (group_val == "M") { pmax(pmin(rnorm(n, 0.6, 0.01), range[2]), range[1]) } else { pmax(pmin(rnorm(n, 0.5, 0.01), range[2]), range[1]) } } my_opts = set_faker_opts( opt_simul_spec_numeric = opt_simul_spec_numeric( dep_sampl = dep_sampl ), # don't forget option from previous case opt_simul_restricted_character = opt_simul_restricted_character( singular = singular_vals ) ) sch <- schema_update_source( sch, file = system.file("extdata", "schema-patient_5.yml", package = "DataFakeR"), my_opts ) sch <- schema_simulate(sch) schema_get_table(sch, "patient")
This way we achieved assumed goal.
Another extra parameter offered by DataFakeR is na_ratio
.
Na ratio allows to precise the ratio of how many NA values should the column have.
For each simulation method the final sample is modified by na_rand function, that replaces desired ratio of values with NA
s.
Default na_ratio
value is 0.05
but can be easily overwritten by opt_default_
configuration, or passed directly in column definition.
Note Whenever column have defined not_null: true
, na_rand
doesn't attach NA
values to the sample.
The last extra parameter offered by DataFakeR is levels_ratio
.
Levels ratio allows to precise how many unique values should the column have.
For each simulation method (before the sample is modified by na_rand
) the sample is modified by levels_rand function, that takes desired number of sample levels and resamples it using only provided levels.
Default levels_ratio
is 1
but can be easily overwritten by opt_default_
configuration, or passed directly in column definition.
Note Whenever column have defined unique: true
or levels_ratio: 1
, levels_rand
doesn't modify the sample.
There are a few parameters that can be configured to each column and be handled by all the simulation methods.
Such parameters are:
values
- The parameter keeps possible values for the simulated column,range
- Two-length parameter storing minimum and maximum value for simulated column (numeric, integer and date only),precision
- Precision of numeric column values,scale
- Scale of numeric column values,min_date
, max_date
- minimal and maximal values for simulating date columns (overwritten by range
when specified),format
- Date format of min_date
, max_date
and range
in case of date columns (%Y-%m-%d by default),nchar
- Maximum number of characters for simulating character column (10 by default).Whenever any of the above parameters is defined as a parameter of column-type simulation method, such value can be used to get more accurate result respecting the configuration.
For example default character simulation method (simul_default_character) takes an advantage of nchar
parameter, but special method for simulating names don't (simul_spec_character_name).
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.