Skip to contents

Define a compartmental model in TMB. This model uses the spec https://canmod.net/misc/cpp_side.

Usage

TMBModel(
  init_mats = MatsList(),
  expr_list = ExprList(),
  params = OptParamsList(0),
  random = OptParamsList(),
  obj_fn = ObjectiveFunction(~0),
  time_steps = Time(0L)
)

Arguments

init_mats

An object of class MatsList.

expr_list

An object of class ExprList.

params

An object of class OptParamsList.

random

An object of class OptParamsList.

obj_fn

An object of class ObjectiveFunction.

time_steps

An object of class Time.

Value

Object of class TMBModel with the following methods.

Methods

  • $data_arg() -- Return all of the components of the data structure to pass to C++.

  • $param_arg() -- Return all of the components of the parameter list to pass to C++.

  • $simulator() -- Return an object of class TMBSimulator, which can be used to simulate data from the model.

Examples

sir = TMBModel(
  init_mats = MatsList(
    state = c(1 - 1e-2, 1e-2, 0),
    beta = 0.3,
    gamma = 0.2,
    N = 1,
    foi = 0,
    ratemat = matrix(0, 3, 3),
    flowmat = matrix(0, 3, 3),
    .mats_to_save = c("state", "N", "foi"),
    .mats_to_return = c("state", "N", "foi")
  ),
  expr_list = ExprList(
    before = list(
      N ~ sum(state)
    ),
    during = list(
      foi ~ beta * state[1, 0] / N,
      ratemat ~ matrix(c(
        0,   0,     0,
        foi, 0,     0,
        0,   gamma, 0), 3, 3),
      flowmat ~ ratemat * state,
      state ~ state - rowSums(flowmat) + t(colSums(flowmat))
    )
  ),
  params = OptParamsList(0.3
    , par_id = 0L
    , mat = "beta"
    , row_id = 0L
    , col_id = 0L
  ),
  random = OptParamsList(),
  obj_fn = ObjectiveFunction(~ foi + 1),
  time_steps = Time(time_steps = 30L)
)
sir$data_arg()
#> $mats
#> $mats[[1]]
#>      [,1]
#> [1,] 0.99
#> [2,] 0.01
#> [3,] 0.00
#> 
#> $mats[[2]]
#>      [,1]
#> [1,]  0.3
#> 
#> $mats[[3]]
#>      [,1]
#> [1,]  0.2
#> 
#> $mats[[4]]
#>      [,1]
#> [1,]    1
#> 
#> $mats[[5]]
#>      [,1]
#> [1,]    0
#> 
#> $mats[[6]]
#>      [,1] [,2] [,3]
#> [1,]    0    0    0
#> [2,]    0    0    0
#> [3,]    0    0    0
#> 
#> $mats[[7]]
#>      [,1] [,2] [,3]
#> [1,]    0    0    0
#> [2,]    0    0    0
#> [3,]    0    0    0
#> 
#> 
#> $mats_save_hist
#> [1]  TRUE FALSE FALSE  TRUE  TRUE FALSE FALSE
#> 
#> $mats_return
#> [1]  TRUE FALSE FALSE  TRUE  TRUE FALSE FALSE
#> 
#> $expr_output_id
#> [1] 3 4 5 6 0
#> 
#> $expr_sim_block
#> [1] 0 0 0 0 0
#> 
#> $expr_num_p_table_rows
#> [1]  2  8 13  3  8
#> 
#> $eval_schedule
#> [1] 1 4 0
#> 
#> $p_table_x
#>  [1] 11  0  3  2  3  1 16  0  0  1  9  8  2  3  4  5  6  4  7  8  9  2 10  2  5
#> [26]  0  0  1 18  0 13 14  6  6
#> 
#> $p_table_n
#>  [1]  1  0  2  2  0  0  3  0 -1 -1  3  9 -1 -1 -1 -1 -1  0 -1 -1 -1  0 -1  2  0
#> [26]  0  2  2  1  0  1  1  0  0
#> 
#> $p_table_i
#>  [1]  1  0  3  5  0  0  7  0  0  0 11 14  0  0  0  0  0  0  0  0  0  0  0 24  0
#> [26]  0 27 29 31  0 32 33  0  0
#> 
#> $p_par_id
#> [1] 0
#> 
#> $p_mat_id
#> [1] 1
#> 
#> $p_row_id
#> [1] 0
#> 
#> $p_col_id
#> [1] 0
#> 
#> $r_par_id
#> integer(0)
#> 
#> $r_mat_id
#> integer(0)
#> 
#> $r_row_id
#> integer(0)
#> 
#> $r_col_id
#> integer(0)
#> 
#> $o_table_x
#> [1]  0  4 11
#> 
#> $o_table_n
#> [1]  2  0 -1
#> 
#> $o_table_i
#> [1] 1 0 0
#> 
#> $literals
#>  [1] 1 0 3 3 0 0 0 0 0 0 0 1
#> 
#> $time_steps
#> [1] 30
#> 
sir$param_arg()
#> $params
#> [1] 0.3
#> 
#> $random
#> numeric(0)
#>