Skip to contents

This function executes the make_model function provided by the user and writes to file the resulting Model object(s). For example, when simulating regression with a fixed design, X would be generated in this function and n, p, beta, and sigma would also be specified.

Usage

generate_model(object = ".", make_model, ..., seed = 123, vary_along = NULL)

Arguments

object

the name of the directory where directory named "files" exists (or should be created) to save Model object in. Default is current working directory. Or can be an object of class Simulation, in which case the object@dir is used and a simulation object is returned instead of an object of class ModelRef.

make_model

a function that outputs an object of class Model. Or a list of such functions.

...

optional parameters that may be passed to make_model

seed

an integer seed for the random number generator.

vary_along

character vector with all elements contained in names(...) See description for more details.

Details

When make_model has arguments, these can be passed using .... These will be passed directly to make_model except for any arguments named in vary_along. These arguments should be lists and a separate model will be created for each combination of elements in these lists. For example, if vary_along = c("n", "p"), then we can pass n=as.list(c(50, 100, 150)) and p=as.list(c(10, 100)) and 6 models will be created, one for each pair of n and p. For each pair (n,p), a distinct extension is added to the end of the model name. This extension is generated using a hash function so that different values of the vary_along parameters will lead to different model name extensions. This ensures that if one later decides to add more values of the vary_along parameters, this will not lead to pre-existing files being overwritten (unless the same values of the vary_along combination are used again.

If object is a directory name, the function returns a reference or list of references to the model(s) generated. If object is a Simulation, then function returns the same Simulation object but with references added to the new models created. These changes to the Simulation object are saved to file.

make_model is called generating an object of class Model, called model, which is saved to dir/name/model.Rdata (where name is the name attribute of model). This file also contains the random number generator state and other information such as the function make_model itself and the date when model was created.

Examples

 # initialize a new simulation
 sim <- new_simulation(name = "normal-example",
                       label = "Normal Mean Estimation",
                       dir = tempdir())
 # generate a model (and add it to the simulation)
 sim <- generate_model(sim, make_my_example_model, n = 20)
#> ..Created model and saved in normal-data/model.Rdata
 # generate a sequence of models (and add them to the simulation)
 sim <- generate_model(sim, make_my_example_model,
                       n = list(10, 20, 30),
                       vary_along = "n")
#> ..Created model and saved in normal-data/n_10/model.Rdata
#> ..Created model and saved in normal-data/n_20/model.Rdata
#> ..Created model and saved in normal-data/n_30/model.Rdata