R6 Class for a Simulation Model
R6Sim.Rd
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 set_input()
Set Input
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.
Method get_inputs()
Get 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.
Method simulate()
Runs the simulation model for a single parameter set.
Method setup_run()
Sets parameters for a current run.
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")