Skip to content

Commit d5f0079

Browse files
documentation edits for flows and specifications
1 parent 530d78b commit d5f0079

File tree

7 files changed

+149
-99
lines changed

7 files changed

+149
-99
lines changed

R/formula_list_generators.R

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ HazardUpdateMethod = function(change_model) {
808808
#' Specify different kinds of flows between compartments.
809809
#'
810810
#' The examples below can be mixed and matched in `mp_tmb_model_spec()`
811-
#' to produce compartmental models. Note that the symbols used below must
811+
#' to produce compartmental models. The symbols used below must
812812
#' be used in an appropriate context (e.g., if `N` is used for total population
813813
#' size, then there must be an expression like `N ~ S + I + R` somewhere in
814814
#' the model or for models with constant population size there must be a
@@ -818,20 +818,23 @@ HazardUpdateMethod = function(change_model) {
818818
#' originates.
819819
#' @param to String giving the name of the compartment to which the flow is
820820
#' going.
821-
#' @param rate String giving the expression for the per-capita or absolute
822-
#' flow rate. Alternatively for per-capita flows, and for back compatibility,
821+
#' @param rate String giving the expression for the per-capita
822+
#' flow rate. Alternatively, and for back compatibility,
823823
#' a two-sided formula with the left-hand-side giving the name of the absolute
824-
#' flow rate per unit time-stepand the right-hand-side giving an expression for
824+
#' flow rate per time-step and the right-hand-side giving an expression for
825825
#' the per-capita rate of flow from `from` to `to`.
826-
#' @param abs_rate String giving the name for the absolute flow rate.
827-
#' By default, during simulations, the absolute flow rate will be computed as
828-
#' `from * rate`. This default behaviour will simulate the compartmental model
829-
#' as discrete difference equations, but this default can be changed to use
830-
#' other approaches (see \code{\link{state_updates}}).
831-
#' If a formula is passed to `rate` (not recommended for better readability),
832-
#' then this `abs_rate` argument will be ignored.
826+
#' @param abs_rate String giving the name for the absolute flow rate per
827+
#' time-step. By default, during simulations, the absolute flow rate will be
828+
#' computed as `from * rate`. This default behaviour will simulate the
829+
#' compartmental model as discrete difference equations, but this can
830+
#' be changed to use other approaches such as ordinary differential equations
831+
#' or stochastic models (see \code{\link{state_updates}}). If a formula is
832+
#' passed to `rate` (not recommended for better readability), then this
833+
#' `abs_rate` argument will be ignored.
833834
#' @param rate_name String giving the name for the absolute flow rate.
834835
#'
836+
#' @seealso [mp_absolute_flow()]
837+
#'
835838
#' @examples
836839
#'
837840
#' # infection by mass action
@@ -896,16 +899,31 @@ mp_per_capita_outflow = function(from, rate, abs_rate = NULL) {
896899
PerCapitaOutflow(from, rate, call_string)
897900
}
898901

899-
#' @describeIn mp_per_capita_flow Experimental
902+
903+
#' Specify Absolute Flow Between Compartments (Experimental)
904+
#'
905+
#' An experimental alternative to \code{\link{mp_per_capita_flow}} that
906+
#' allows users to specify flows using absolute rates instead of
907+
#' per-capita rates.
908+
#'
909+
#' @param from String giving the name of the compartment from which the flow
910+
#' originates.
911+
#' @param to String giving the name of the compartment to which the flow is
912+
#' going.
913+
#' @param rate String giving the expression for the absolute
914+
#' flow rate per time-step.
915+
#' @param rate_name String giving the name for the variable that
916+
#' will store the `rate`.
917+
#'
918+
#' @seealso [mp_per_capita_flow()]
919+
#'
900920
#' @export
901921
mp_absolute_flow = function(from, to, rate, rate_name = NULL) {
902922
call_string = deparse(match.call())
903923
rate = handle_abs_rate_args(rate, rate_name)
904924
AbsoluteFlow(from, to, rate, call_string)
905925
}
906926

907-
908-
909927
PerCapitaOutflow = function(from, rate, call_string) {
910928
self = PerCapitaFlow(from, NULL, rate, call_string)
911929
self$change_frame = function() {
@@ -1037,16 +1055,9 @@ to_change_component.ChangeComponent = function(x) x
10371055
to_change_component.formula = function(x) Formula(x)
10381056

10391057

1040-
#' Reduce Model
1041-
#'
1042-
#' Reduce a model by removing any model structure
1043-
#' (e.g. \code{\link{mp_per_capita_flow}}), so that expression lists
1044-
#' are plain R formulas.
1045-
#'
1046-
#' @param model A model object.
1047-
#'
1048-
#' @describeIn mp_expand Synonym of `mp_expand` present only for
1049-
#' back-compatibility. Please use `mp_expand` in new projects.
1058+
#' @describeIn mp_expand Confusingly, `mp_reduce` and `mp_expand` are synonyms.
1059+
#' Please use `mp_expand` in new projects, as `mp_reduce` is available for
1060+
#' back-compatibility only.
10501061
#' @export
10511062
mp_reduce = function(model) UseMethod("mp_reduce")
10521063

R/mp_tmb_model_spec.R

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -254,50 +254,59 @@ must_save_time_args = function(formulas) {
254254
#' Specify a simulation model in the TMB engine. A detailed explanation of this
255255
#' function is covered in `vignette("quickstart")`.
256256
#'
257-
#' @param before List of expressions to be evaluated (in the order provided)
258-
#' before the simulation loop begins. Expressions can either be standard
259-
#' R \code{\link{formula}} objects or calls to flow functions (e.g.,
260-
#' \code{\link{mp_per_capita_flow}}). Formulas must have a left hand
261-
#' side that gives the name of the matrix being updated, and a right hand side
262-
#' giving an expression containing only (1) the names of quantities in the
263-
#' model, (2) functions defined in the TMB engine, and (3) numerical literals
264-
#' (e.g., \code{3.14}). The available functions in the TMB engine can be
265-
#' described in \code{\link{engine_functions}}. Names can be provided for the
266-
#' components of \code{before}, and these names do not have to be unique. These
267-
#' names are used by the \code{sim_exprs} argument.
257+
#' @param before List of formulas to be evaluated (in the order provided)
258+
#' before the simulation loop begins. These formulas must be standard
259+
#' two-sided R \code{\link{formula}} objects. See `details` below for the
260+
#' rules for these formulas.
268261
#' @param during List of formulas or calls to flow functions (e.g.,
269262
#' \code{\link{mp_per_capita_flow}}) to be evaluated at every iteration of the
270-
#' simulation loop, with the same rules as \code{before}.
271-
#' @param after List of formulas or calls to flow functions (e.g.,
272-
#' \code{\link{mp_per_capita_flow}}) to be evaluated after the simulation loop,
273-
#' with the same rules as \code{before}.
263+
#' simulation loop.
264+
#' @param after List of formulas to be evaluated (in the order provided)
265+
#' before the simulation loop begins. These formulas must be standard
266+
#' two-sided R \code{\link{formula}} objects. See `details` below for the
267+
#' rules for these formulas.
274268
#' @param default Named list of objects, each of which can be coerced into
275269
#' a \code{\link{numeric}} \code{\link{matrix}}. The names refer to
276270
#' variables that appear in \code{before}, \code{during}, and \code{after}.
277271
#' @param integers Named list of vectors that can be coerced to integer
278272
#' vectors. These integer vectors can be used by name in model formulas to
279273
#' provide indexing of matrices and as grouping factors in
280274
#' \code{\link{group_sums}}.
281-
#' @param must_save Character vector of the names of matrices that must have
275+
#' @param must_save Character vector of the names of variables that must have
282276
#' their values stored at every iteration of the simulation loop. For example,
283-
#' a matrix that the user does not want to be returned but that impacts dynamics
284-
#' with a time lag must be saved and therefore in this list.
285-
#' @param must_not_save Character vector of the names of matrices that must
277+
#' a variable that you do not want to be returned, but that impacts
278+
#' dynamics with a time lag, must be saved and therefore must be in this list.
279+
#' @param must_not_save Character vector of the names of variables that must
286280
#' not have their values stored at every iteration of the simulation loop. For
287281
#' example, the user may ask to return a very large matrix that would create
288282
#' performance issues if stored at each iteration. The creator of the model
289-
#' can mark such matrices making it impossible for the user of the model to
283+
#' can mark such variables making it impossible for the user of the model to
290284
#' save their full simulation history.
291285
#' @param sim_exprs Character vector of the names of \code{before},
292286
#' \code{during}, and \code{after} expressions that must only be evaluated
293287
#' when simulations are being produced and not when the objective function is
294288
#' being evaluated. For example, expressions that generate stochasticity should
295289
#' be listed in \code{sim_exprs} because TMB objective functions must be
296290
#' continuous.
297-
#' @param state_update (experimental) Optional character vector for how to
298-
#' update the state variables when it is relevant. Options include `"euler"`,
291+
#' @param state_update Optional character vector for how to update the state
292+
#' variables when it is relevant. Options include `"euler"` (the default),
299293
#' `"rk4"`, and `"euler_multinomial"`.
300294
#'
295+
#' @details
296+
#' Expressions in the `before`, `during`, and `after` lists can be standard
297+
#' R \code{\link{formula}} objects for defining variables in the model. These
298+
#' formulas must have a left hand side that gives the name of the (possibly
299+
#' matrix-valued) variable being updated, and a right hand side giving an
300+
#' expression containing only (1) the names of quantities in the model, (2)
301+
#' numerical literals (e.g., \code{3.14}), or (3) functions defined in the TMB
302+
#' engine (described in \code{\link{engine_functions}}). For example, the
303+
#' expression `N ~ S + I + R` updates the value of `N` to be the sum of the
304+
#' variables `S`, `I`, and `R`.
305+
#'
306+
#' Names can be provided for the components of the `before`, `during`, and
307+
#' `after` lists, and these names do not have to be unique. These names are
308+
#' used by the \code{sim_exprs} argument.
309+
#'
301310
#' @examples
302311
#' ## A simple SI model.
303312
#' spec = mp_tmb_model_spec(
@@ -337,8 +346,6 @@ must_save_time_args = function(formulas) {
337346
#' |> mp_trajectory()
338347
#' )
339348
#'
340-
#'
341-
#'
342349
#' @concept create-model-spec
343350
#' @export
344351
mp_tmb_model_spec = TMBModelSpec

man/mp_absolute_flow.Rd

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/mp_expand.Rd

Lines changed: 3 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/mp_per_capita_flow.Rd

Lines changed: 15 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/mp_tmb_insert.Rd

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)