Compartmental | R Documentation |
An R6 class representing a compartmental model
An R6 class representing a compartmental model
This is a subclass of Model. Unlike a Model object which gives the ODE for the rate of change of the states, a Compartmental model describes the model using compartments and transitions between compartments. Each transition may correspond to an event. The ODE system can then be derived by summing the transitions that flow from the compartment (negative flows) and those that flow to the compartment (positive flows). Because a compartmental model can describe events, they can also be used for stochastic simulations (such as the Gillespie method).
The deleted compartment or substitution is converted to a parameter. A parameter cannot be deleted. If the equation is changed so that a parameter is not used by the model any more, then the paramter is automatically removed.
The formula can be specified by a formula, which has the form from -> to ~ rate or to <- from ~ rate where the rate can be either an expression, or percapita(expression). Alternatively the from, to, or rate (and percapita) can be specified by the arguments of the same name.
A transition must be named. If the name is not provided, one is automatically generated. Any further change to the transition must be referred by the name.
The from or to can be null. If from is NULL, then the transition is an input. If to is NULL, then the transition is an output. If both the name is deleted.
If the transition with the given name exists, then the transition is changed.
REpiSim::Model
-> Compartmental
transitions
a read-only field to access the transitions.
representation
a read-only active field that returns the representation of the model it returns a list that contains self$compartments, self$transitions and self$substitutions. The compartmental model can then be reconstructed from the representation.
new()
constructor
Compartmental$new(..., file = NULL)
...
the names of the compartment, or substitutions
file
if not NULL, a path or connection to a model file to read the model from
# an SIR model SIR = Compartmental$new(S, I, R) SIR$transition(S->I ~ beta*S*I/N, N=S+I+R, name="infection") SIR$transition(I->R ~ gamma*I, name="recovery") print(SIR)
compartment()
define a compartment
Compartmental$compartment(name)
name
the name of the compartment
an invisible self for chaining methods
# an SIR model SIR = Compartmental$new() SIR$compartment("S")$compartment("I")$compartment(R) SIR$transition(S->I ~ beta*S*I/N, name="infection") SIR$transition(I->R ~ gamma*I, name="recovery") SIR$where(N=S+I+R) print(SIR)
delete()
delete a compartment, a substitution or a transition
Compartmental$delete(name)
name
the name of the compartment, substitution or transition to delete.
an invisible self for chaining methods
# an SIR model SIR = Compartmental$new(S, I, R) SIR$transition(S->I ~ beta*S*I/N, N=S+I+R, name="infection") SIR$transition(I->R ~ gamma*I, name="recovery") print(SIR)
transition()
define or change a transition
Compartmental$transition( formula = NULL, ..., from = NULL, to = NULL, rate = NULL, percapita = FALSE, name = NULL )
formula
the transition can be fullt specified by a formula. Please see the details.
...
named arguments specifying substitutions for the parameters used by this transition.
from
the name of the compartment that the transition originates
to
the name of the compartment that the transition flows into
rate
the rate of the transition, an expression. If rate is NULL, the transition is deleted.
percapita
whether the rate is per capita, i.e., the total rate would be the per capita rate multiplied by the from compartment.
name
the name of the transition, a character. If NULL, the name is automatically generated
the name of the transition
# an SIR model SIR = Compartmental$new(S, I, R) SIR$transition(S->I ~ beta*S*I/N, N=S+I+R, name="infection") SIR$transition(I->R ~ gamma*I, name="recovery") SIR$transition(S->R ~ v, percapita=TRUE, name="vaccination") # the following command changes the "vaccination" transition SIR$transition(S->NULL ~ v*S, name="vaccination") # this following command delete the "vaccination" transition, because # the rate is default to NULL. SIR$transition(name="vaccination") SIR$transition(NULL->S ~ lambda, name="births") SIR$transition(S -> NULL ~ percapita(mu), name="death.S") SIR$transition(I -> NULL ~ percapita(mu), name="death.I") SIR$transition(R -> NULL ~ percapita(mu), name="death.R") print(SIR)
format()
format the class for printing
Compartmental$format()
clone()
The objects of this class are cloneable with this method.
Compartmental$clone(deep = FALSE)
deep
Whether to make a deep clone.
## ------------------------------------------------
## Method `Compartmental$new`
## ------------------------------------------------
# an SIR model
SIR = Compartmental$new(S, I, R)
SIR$transition(S->I ~ beta*S*I/N, N=S+I+R, name="infection")
SIR$transition(I->R ~ gamma*I, name="recovery")
print(SIR)
## ------------------------------------------------
## Method `Compartmental$compartment`
## ------------------------------------------------
# an SIR model
SIR = Compartmental$new()
SIR$compartment("S")$compartment("I")$compartment(R)
SIR$transition(S->I ~ beta*S*I/N, name="infection")
SIR$transition(I->R ~ gamma*I, name="recovery")
SIR$where(N=S+I+R)
print(SIR)
## ------------------------------------------------
## Method `Compartmental$delete`
## ------------------------------------------------
# an SIR model
SIR = Compartmental$new(S, I, R)
SIR$transition(S->I ~ beta*S*I/N, N=S+I+R, name="infection")
SIR$transition(I->R ~ gamma*I, name="recovery")
print(SIR)
## ------------------------------------------------
## Method `Compartmental$transition`
## ------------------------------------------------
# an SIR model
SIR = Compartmental$new(S, I, R)
SIR$transition(S->I ~ beta*S*I/N, N=S+I+R, name="infection")
SIR$transition(I->R ~ gamma*I, name="recovery")
SIR$transition(S->R ~ v, percapita=TRUE, name="vaccination")
# the following command changes the "vaccination" transition
SIR$transition(S->NULL ~ v*S, name="vaccination")
# this following command delete the "vaccination" transition, because
# the rate is default to NULL.
SIR$transition(name="vaccination")
SIR$transition(NULL->S ~ lambda, name="births")
SIR$transition(S -> NULL ~ percapita(mu), name="death.S")
SIR$transition(I -> NULL ~ percapita(mu), name="death.I")
SIR$transition(R -> NULL ~ percapita(mu), name="death.R")
print(SIR)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.