scenario_analysis {idmodelr} | R Documentation |
A Function to Perform Scenario Analysis for a Generic Model Object.
Description
This function uses parameter permutations produced by
generate_parameter_permutations
to simulate from a supplied model function.
It can be used to examine multiple scenarios, with any number of parameter variations, for multiple samples.
Usage
scenario_analysis(
parameter_df,
variable_params = NULL,
model = NULL,
sim_fn = NULL,
summary_fn = NULL,
cores = 1,
rerun = FALSE,
verbose = FALSE,
by_row = FALSE,
test = FALSE,
...
)
Arguments
parameter_df |
A dataframe of parameter permutations as produced by |
variable_params |
A character vector containing the names of the parameters that are varied in |
model |
A model compatible with your |
sim_fn |
A generic simulation function, with the first argument as the model object,
a |
summary_fn |
A function which accepts a single dataframe argument customised to fit with the standard
output of |
cores |
The number of cores to use for the scenario analysis, defaults to 1. |
rerun |
A logical indicating if the function should be rerun or saved results should be loaded.
Defaults to |
verbose |
Logical (defaults to |
by_row |
Logical (defaults to |
test |
A logical (defaults to |
... |
Pass additional arguments to sim_fn. Only implemented when a single core is used. |
Value
A tidy dataframe containing simulated model trajectories for each scenario varied parameter combination. Use 'tidyr::unnest“ to examine all simulation results.
Examples
scenarios <- data.frame(scenario = c("test_1", "test_2"), scenario_param = c(0, 1))
variable_params <- data.frame(variable = c(0, 0.5, 1))
fixed_params <- c(fixed_1 = 2, fixed_2 = c(1, 3, 4))
sample_params <- c(sample_1 = 2, sample_2 = c(2, 1))
parameter_df <- generate_parameter_permutations(variable_params, fixed_params, sample_params,
excluded_params = c("variable"), scenarios,
parameter_samples = 10)
## set up dummy simulation function (returning an empty dataframe)
dummy_sim_fn <- function(object, inits, params, times, as.data.frame) {
x <- tibble::tibble()
return(x)
}
## Set up dummy summary function
dummy_sum_fn <- function(df){
df <- dplyr::mutate(df, summarised_simulations = simulations)
return(df)
}
dummy_model <- function(){}
## Run scenario analysis
scenario_analysis(parameter_df, variable_params = "variable", model = dummy_model,
sim_fn = dummy_sim_fn, cores = 1, summary_fn = dummy_sum_fn)