
Generate a line list from a branching process model parametrised by reproduction number
Source:R/simulation-utils.R
sim_branching_process.RdGenerate a line list from a branching process model parametrised by reproduction number
Arguments
- changes
a dataframe containing a
ttime column andRreproduction number parameter. This parameter is optional iffn_Rtis specified- max_time
maximum duration of simulation
- seed
random seed
- fn_Rt
can be specified instead of
changesdf. This is a vectorised function that accepts a time parameter and returns a reproduction number. If both this andchangesare 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
tand other imported case metadata returning a dispersion parameter controlling the likelihood of individual super-spreading. This must be between 1 andInfwith 1 being standard poisson dispersion and larger values representing over dispersion.- imports_df
a data frame containing minimally
timeandcountcolumns plus any metadata about the imports in additional columns. Metadata columns can inform thefn_Rt,fn_kappaandfn_ipfunctions as additional parameters.- fn_imports
a time varying function the defines the number of infected importations. If
imports_dfis 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
purrrstyle 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_whenstyle 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 for a simulated outbreak
A dataframe containing the following columns:
id (unique_id) - Patient level unique id
time (ggoutbreak::time_period) - Time of infection. A `time_period`
Any grouping possible.
Examples
tmp = sim_branching_process(
changes = dplyr::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 = dplyr::tribble(
~time, ~variant, ~count,
0:4, "wild-type", 100,
10:14, "alpha", 5,
)