Draws n synthetic rows from a csdb_profile. The rows follow the
published summaries and hold no row of the profiled data.
Arguments
- profile
A
csdb_profile, fromprofile_query(),read_profile()orprofile_schema().- n
The number of rows, a whole number of at least 1.
NULLgives 1000 rows, the default.- seed
The seed for
set.seed(). The function restores the random state of the caller on exit.- world
NULL, or a named list that maps a column to its allowed values. The function converts the values to the column's type. It ignores, without a message, each element that names no column ofprofile$field_types, so one world can serve several tables.
Details
The R type of each column follows its field type: TEXT character,
INTEGER integer, DOUBLE double, BOOLEAN logical, DATE Date and
DATETIME POSIXct in UTC.
A column takes its values from its own summary:
levelsordays: a draw of the published values, weighted byn. The suppressed cells generate nothing. ADATETIMEdrawn fromdaysgets a uniform whole second within its UTC day.weeks,monthsorquarters: a cell drawn byn, then a uniform day within it. A cell ofquartersends with the quarter inquarters_towhen the profile holdsquarters_to.breaks: a bin drawn byn, then a uniform value within the bin. AnINTEGERvalue is a uniform whole number within the bin. A column withnonnegativeTRUEdraws no value below 0.n_distinctof aTEXTcolumn:"<column>_<i>", withiuniform in 1 ton_distinct.missing_share:NAat that rate. A share of"suppressed"gives noNA. A key column gets noNA.
A schema_only or exclude column, and a column whose summary has no
published cell, gets schema values. A TEXT or INTEGER key column gets
"<column>_<i>" or i for row i, so a single-column key is unique at
any n. Other columns get schema values by type:
TEXT:"<column>_<i>", withifrom 1 to 10.INTEGER: a whole number from 0 to 100.DOUBLE: a uniform value from 0 to 1.BOOLEAN:TRUEorFALSE. ABOOLEANwithn_distinctonly also gets these values.DATEandDATETIME: a uniform day from 2020-01-01 to 2024-12-31. ADATETIMEgets a uniform whole second within that day.
The joint structure comes after the columns, in this order:
Each crosstab draws its two columns jointly by the cell counts. This replaces the values of both columns.
Each group size draws a range by
n, then a uniform row count fromsize_fromtosize_to. The last range has nosize_toand draws up to2 * size_from. It draws row counts until they cover all rows, gives each group a fresh identifier, and truncates the last group. With no published range, each group has 1 row.Each dependency that holds gives every value of its first column one value of the second column. The function assigns the values of the first column from the most rows to the fewest. Each goes to the value of the second column that is furthest below its drawn row count. This keeps the drawn frequencies of the second column close.
Before it draws, the function computes the key space. That is the
product, over the key columns, of the number of values each column can
take. When n is larger, it stops with an error that names the keys, the
key space and n. A key column can take:
a
worldcolumn: its allowed values.levelsordays: the values with a count above 0. ADATETIMEfromdayscan take every second of those days.weeks,monthsorquarters: every day of the cells with a count above 0. ADATETIMEcan take every second of those days.breaks: anINTEGERtakes every whole number of the bins with a count above 0, from 0 up whennonnegativeisTRUE. ADOUBLEhas no limit.n_distinctof aTEXTcolumn:n_distinctvalues.a group-size column: no limit, because each group gets a fresh identifier.
schema values: no limit for a
TEXT,INTEGERorDOUBLEkey column, and 2 forBOOLEAN. ADATEtakes the 1827 days from 2020-01-01 to 2024-12-31, and aDATETIMEevery second of those days.
A crosstab replaces its columns, so a key column of a crosstab can take
the values that it has in the cells left after world. The key space
does not count crosstab pairs or dependencies, so it is an upper bound.
The function redraws each row whose keys repeat an earlier row. After 20
redraws it stops with an error that names the keys and n. A redrawn row
keeps the values of its group-size columns.
A world column takes its values from world only. A value that the
column's levels or days also hold keeps its published weight. Every
other value gets the mean of those weights, or all values get equal
weights when the profile holds none of them. A crosstab keeps only the
cells whose values world allows, and has no effect when no cell is left.
A group-size column takes its identifiers from world, without
replacement where world has enough values.
See also
Other simulation functions:
profile_compare(),
profile_schema()
Examples
d <- data.frame(
sex = rep(c("female", "male"), 10),
age = rep(c(4L, 15L, 38L, 52L, 71L), 4)
)
p <- profile_data(
d,
c(sex = "TEXT", age = "INTEGER"),
columns = c(sex = "full", age = "coarsen")
)
simulate_from_profile(p, n = 5, seed = 2)
#> sex age
#> <char> <int>
#> 1: male 11
#> 2: female 33
#> 3: female 15
#> 4: male 29
#> 5: female 42
