| Title: | Generate and Simulate Deterministic Compartmental Models |
|---|---|
| Description: | R package to build and simulate deterministic compartmental models that can be semi-Markovian. Length of stay in each compartment can be defined to follow a parametric distribution (d_exponential(), d_gamma(), d_weibull(), d_lognormal()) or a non-parametric distribution (nonparametric()). |
| Authors: | Thinh Ong [aut, cph] (ORCID: <https://orcid.org/0000-0001-6772-9291>), Anh Phan [aut, cre] (ORCID: <https://orcid.org/0009-0000-2129-435X>), Marc Choisy [aut] (ORCID: <https://orcid.org/0000-0002-5187-6390>), Niels Lohman [ctb, cph] (Author of the bundled JSON C++ library), Bjoern Hoehrmann [ctb, cph] (Author of UTF-8 validation code in the bundled JSON C++ library), Florian Loitsch [ctb, cph] (Author of the binary to decimal floating-point conversion code in the bundled JSON C++ library), Ingo Berg [ctb, cph] (Author of the bundled MuParser C++ library) |
| Maintainer: | Anh Phan <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.2.3 |
| Built: | 2026-05-20 07:34:27 UTC |
| Source: | https://github.com/thinhong/denim |
Simulate deterministic model
Imports
Maintainer: Anh Phan [email protected] (ORCID)
Authors:
Thinh Ong [email protected] (ORCID) [copyright holder]
Marc Choisy [email protected] (ORCID)
Other contributors:
Niels Lohman (Author of the bundled JSON C++ library) [contributor, copyright holder]
Bjoern Hoehrmann [email protected] (Author of UTF-8 validation code in the bundled JSON C++ library) [contributor, copyright holder]
Florian Loitsch (Author of the binary to decimal floating-point conversion code in the bundled JSON C++ library) [contributor, copyright holder]
Ingo Berg (Author of the bundled MuParser C++ library) [contributor, copyright holder]
Useful links:
Report bugs at https://github.com/thinhong/denim/issues
Discrete exponential distribution
d_exponential(rate, dist_init = FALSE)d_exponential(rate, dist_init = FALSE)
rate |
rate parameter of an exponential distribution |
dist_init |
whether to distribute initial value across subcompartments following this distribution. (default to FALSE, meaning init value is always in the first compartment) |
a Transition object for simulator
transitions <- list("I -> D" = d_exponential(0.3)) transitions <- denim_dsl({I -> D = d_exponential(0.3)})transitions <- list("I -> D" = d_exponential(0.3)) transitions <- denim_dsl({I -> D = d_exponential(0.3)})
Discrete gamma distribution
d_gamma(rate, shape, dist_init = FALSE)d_gamma(rate, shape, dist_init = FALSE)
rate |
rate parameter of a gamma distribution |
shape |
shape parameter of a gamma distribution |
dist_init |
whether to distribute initial value across subcompartments following this distribution. |
a Transition object for simulator
transitions <- list("S -> I" = d_gamma(rate = 1, shape = 5)) transitions_dsl <- denim_dsl({S -> I = d_gamma(rate = 1, shape = 5)}) # define model parameters as distributional parameters transitions_dsl <- denim_dsl({S -> I = d_gamma(rate = i_rate, shape = i_shape)})transitions <- list("S -> I" = d_gamma(rate = 1, shape = 5)) transitions_dsl <- denim_dsl({S -> I = d_gamma(rate = 1, shape = 5)}) # define model parameters as distributional parameters transitions_dsl <- denim_dsl({S -> I = d_gamma(rate = i_rate, shape = i_shape)})
Discrete log-normal distribution
d_lognormal(mu, sigma, dist_init = FALSE)d_lognormal(mu, sigma, dist_init = FALSE)
mu |
location parameter or the ln mean |
sigma |
scale parameter or ln standard deviation |
dist_init |
whether to distribute initial value across subcompartments following this distribution. (default to FALSE, meaning init value is always in the first compartment) |
a Transition object for simulator
transitions <- list("I -> D" = d_lognormal(3, 0.6)) transitions <- denim_dsl({I -> D = d_lognormal(3, 0.6)})transitions <- list("I -> D" = d_lognormal(3, 0.6)) transitions <- denim_dsl({I -> D = d_lognormal(3, 0.6)})
Discrete Weibull distribution
d_weibull(scale, shape, dist_init = FALSE)d_weibull(scale, shape, dist_init = FALSE)
scale |
scale parameter of a Weibull distribution |
shape |
shape parameter of a Weibull distribution |
dist_init |
whether to distribute initial value across subcompartments following this distribution. (default to FALSE, meaning init value is always in the first compartment) |
a Transition object for simulator
transitions <- list("I -> D" = d_weibull(0.6, 2)) transitions <- denim_dsl({ I -> D = d_weibull(0.6, 2) })transitions <- list("I -> D" = d_weibull(0.6, 2)) transitions <- denim_dsl({ I -> D = d_weibull(0.6, 2) })
This function parses model transitions defined in denim's DSL syntax
denim_dsl(x)denim_dsl(x)
x |
|
denim_transition object
transitions <- denim_dsl({ S -> I = beta * (I/N) * S I -> R = d_gamma(rate = 1/4, shape = 3) })transitions <- denim_dsl({ S -> I = beta * (I/N) * S I -> R = d_gamma(rate = 1/4, shape = 3) })
Convert a vector of frequencies, percentages... into a distribution
nonparametric(x, dist_init = FALSE)nonparametric(x, dist_init = FALSE)
x |
a vector of values |
dist_init |
whether to distribute initial value across subcompartments following this distribution. (default to FALSE, meaning init value is always in the first compartment)) |
a Transition object for simulator
transitions <- list("S->I"=nonparametric( c(0.1, 0.2, 0.5, 0.2) )) transitions <- denim_dsl({S->I=nonparametric( c(0.1, 0.2, 0.5, 0.2) )}) # you can also define a model parameter for the distribution transitions <- denim_dsl({S->I=nonparametric( dwelltime_dist )})transitions <- list("S->I"=nonparametric( c(0.1, 0.2, 0.5, 0.2) )) transitions <- denim_dsl({S->I=nonparametric( c(0.1, 0.2, 0.5, 0.2) )}) # you can also define a model parameter for the distribution transitions <- denim_dsl({S->I=nonparametric( dwelltime_dist )})
Overloaded plot function for denim object
## S3 method for class 'denim' plot(x, ..., color_palette = NULL)## S3 method for class 'denim' plot(x, ..., color_palette = NULL)
x |
|
... |
|
color_palette |
|
Simulation function that call the C++ simulator
sim( transitions, initialValues, parameters = NULL, simulationDuration, timeStep = 1, errorTolerance = 0.001 )sim( transitions, initialValues, parameters = NULL, simulationDuration, timeStep = 1, errorTolerance = 0.001 )
transitions |
output of function |
initialValues |
a vector contains the initial values of all compartments defined
in the transitions, follows this format |
parameters |
a vector contains values of any parameters that are not compartments,
usually parameters used in |
simulationDuration |
duration of time to be simulate |
timeStep |
set the output time interval. For example, if |
errorTolerance |
set the threshold so that a cumulative distribution function
can be rounded to 1. For example, if we want a cumulative probability of 0.999 to
be rounded as 1, we set |
a data.frame with class denim that can be plotted with a plot() method
# model can be defined using denim DSL transitions <- denim_dsl({ S -> I = beta * S * I / N I -> R = d_gamma(1/3, 2) }) # or as a list transitions <- list( "S -> I" = "beta * S * I / N", "I -> R" = d_gamma(1/3, 2) ) initialValues <- c( S = 999, I = 1, R = 0 ) parameters <- c( beta = 0.012, N = 1000 ) simulationDuration <- 30 timeStep <- 0.01 mod <- sim(transitions = transitions, initialValues = initialValues, parameters = parameters, simulationDuration = simulationDuration, timeStep = timeStep)# model can be defined using denim DSL transitions <- denim_dsl({ S -> I = beta * S * I / N I -> R = d_gamma(1/3, 2) }) # or as a list transitions <- list( "S -> I" = "beta * S * I / N", "I -> R" = d_gamma(1/3, 2) ) initialValues <- c( S = 999, I = 1, R = 0 ) parameters <- c( beta = 0.012, N = 1000 ) simulationDuration <- 30 timeStep <- 0.01 mod <- sim(transitions = transitions, initialValues = initialValues, parameters = parameters, simulationDuration = simulationDuration, timeStep = timeStep)