Skip to contents

Base class for building simulation models with R6. Provides methods for managing inputs, parameters, and simulation execution.

Details

The R6Sim class includes functionality for: * Input and parameter management * JSON serialization * Parallel execution * Parameter sampling

Public fields

name

is a character string representing the model name.

inputs

is a list of model inputs. One can add inputs to the object with the set_input function.

inputs_table

is a data.frame listing all inputs added to the model.

params_df

is a data.frame containing parameters to run the model. Not to be confused with the params vector withi the inputs list, which is what should be used by the simulation model. Each column is a parameter, each row is a parameter set. The set_posterior distribution will be used to set the posterior distribution from a bayesian calibration.

sim_results

is the object that will receive results from the simulation function.

simulate_fn

is a function that will take as parameters self and ..., simulate the natural history of crc and returns invisible(self).

Methods


Method new()

Create a new `R6Sim` object.

Usage

R6Sim$new(name)

Arguments

name

name of the model to be created.

Returns

s new `R6Sim` object.


Method set_input()

Set Input

Usage

R6Sim$set_input(name, value, type = NA_character_)

Arguments

name

Character string defining the input name

value

Input value. Can be a single value, list or vector.

type

Optional character string defining input type.

Details

Validates input types and maintains input registry. Accepts numeric, character, logical, data.frame and list inputs. Type tags enable selective JSON export.

Examples

model$set_input("population", 1000, type = "parameter")
model$set_input("growth_rates", c(0.01, 0.02), type = "scenario")
model$set_input("settings", list(iterations = 100), type = "config")


Method get_inputs()

Get Inputs

Usage

R6Sim$get_inputs(source)

Arguments

source

either a path to a xlsx or yaml file, or a function that returns a named list of new inputs to be added to the model.

Details

Use this function to get model inputs from a spreadsheet or yaml file


Method to_json()

Converts a `R6Sim` to a JSON string

Usage

R6Sim$to_json(input_types)

Arguments

input_types

vector of types of input to include in the json object.

Returns

a JSON string containing the R6Sim objects that should be exported


Method set_inputs_from_json()

Set model Inputs from JSON string

Usage

R6Sim$set_inputs_from_json(json)

Arguments

json

a JSON string generated by the model_to_json function

Details

Use this function to set model inputs from a JSON string. Note that the posterior distribution is not included in the json strong of model inputs.


Method set_param_dist()

Set distribution of model parameters

Usage

R6Sim$set_param_dist(
  params_list,
  param_dist_weights,
  cols_to_ignore = NULL,
  n_sample = 1000,
  use_average = F,
  seed = 12345678,
  resample = T
)

Arguments

params_list

named list of one more more data.frames containing a distribution of model parameters.

param_dist_weights

character indicating the name of the column that contain weights to be used when sampling from the posterior

cols_to_ignore

character vector of columns name to ignore. This is useful when the posterior files contain columns that are not parameters and are not inputs to the model.

n_sample

the size of the sample to take from each posterior file.

use_average

T if one wants to use the average value of all parameters rather than the mean

seed

random seed to use when sampling from the posterior

resample

if T, samples from the posterior using the posterior weights. Otherwise, returns the full posterior, and preservers the weights.

Details

Use this function to set a distribution of model parameters to the model. This is useful when we want to sample from a posterior distribution of model parameters.


Method simulate()

Runs the simulation model for a single parameter set.

Usage

R6Sim$simulate(...)

Arguments

...

any set of parameters passed to this function will be passed along to the model simulate function.

Details

The simulate method should be used to simulate a model run. All inputs used by the model should already have been defined before this function is called.


Method setup_run()

Sets parameters for a current run.

Usage

R6Sim$setup_run(...)

Arguments

...

any set of parameters passed to this function will be passed along to the user natural history function.

Details

This function is a wrapper around the user natural history function. It passes the `self` object and any other parameters provided to the function.


Method clone()

The objects of this class are cloneable with this method.

Usage

R6Sim$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

# Create simulation model
MyModel <- R6::R6Class(
  "MyModel",
  inherit = R6Sim,
  public = list(
    initialize = function(name) {
      super$initialize(name)
      self$set_input("population", 1000)
      self$set_input("growth_rate", 0.05)
    },
    simulate = function(...) {
      pop <- self$inputs$population
      growth <- self$inputs$growth_rate
      results <- pop * (1 + growth)^(1:10)
      return(data.frame(year = 1:10, population = results))
    }
  )
)

model <- MyModel$new("pop_model")
results <- model$simulate()


## ------------------------------------------------
## Method `R6Sim$set_input`
## ------------------------------------------------

model$set_input("population", 1000, type = "parameter")
model$set_input("growth_rates", c(0.01, 0.02), type = "scenario")
model$set_input("settings", list(iterations = 100), type = "config")