Experiment: 'R6' class representing a simulation experiment.

ExperimentR Documentation

R6 class representing a simulation experiment.

Description

A simulation experiment with any number of DGP, Method, Evaluator, and Visualizer objects.

Generally speaking, users won't directly interact with the Experiment R6 class, but instead indirectly through create_experiment() and the tidy Experiment helpers listed in below in the See also section.

Details

When run, an Experiment seamlessly combines DGPs and Methods, computing results in parallel. Those results can then be evaluated using Evaluators and visualized using Visualizers.

Public fields

name

The name of the Experiment.

Methods

Public methods


Method new()

Initialize a new Experiment object.

Usage
Experiment$new(
  name = "experiment",
  dgp_list = list(),
  method_list = list(),
  evaluator_list = list(),
  visualizer_list = list(),
  future.globals = TRUE,
  future.packages = NULL,
  clone_from = NULL,
  save_dir = NULL,
  ...
)
Arguments
name

The name of the Experiment.

dgp_list

An optional list of DGP objects.

method_list

An optional list of Method objects.

evaluator_list

An optional list of Evaluator objects.

visualizer_list

An optional list of Visualizer objects.

future.globals

Character vector of names in the global environment to pass to parallel workers. Passed as the argument of the same name to future.apply::future_lapply() and related functions. To set for a specific run of the experiment, use the same argument in run_experiment().

future.packages

Character vector of packages required by parallel workers. Passed as the argument of the same name to future.apply::future_lapply() and related functions. To set for a specific run of the experiment, use the same argument in run_experiment().

clone_from

An optional Experiment object to use as a base for this one.

save_dir

An optional directory in which to save the experiment's results. If NULL, results are saved in the current working directory in a directory called "results" with a sub-directory named after Experiment$name when using run_experiment() or fit_experiment() with save=TRUE.

...

Not used.

Returns

A new instance of Experiment.


Method run()

Run the full Experiment pipeline (fitting, evaluating, and visualizing).

Usage
Experiment$run(
  n_reps = 1,
  parallel_strategy = "reps",
  future.globals = NULL,
  future.packages = NULL,
  future.seed = TRUE,
  use_cached = FALSE,
  return_all_cached_reps = FALSE,
  save = FALSE,
  checkpoint_n_reps = 0,
  verbose = 1,
  ...
)
Arguments
n_reps

The number of replicates of the Experiment for this run.

parallel_strategy

A vector with some combination of "reps", "dgps", or "methods". Determines how computation will be distributed across available resources. Currently only the default, "reps", is supported.

future.globals

Character vector of names in the global environment to pass to parallel workers. Passed as the argument of the same name to future.apply::future_lapply and related functions. To set for all runs of the experiment, use the same argument during initialization.

future.packages

Character vector of packages required by parallel workers. Passed as the argument of the same name to future.apply::future_lapply and related functions. To set for all runs of the experiment, use the same argument during initialization.

future.seed

Passed as the argument of the same name in future.apply::future_apply.

use_cached

Logical. If TRUE, find and return previously saved results. If cached results cannot be found, continue as if use_cached was FALSE.

return_all_cached_reps

Logical. If FALSE (default), returns only the fit results for the requested n_reps. If TRUE, returns fit results for the requested n_reps plus any additional cached replicates from the (DGP, Method) combinations in the Experiment. Note that even if return_all_cached_reps = TRUE, only the n_reps replicates are used when evaluating and visualizing the Experiment.

save

If TRUE, save outputs to disk.

checkpoint_n_reps

The number of experiment replicates to compute before saving results to disk. If 0 (the default), no checkpoints are saved.

verbose

Level of verbosity. Default is 1, which prints out messages after major checkpoints in the experiment. If 2, prints additional debugging information for warnings and messages from user-defined functions (in addition to error debugging information). If 0, no messages are printed other than user-defined function error debugging information.

...

Not used.

Returns

A named list of results from the simulation experiment with the following entries:

fit_results

A tibble containing results from the fit method. In addition to results columns, has columns named '.rep', '.dgp_name', '.method_name', and the vary_across parameter names if applicable.

eval_results

A list of tibbles containing results from the evaluate method, which evaluates each Evaluator in the Experiment. Length of list is equivalent to the number of Evaluators.

viz_results

A list of tibbles containing results from the visualize method, which visualizes each Visualizer in the Experiment. Length of list is equivalent to the number of Visualizers.


Method generate_data()

Generate sample data from all DGP objects that were added to the Experiment, including their varied params. Primarily useful for debugging. Note that results are not generated in parallel.

Usage
Experiment$generate_data(n_reps = 1, ...)
Arguments
n_reps

The number of datasets to generate per DGP.

...

Not used.

Returns

A list of length equal to the number of DGPs in the Experiment. If the Experiment does not have a vary_across component, then each element in the list is a list of n_reps datasets generated by the given DGP. If the Experiment does have a vary_across component, then each element in the outermost list is a list of lists. The second layer of lists corresponds to a specific parameter setting within the vary_across scheme, and the innermost layer of lists is of length n_reps with the dataset replicates, generated by the DGP.


Method fit()

Fit Methods in the Experiment across all DGPs for n_reps repetitions and return results from fits.

Usage
Experiment$fit(
  n_reps = 1,
  parallel_strategy = "reps",
  future.globals = NULL,
  future.packages = NULL,
  future.seed = TRUE,
  use_cached = FALSE,
  return_all_cached_reps = FALSE,
  save = FALSE,
  checkpoint_n_reps = 0,
  verbose = 1,
  ...
)
Arguments
n_reps

The number of replicates of the Experiment for this run.

parallel_strategy

A vector with some combination of "reps", "dgps", or "methods". Determines how computation will be distributed across available resources. Currently only the default, "reps", is supported.

future.globals

Character vector of names in the global environment to pass to parallel workers. Passed as the argument of the same name to future.apply::future_lapply and related functions. To set for all runs of the experiment, use the same argument during initialization.

future.packages

Character vector of packages required by parallel workers. Passed as the argument of the same name to future.apply::future_lapply and related functions. To set for all runs of the experiment, use the same argument during initialization.

future.seed

Passed as the argument of the same name in future.apply::future_apply.

use_cached

Logical. If TRUE, find and return previously saved results. If cached results cannot be found, continue as if use_cached was FALSE.

return_all_cached_reps

Logical. If FALSE (default), returns only the fit results for the requested n_reps. If TRUE, returns fit results for the requested n_reps plus any additional cached replicates from the (DGP, Method) combinations in the Experiment.

save

If TRUE, save outputs to disk.

checkpoint_n_reps

The number of experiment replicates to compute before saving results to disk. If 0 (the default), no checkpoints are saved.

verbose

Level of verbosity. Default is 1, which prints out messages after major checkpoints in the experiment. If 2, prints additional debugging information for warnings and messages from user-defined functions (in addition to error debugging information). If 0, no messages are printed other than user-defined function error debugging information.

...

Additional ⁠future.*⁠ arguments to pass to future.apply functions. See future.apply::future_lapply() and future.apply::future_mapply().

Returns

A tibble containing the results from fitting all Methods across all DGPs for n_reps repetitions. In addition to results columns, has columns named '.rep', '.dgp_name', '.method_name', and the vary_across parameter names if applicable.


Method evaluate()

Evaluate the performance of method(s) across all Evaluator objects in the Experiment and return results.

Usage
Experiment$evaluate(
  fit_results,
  use_cached = FALSE,
  save = FALSE,
  verbose = 1,
  ...
)
Arguments
fit_results

A tibble, as returned by fit_experiment().

use_cached

Logical. If TRUE, find and return previously saved results. If cached results cannot be found, continue as if use_cached was FALSE.

save

If TRUE, save outputs to disk.

verbose

Level of verbosity. Default is 1, which prints out messages after major checkpoints in the experiment. If 2, prints additional debugging information for warnings and messages from user-defined functions (in addition to error debugging information). If 0, no messages are printed other than user-defined function error debugging information.

...

Not used.

Returns

A list of evaluation result tibbles, one for each Evaluator.


Method visualize()

Visualize the performance of methods and/or its evaluation metrics using all Visualizer objects in the Experiment and return visualization results.

Usage
Experiment$visualize(
  fit_results,
  eval_results = NULL,
  use_cached = FALSE,
  save = FALSE,
  verbose = 1,
  ...
)
Arguments
fit_results

A tibble, as returned by fit_experiment().

eval_results

A list of result tibbles, as returned by evaluate_experiment().

use_cached

Logical. If TRUE, find and return previously saved results. If cached results cannot be found, continue as if use_cached was FALSE.

save

If TRUE, save outputs to disk.

verbose

Level of verbosity. Default is 1, which prints out messages after major checkpoints in the experiment. If 2, prints additional debugging information for warnings and messages from user-defined functions (in addition to error debugging information). If 0, no messages are printed other than user-defined function error debugging information.

...

Not used.

Returns

A list of visualizations, one for each Visualizer.


Method add_dgp()

Add a DGP object to the Experiment.

Usage
Experiment$add_dgp(dgp, name = NULL, ...)
Arguments
dgp

A DGP object.

name

A name to identify the DGP.

...

Not used.

Returns

The Experiment object, invisibly.


Method update_dgp()

Update a DGP object in the Experiment.

Usage
Experiment$update_dgp(dgp, name, ...)
Arguments
dgp

A DGP object.

name

An existing name identifying the DGP to be updated.

...

Not used.

Returns

The Experiment object, invisibly.


Method remove_dgp()

Remove a DGP object from the Experiment.

Usage
Experiment$remove_dgp(name = NULL, ...)
Arguments
name

An existing name identifying the DGP to be removed.

...

Not used

Returns

The Experiment object, invisibly.


Method get_dgps()

Retrieve the DGP objects associated with the Experiment.

Usage
Experiment$get_dgps()
Returns

A named list of the DGP objects in the Experiment.


Method add_method()

Add a Method object to the Experiment.

Usage
Experiment$add_method(method, name = NULL, ...)
Arguments
method

A Method object.

name

A name to identify the Method to be updated.

...

Not used.

Returns

The Experiment object, invisibly.


Method update_method()

Update a Method object in the Experiment.

Usage
Experiment$update_method(method, name, ...)
Arguments
method

A Method object.

name

An existing name identifying the Method to be updated.

...

Not used.

Returns

The Experiment object, invisibly.


Method remove_method()

Remove a Method object from the Experiment.

Usage
Experiment$remove_method(name = NULL, ...)
Arguments
name

An existing name identifying the Method to be removed.

...

Not used

Returns

The Experiment object, invisibly.


Method get_methods()

Retrieve the Method objects associated with the Experiment.

Usage
Experiment$get_methods()
Returns

A named list of the Method objects in the Experiment.


Method add_evaluator()

Add an Evaluator object to the Experiment.

Usage
Experiment$add_evaluator(evaluator, name = NULL, ...)
Arguments
evaluator

An Evaluator object.

name

A name to identify the Evaluator.

...

Not used.

Returns

The Experiment object, invisibly.


Method update_evaluator()

Update an Evaluator object in the Experiment.

Usage
Experiment$update_evaluator(evaluator, name, ...)
Arguments
evaluator

An Evaluator object.

name

An existing name identifying the Evaluator to be updated.

...

Not used.

Returns

The Experiment object, invisibly.


Method remove_evaluator()

Remove an Evaluator object from the Experiment.

Usage
Experiment$remove_evaluator(name = NULL, ...)
Arguments
name

An existing name identifying the Evaluator to be removed.

...

Not used

Returns

The Experiment object, invisibly.


Method get_evaluators()

Retrieve the Evaluator objects associated with the Experiment.

Usage
Experiment$get_evaluators()
Returns

A named list of the Evaluator objects in the Experiment.


Method add_visualizer()

Add a Visualizer object to the Experiment.

Usage
Experiment$add_visualizer(visualizer, name = NULL, ...)
Arguments
visualizer

A Visualizer object.

name

A name to identify the Visualizer.

...

Not used.

Returns

The Experiment object, invisibly.


Method update_visualizer()

Update a Visualizer object in the Experiment.

Usage
Experiment$update_visualizer(visualizer, name, ...)
Arguments
visualizer

A Visualizer object.

name

An existing name identifying the Visualizer to be updated.

...

Not used.

Returns

The Experiment object, invisibly.


Method remove_visualizer()

Remove a Visualizer object from the Experiment.

Usage
Experiment$remove_visualizer(name = NULL, ...)
Arguments
name

An existing name identifying the Visualizer to be removed.

...

Not used

Returns

The Experiment object, invisibly.


Method get_visualizers()

Retrieve the Visualizer objects associated with the Experiment.

Usage
Experiment$get_visualizers()
Returns

A named list of the Visualizer objects in the Experiment. vary_across component is added and the Experiment is run, the Experiment is systematically varied across values of the specified parameter in the DGP or Method while all other parameters are held constant.


Method add_vary_across()

Usage
Experiment$add_vary_across(.dgp, .method, ...)
Arguments
.dgp

Name of DGP to vary in the Experiment. Can also be a DGP object that matches one in the Experiment or even a vector/list of DGP names/objects, assuming they all support the target arguments provided via ....

.method

Name of Method to vary in the Experiment. Can also be a Method object that matches one in the Experiment or even a vector/list of Method names/objects, assuming they all support the target arguments provided via ....

...

Any number of named arguments where names match an argument in the user-specified DGP or Method function and values are vectors (for scalar parameters) or lists (for arbitrary parameters).

Details

One of the .dgp or .method arguments (but not both) must be provided.

Returns

The Experiment object, invisibly.


Method update_vary_across()

Update a vary_across component in the Experiment.

Usage
Experiment$update_vary_across(.dgp, .method, ...)
Arguments
.dgp

Name of DGP to vary in the Experiment. Can also be a DGP object that matches one in the Experiment or even a vector/list of DGP names/objects, assuming they all support the target arguments provided via ....

.method

Name of Method to vary in the Experiment. Can also be a Method object that matches one in the Experiment or even a vector/list of Method names/objects, assuming they all support the target arguments provided via ....

...

Any number of named arguments where names match an argument in the user-specified DGP or Method function and values are vectors (for scalar parameters) or lists (for arbitrary parameters).

Details

One of the .dgp or .method arguments (but not both) must be provided.

Returns

The Experiment object, invisibly.


Method remove_vary_across()

Remove a vary_across component in the Experiment.

Usage
Experiment$remove_vary_across(dgp, method, param_names = NULL)
Arguments
dgp

Name of DGP to vary in the Experiment. Can also be a DGP object that matches one in the Experiment or even a vector/list of DGP names/objects.

method

Name of Method to vary in the Experiment. Can also be a Method object that matches one in the Experiment or even a vector/list of Method names/objects.

param_names

A character vector of parameter names to remove. If not provided, the entire set of vary_across parameters will be removed for the specified DGP/Method.

Details

If both the dgp and method arguments are not provided, then all vary_across parameters from the experiment are removed.

Returns

The Experiment object, invisibly.


Method get_vary_across()

Retrieve the parameters to vary across for each DGP and Method in the Experiment.

Usage
Experiment$get_vary_across()
Returns

a nested list with entries "dgp" and "method".


Method clear_cache()

Clear (or delete) cached results from an Experiment to start the experiment fresh/from scratch.

Usage
Experiment$clear_cache()
Returns

The Experiment object, invisibly.


Method get_cached_results()

Read in cached results from disk from a previously saved Experiment run.

Usage
Experiment$get_cached_results(results_type, verbose = 0)
Arguments
results_type

Character string indicating the type of results to read in. Must be one of "experiment", "experiment_cached_params", "fit", "eval", or "viz".

verbose

Level of verbosity. Default is 1, which prints out messages after major checkpoints in the experiment. If 2, prints additional debugging information for warnings and messages from user-defined functions (in addition to error debugging information). If 0, no messages are printed other than user-defined function error debugging information.

Returns

The cached results, specifically the cached Experiment object if results_type = "experiment", the cached fit results if results_type = "fit", the cached evaluation results if results_type = "eval", the cached visualization results if results_type = "viz", and the experiment parameters used in the cache if results_type = "experiment_cached_params".


Method set_doc_options()

Set R Markdown options for Evaluator or Visualizer outputs in the summary report. Some options include the height/width of plots and number of digits to show in tables.

Usage
Experiment$set_doc_options(field_name, name, show = NULL, nrows, ...)
Arguments
field_name

One of "evaluator" or "visualizer".

name

Name of Evaluator or Visualizer to set R Markdown options.

show

If TRUE, show output; if FALSE, hide output in R Markdown report. Default NULL does not change the "doc_show" field in Evaluator/Visualizer.

nrows

Maximum number of rows to show in the Evaluator's results table in the R Markdown report. If NULL, shows all rows. Default does not change the "doc_nrows" field in the Evaluator. Argument is ignored if field_name = "visualizer".

...

Named R Markdown options to set. If field_name = "visualizer", options are "height" and "width". If field_name = "evaluator", see options for vthemes::pretty_DT().

Returns

The Experiment object, invisibly.


Method get_save_dir()

Get the directory in which the Experiment's results and visualizations are saved.

Usage
Experiment$get_save_dir()
Returns

The relative path to where the Experiment's results and visualizations are saved.


Method set_save_dir()

Set the directory in which the Experiment's results and visualizations are saved.

Usage
Experiment$set_save_dir(save_dir)
Arguments
save_dir

The directory in which the Experiment's results will be saved.

Returns

The Experiment object, invisibly.


Method save()

Save the Experiment object to a .rds file under the Experiment's results directory (see get_save_dir()).

Usage
Experiment$save()
Returns

The Experiment object, invisibly.


Method export_visualizers()

Export all cached Visualizer results from an Experiment to images in the ⁠viz_results/⁠ directory under the Experiment's results directory (see get_save_dir()).

Usage
Experiment$export_visualizers(
  device = "pdf",
  width = "auto",
  height = "auto",
  ...
)
Arguments
device

See device argument of ggplot2::ggsave().

width

See width argument of ggplot2::ggsave().

height

See height argument of ggplot2::ggsave().

...

Additional arguments to pass to ggplot2::ggsave().

Returns

The Experiment object, invisibly.


Method print()

Print the Experiment in a nice format, showing the DGP, Method, Evaluator, Visualizers, and varied parameters involved in the Experiment.

Usage
Experiment$print()
Returns

The original Experiment object, invisibly.


Method clone()

The objects of this class are cloneable with this method.

Usage
Experiment$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

See Also

The following tidy helpers take an Experiment object as their first argument: create_experiment(), generate_data(), fit_experiment(), evaluate_experiment(), visualize_experiment(), run_experiment(), clear_cache(), get_cached_results(), get_save_dir(), set_save_dir(), save_experiment(), export_visualizers(), ⁠add_*()⁠, ⁠update_*()⁠, ⁠remove_*()⁠, ⁠get_*()⁠, and ⁠*_vary_across()⁠.


Yu-Group/simChef documentation built on March 25, 2024, 3:22 a.m.