Construct an object that can get used to calibrate an object produced by
mp_tmb_model_spec or mp_tmb_library,
and possibly modified by mp_tmb_insert or
mp_tmb_update. Note that the output of this function is
not a model that has been calibrated to data, but rather an object that
contains a model that can be calibrated. To actually attempt a
calibration one needs the mp_optimize function (see
examples below).
Arguments
- spec
An TMB model spec to fit to data. Such specs can be produced by
mp_tmb_model_specormp_tmb_library, and possibly modified withmp_tmb_insertandmp_tmb_update.- data
A data frame containing trajectories to fit to and possibly time-varying parameters. The data must be of the same format as that produced by
mp_trajectory.- traj
A character vector giving the names of trajectories to fit to data, or a named list of likelihood distributions specified with a
distributionfor each trajectory. Transformations of trajectories cannot be named implicitly unless they are also transformed through theoutputsargument and their transformed version appears in thedata.- par
A character vector giving the names of parameters (either time-varying or not) to fit using trajectory match, or a named list of prior distributions specified with a
distributionfor each parameter. Parameters can be implicitly fitted on a transformed scale by prefixing the name of the parameter with the name of the transformation (e.g.,log_betawill fitbetaon the log-transformed scale, andmp_tmb_coefwill report estimates on the original scale by default. Themp_tmb_implicit_backtransfunction is used internally. See that help page for available transformations.- tv
A character vector giving the names of parameters to make time-varying according to the values in
data, or a radial basis function specified withmp_rbf.- outputs
A character vector of outputs that will be generated when
mp_trajectory,mp_trajectory_sd, ormp_trajectory_ensembleare called on the optimized calibrator. By default it is just the trajectories listed intraj. Outputs can be implicitly transformed by prefixing the name of the output with the name of the transformation (e.g.,log_infectionwill outputlog(infection), butmp_trajectory_sdwill reportinfectionand its confidence interval on the original scale). Themp_tmb_implicit_transfunction is used internally. See that help page for available transformations.- default
A list of default values to use to update the defaults in the
spec. By default nothing is updated. Alternatively one could usemp_tmb_updateto update the spec outside of the function. Indeed such an approach is necessary if new expressions, in addition to default updates, need to be added to the spec (e.g. seasonally varying transmission).- inits
An optional list of initial values for the state variables. These initial values can be added to the
defaultlist with identical results, but adding them toinitsis better practice because it makes it clear that they are initial values that will change as the state updates.- time
Specify the start and end time of the simulated trajectories, and the time period associated with each time step. The default is
NULL, which takes simulation bounds from thedata. You can usemp_sim_boundsandmp_sim_offsetto be more specific. See the example on themp_optimizehelp file for an illustration the use ofmp_sim_offset.- save_data
Save a copy of the data in the calibrator object that is returned, so that you do not need to pass the data manually to downstream functions like
mp_forecaster. It the resulting calibrator object is so large that it causes you problems, consider not saving the data in the calibrator object and manually passing it to the data argument ofmp_forecaster.- optimize
Call
mp_optimizeon the resulting calibrator object, before returning it. The default isFALSEso that you can control when you would like to spend time optimizing.
Examples
spec = mp_tmb_library("starter_models", "sir", package = "macpan2")
sim = mp_simulator(spec, 50, "infection")
data = mp_trajectory(sim)
cal = mp_tmb_calibrator(
spec
, data
, traj = "infection"
, par = "beta"
, default = list(beta = 0.25)
)
mp_optimize(cal)
#> $par
#> params
#> 0.2
#>
#> $objective
#> [1] 49.74796
#>
#> $convergence
#> [1] 0
#>
#> $iterations
#> [1] 5
#>
#> $evaluations
#> function gradient
#> 6 6
#>
#> $message
#> [1] "relative convergence (4)"
#>
if (suppressPackageStartupMessages(require(broom.mixed))) {
print(mp_tmb_coef(cal))
}
#> term mat row col default type estimate std.error
#> 1 params beta 0 0 0.25 fixed 0.2 0.009166433
