Skip to contents

A helper function for developing new Visualizer plotting functions that plot results from particular replicate(s) in the Experiment fit. This function will construct one plot for each row in the Experiment's fit_results from the specified replicates.

Usage

plot_fit_constructor(
  fit_results,
  vary_params = NULL,
  reps = 1,
  plot_fun,
  interactive = FALSE,
  ...
)

Arguments

fit_results

A tibble, as returned by fit_experiment().

vary_params

A vector of DGP or Method parameter names that are varied across in the Experiment.

reps

Vector of replicates from which to plot results.

plot_fun

The plotting function, which takes in the arguments fit_results, vary_params, and possibly others passed from ....

interactive

Logical. If TRUE, returns interactive plotly plots. If FALSE, returns static ggplot plots.

...

Additional arguments to pass to plot_fun().

Value

If interactive = TRUE, returns a plotly object or list of plotly objects if there are multiple replicates, DGPs, or Methods to plot. If interactive = FALSE, returns a ggplot

object or list of ggplot objects if there are multiple replicates, DGPs, or Methods to plot.

Examples

# generate example fit results data
fit_results <- tibble::tibble(
  .rep = rep(1:2, times = 2),
  .dgp_name = c("DGP1", "DGP1", "DGP2", "DGP2"),
  .method_name = c("Method"),
  # true response
  y = lapply(1:4, FUN = function(x) rnorm(100)),
  # predicted response
  predictions = lapply(1:4, FUN = function(x) rnorm(100))
)

# function to plot scatter plot of y vs predictions
plot_fun <- function(fit_results, vary_params = NULL) {
  plt <- fit_results %>%
    tidyr::unnest(c("y", "predictions")) %>%
    ggplot2::ggplot() +
    ggplot2::aes(x = y, y = predictions) +
    ggplot2::geom_point() +
    ggplot2::labs(title = sprintf("DGP: %s | Method: %s | Rep: %s",
                                  fit_results$.dgp_name,
                                  fit_results$.method_name,
                                  fit_results$.rep))
  return(plt)
}

# returns the scatter plot for each (DGP, Method) combination from rep 1
plt <- plot_fit_constructor(fit_results, reps = 1, plot_fun = plot_fun)