c-rutter / imabc

https://c-rutter.github.io/imabc/
8 stars 1 forks source link
calibration r simulation

IMABC

CRAN
status R-CMD-check

Installation

You can install the latest released version from CRAN with:

install.packages("imabc")

You can install the development version from GitHub with:

install.packages("devtools")
devtools::install_github("c-rutter/imabc")

Other packages needed are:

Running the Code

Define Model Parameters/Priors

Parameters must have unique names (a unique name will be supplied if not given)

library(imabc)
priors <- define_priors(
  # x1: Uniform Prior (from base R)
  x1 = add_prior(
    dist_base_name = "unif",
    min = 0.2, max = 0.9
  ),
  # x2: Truncated Normal (from truncnorm package)
  add_prior(
    parameter_name = "x2",
    density_fn = "dtruncnorm",
    mean = 0.5, sd = 0.05, a = 0.4, b = 0.8, # a = min and b = max
    min = 0.4, max = 0.8 # User must specify both in truncnorm
  ),
  # V3: Fixed parameter (not calibrated)
  add_prior(0.5)
)

Define Target Values

Targets must have unique names (a unique name will be supplied if not given)

targets <- define_targets(
  # G1: Grouped targets include T1 and T2
  G1 = group_targets(
    T1 = add_target(
      target = 1.5,
      starting_range = c(1.0, 2.0),
      stopping_range = c(1.49, 1.51)
    ),
    add_target(
      target_name = "T2",
      target = 0.5,
      starting_range = c(0.2, 0.9),
      stopping_range = c(0.49, 0.51)
    )
  )
)

Define Target Function

The target function must return a vector whose length is equal to the number of targets. If the return vector is named using the target names then the order of the vector will be corrected by imabc(). If the return vector is not named, the targets should be added to the vector in the same order they were added to the targets object. Finalize the target function using the define_target_function to ensure the function is specified correctly for imabc().

fn1 <- function(x1, x2) { x1 + x2 + sample(c(-1, 1), 1)*rnorm(1, 0, 0.1) }
fn2 <- function(x1, x2) { x1 * x2 + sample(c(-1, 1), 1)*rnorm(1, 0, 0.1) }
fn <- function(x1, x2) {
  res <- c()
  res["T1"] <- fn1(x1, x2)
  res["T2"] <- fn2(x1, x2)
  return(res)
}
target_fun <- define_target_function(
  targets, priors, FUN = fn, use_seed = FALSE
)

Calibrate Model

The primary function is imabc(). The inputs and their descriptions are as follows:

calibration_results <- imabc(
  priors = priors,
  targets = targets,
  target_fun = target_fun,
  seed = 54321,
  N_start = 2000,
  N_centers = 2,
  Center_n = 500,
  N_cov_points = 50,
  N_post = 100
)

Some additional notes: * Regarding parallelization: If the user wishes to take advantage of parallelization they can register a parallel backend before running the imabc function (e.g. registerDoParallel(cores = detectCores() - 1)). imabc() uses foreach to submit parameters to the target function so what works with it will work with imabc. If the register function the user wishes to use requires that additional inputs beyond the parameters data.table, the target function, and the special other values listed in the backend_fun description be passed to foreach specifically the user must handle the entire submission of runs themselves by defining the backend_fun option in imabc. * At the moment, The distance between simulated targets and set target values (desired targets) is calculated following a chisquare ((desired - simulated)2)/(desired2)) except in the case where the desired target is 0. For desired targets = 0, the function replaces the denominator with 0.5 x range(stopping bounds): ((simulated)2)/((0.5 x (upper_bound_stop - lower_bound_stop))2). If the user wishes to use the stopping range to scale all the target distances they can set the global option imabc.target_eval_distance to “stopping_range” (i.e. before running imabc, run options(imabc.target_eval_distance = "stopping_range"))

To do