Construct and modify data simulations
Arguments
- sim
The data simulation object to be modified.
- report_hidden
If
TRUE
, show the hidden nodes (nodes whose names begin with a dot.)- ...
Descriptions of the nodes in the simulation, written in assignment form. See details.
Value
an object of class "datasim". Internally, this is a list of the R assignment expressions used when running the simulation.
Details
Simulations in LSTbook
are first specified by providing the
code for each node (which can be written in terms of the values of other nodes). Once
constructed, data can be extracted from the simulation using datasim_run(n)
or the
generic sample(n)
.
Each argument defines one node in the simulation. The argument syntax is unusual, using
assignment. For instance, an argument y <- 3*x + rnorm(n)
defines a node named y
. The R code
on the RHS of the assignment operator (that is, 3*x + rnorm(n)
in the example) will
be used by datasim_run()
to generate the y
variable when the simulation is run. Nodes
defined by previous arguments can be used in the code for later arguments.
Helper functions such as mix_with()
, categorical()
, and several others can be used within
the node to perform complex operations.
Remember to use commas to separate the arguments in the normal way.
Examples
Simple_sim <- datasim_make(x <- rnorm(n, sd=2), y <- 3*x + rnorm(n))
Simple_sim |> datasim_run(n = 5)
#> # A tibble: 5 × 2
#> x y
#> <dbl> <dbl>
#> 1 0.361 1.32
#> 2 -6.55 -18.4
#> 3 -4.70 -14.9
#> 4 -4.01 -11.2
#> 5 1.95 7.43