
Generate a line list from a branching process model parametrised by reproduction number
Source:R/simulation-utils.R
sim_branching_process.Rd
Generate a line list from a branching process model parametrised by reproduction number
Arguments
- changes
a dataframe containing a
t
time column andR
reproduction number parameter. This parameter is optional iffn_Rt
is specified- max_time
maximum duration of simulation
- seed
random seed
- fn_Rt
can be specified instead of
changes
df. This is a vectorised function that accepts a time parameter and returns a reproduction number. If both this andchanges
are specified this takes preference.- fn_ip
a function that takes input vector
t
(and/orclass
) and returns an infectivity profile at timest
.- fn_kappa
a vectorised function taking
t
and other imported case metadata returning a dispersion parameter controlling the likelihood of individual super-spreading. This must be between 1 andInf
with 1 being standard poisson dispersion and larger values representing over dispersion.- imports_df
a data frame containing minimally
time
andcount
columns plus any metadata about the imports in additional columns. Metadata columns can inform thefn_Rt
,fn_kappa
andfn_ip
functions as additional parameters.- fn_imports
a time varying function the defines the number of infected importations. If
imports_df
is defined then this is used instead- fn_list_next_gen
a named list of functions. The name corresponds to metadata columns in the simulation, the function is a
purrr
style mapping that will replace the old value in the named column with a new one. Such a function can be generated withcfg_transition_fn()
when a transition probability matrix is involved, of it can be specified directly as acase_when
style function. The function must be vectorised and assume no grouping structure. If the function has named parameters it can reference any of the metadata columns, or time (ast
). Thercategorical()
function may be useful in this scenario.- ...
not used
Value
a line list of cases, with individual ids, infection times, and infector ids, for a simulated outbreak
Examples
tmp = sim_branching_process(
changes = tibble::tibble(t = c(0,40), R = c(1.5,0.8)),
max_time = 120,
seed = 100,
fn_imports = ~ ifelse(.x<10,1,0)
)
#> .
#> .
#> .
#> .
#> .
#> .
#> .
#> .
#> .
#> .
#> .
#> .
#> .
#> .
#> .
#> .
#> .
#> .
#> .
#> .
#> .
#> .
#> .
#> .
#> .
#> .
#> .
#> .
#> .
#> complete
if(interactive()) {
plot_cases(tmp, mapping=ggplot2::aes(fill=as.factor(generation)),linewidth=0.1)
}
# imports can also be specified as a dataframe, which allows additional
# metadata in the line list. An example of which is as follows:
imports_df = tibble::tribble(
~time, ~variant, ~count,
0:4, "wild-type", 100,
10:14, "alpha", 5,
)