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
.
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()
.
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 inrun_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 inrun_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 afterExperiment$name
when usingrun_experiment()
orfit_experiment()
withsave=TRUE
....
Not used.
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 ifuse_cached
wasFALSE
.return_all_cached_reps
Logical. If
FALSE
(default), returns only the fit results for the requestedn_reps
. IfTRUE
, returns fit results for the requestedn_reps
plus any additional cached replicates from the (DGP
,Method
) combinations in theExperiment
. Note that even ifreturn_all_cached_reps = TRUE
, only then_reps
replicates are used when evaluating and visualizing theExperiment
.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 thevary_across
parameter names if applicable.- eval_results
A list of tibbles containing results from the
evaluate
method, which evaluates eachEvaluator
in theExperiment
. Length of list is equivalent to the number ofEvaluators
.- viz_results
A list of tibbles containing results from the
visualize
method, which visualizes eachVisualizer
in theExperiment
. Length of list is equivalent to the number ofVisualizers
.
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.
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 ifuse_cached
wasFALSE
.return_all_cached_reps
Logical. If
FALSE
(default), returns only the fit results for the requestedn_reps
. IfTRUE
, returns fit results for the requestedn_reps
plus any additional cached replicates from the (DGP
,Method
) combinations in theExperiment
.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. Seefuture.apply::future_lapply()
andfuture.apply::future_mapply()
.
Method evaluate()
Evaluate the performance of method(s) across all
Evaluator objects in the Experiment
and return results.
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 ifuse_cached
wasFALSE
.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.
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 ifuse_cached
wasFALSE
.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.
Method update_dgp()
Update a DGP object in the Experiment
.
Method remove_dgp()
Remove a DGP object from the Experiment
.
Method get_dgps()
Retrieve the DGP objects associated with the Experiment
.
Method add_method()
Add a Method object to the Experiment
.
Method update_method()
Update a Method object in the Experiment
.
Method remove_method()
Remove a Method object from the Experiment
.
Method get_methods()
Retrieve the Method objects associated with the Experiment
.
Method add_evaluator()
Add an Evaluator object to the Experiment
.
Method update_evaluator()
Update an Evaluator object in the Experiment
.
Method remove_evaluator()
Remove an Evaluator object from the Experiment
.
Method get_evaluators()
Retrieve the Evaluator objects associated with the Experiment
.
Method add_visualizer()
Add a Visualizer object to the Experiment
.
Method update_visualizer()
Update a Visualizer object in the Experiment
.
Method remove_visualizer()
Remove a Visualizer object from the Experiment
.
Method get_visualizers()
Retrieve the Visualizer objects associated with the Experiment
.
Method add_vary_across()
Arguments
.dgp
Name of
DGP
to vary in theExperiment
. Can also be aDGP
object that matches one in theExperiment
or even a vector/list ofDGP
names/objects, assuming they all support the target arguments provided via...
..method
Name of
Method
to vary in theExperiment
. Can also be aMethod
object that matches one in theExperiment
or even a vector/list ofMethod
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
orMethod
function and values are vectors (for scalar parameters) or lists (for arbitrary parameters).
Method update_vary_across()
Update a vary_across
component in the Experiment
.
Arguments
.dgp
Name of
DGP
to vary in theExperiment
. Can also be aDGP
object that matches one in theExperiment
or even a vector/list ofDGP
names/objects, assuming they all support the target arguments provided via...
..method
Name of
Method
to vary in theExperiment
. Can also be aMethod
object that matches one in theExperiment
or even a vector/list ofMethod
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
orMethod
function and values are vectors (for scalar parameters) or lists (for arbitrary parameters).
Method remove_vary_across()
Remove a vary_across
component in the Experiment
.
Arguments
dgp
Name of
DGP
to vary in theExperiment
. Can also be aDGP
object that matches one in theExperiment
or even a vector/list ofDGP
names/objects.method
Name of
Method
to vary in theExperiment
. Can also be aMethod
object that matches one in theExperiment
or even a vector/list ofMethod
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 specifiedDGP
/Method
.
Method get_vary_across()
Retrieve the parameters to vary across for each DGP
and
Method
in the Experiment
.
Method clear_cache()
Clear (or delete) cached results from an Experiment
to
start the experiment fresh/from scratch.
Method get_cached_results()
Read in cached results from disk from a previously saved
Experiment
run.
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.
Arguments
field_name
One of "evaluator" or "visualizer".
name
Name of
Evaluator
orVisualizer
to set R Markdown options.show
If
TRUE
, show output; ifFALSE
, hide output in R Markdown report. DefaultNULL
does not change the "doc_show" field inEvaluator
/Visualizer
.nrows
Maximum number of rows to show in the
Evaluator
's results table in the R Markdown report. IfNULL
, shows all rows. Default does not change the "doc_nrows" field in theEvaluator
. Argument is ignored iffield_name = "visualizer"
....
Named R Markdown options to set. If
field_name = "visualizer"
, options are "height" and "width". Iffield_name = "evaluator"
, see options forvthemes::pretty_DT()
.
Method get_save_dir()
Get the directory in which 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.
Method save()
Save the Experiment
object to a .rds file under the
Experiment
's results directory (see get_save_dir()
).
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()
).
Arguments
device
See
device
argument ofggplot2::ggsave()
.width
See
width
argument ofggplot2::ggsave()
.height
See
height
argument ofggplot2::ggsave()
....
Additional arguments to pass to
ggplot2::ggsave()
.
Method print()
Print the Experiment
in a nice format, showing the
DGP
, Method
, Evaluator
, Visualizers
, and varied parameters
involved in the Experiment
.