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_spec
ormp_tmb_library
, and possibly modified withmp_tmb_insert
andmp_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
distribution
for each trajectory. Transformations of trajectories cannot be named implicitly unless they are also transformed through theoutputs
argument 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
distribution
for 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_beta
will fitbeta
on the log-transformed scale, andmp_tmb_coef
will report estimates on the original scale by default. Themp_tmb_implicit_backtrans
function 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_ensemble
are 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_infection
will outputlog(infection)
, butmp_trajectory_sd
will reportinfection
and its confidence interval on the original scale). Themp_tmb_implicit_trans
function 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_update
to 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
default
list with identical results, but adding them toinits
is 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_bounds
andmp_sim_offset
to be more specific. See the example on themp_optimize
help 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
.
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)
#> outer mgc: 389.7835
#> outer mgc: 259.2344
#> outer mgc: 40.03711
#> outer mgc: 1.594322
#> outer mgc: 0.002775624
#> outer mgc: 8.4447e-09
#> $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))
}
#> outer mgc: 8.4447e-09
#> outer mgc: 11.74622
#> outer mgc: 12.05664
#> outer mgc: 31.01941
#> term mat row col default type estimate std.error
#> 1 params beta 0 0 0.25 fixed 0.2 0.009166433