Skip to contents

Search for the appropriate value of unknown parameters to obtain specific model results.

Usage

calibrate_model(
  x,
  parameter_names,
  fn_values,
  target_values,
  initial_values = NULL,
  method = c("Nelder-Mead", "BFGS", "L-BFGS-B"),
  ...
)

Arguments

x

Result from run_model() or update().

parameter_names

Names of the parameters to calibrate.

fn_values

Function applied to the model that returns the values of interest as a numeric vector.

target_values

Values to match, same length as the output from fn_values.

initial_values

Optional starting values. See details.

method

Optimisation method (Nelder-Mead, BFGS, or L-BFGS-B).

...

Optional arguments passed to optimx::optimx().

Value

A data frame in which each row has the calibrated values of parameters given in parameter_names, for the corresponding row of initial_values, along with the convergence code for each run.

Details

Parameters not being optimized are unchanged from the values in the model run. If initial_values is NULL, the initial parameter values will also be taken from the model run.

initial_values can be a vector or a table. In the second case each row corresponds to a set of initial parameter values: the calibration will be run once per set.

Passing in multiple initial values allows (among other things) the user to check whether the calibration gets the same results from different starting points.

Multi-dimensional problems are optimized with optimx::optimx(), 1-dimensional problems with stats::optimise() (except when a method is given). convcode is always NA with stats::optimise().

Running calibrate_model() does not change the model parameters; the user must create a new model and run it if desired.

See also vignette("k-calibration").

Examples


param <- define_parameters(p = 0.8)

mat <- define_transition(
  p, C,
  0, 1
)
#> No named state -> generating names.
mod <- define_strategy(
  transition = mat,
  A = define_state(cost=10, effect = 0.5), 
  B = define_state(cost = 5, effect = 0.8)
)

res_mod <- run_model(
  mod = mod,
  parameters = param,
  init = c(1000L, 0L),
  cycles = 10,
  cost = cost,
  effect = effect,
  method = "end"
)

f <- function(x) {
  dplyr::filter(
    get_counts(x),
    state_names == "A" & model_time == 10
  )$count
}
f(res_mod)
#> [1] 107.3742

#'\dontrun{
#'calibrate_model(
#'  res_mod,
#'  parameter_names = "p",
#'  fn_values = f,
#'  target_values = 130,
#'  initial_values = data.frame(p = c(0.5, 0.9)),
#'  lower = 0, upper = 1
#')
#'}