Compartmental: An R6 class representing a compartmental model

CompartmentalR Documentation

An R6 class representing a compartmental model

Description

An R6 class representing a compartmental model

An R6 class representing a compartmental model

Details

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.

Super class

REpiSim::Model -> Compartmental

Active bindings

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.

Methods

Public methods

Inherited methods

Method new()

constructor

Usage
Compartmental$new(..., file = NULL)
Arguments
...

the names of the compartment, or substitutions

file

if not NULL, a path or connection to a model file to read the model from

Examples
# 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 compartment()

define a compartment

Usage
Compartmental$compartment(name)
Arguments
name

the name of the compartment

Returns

an invisible self for chaining methods

Examples
# 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 delete()

delete a compartment, a substitution or a transition

Usage
Compartmental$delete(name)
Arguments
name

the name of the compartment, substitution or transition to delete.

Returns

an invisible self for chaining methods

Examples
# 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 transition()

define or change a transition

Usage
Compartmental$transition(
  formula = NULL,
  ...,
  from = NULL,
  to = NULL,
  rate = NULL,
  percapita = FALSE,
  name = NULL
)
Arguments
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

Returns

the name of the transition

Examples
# 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)

Method format()

format the class for printing

Usage
Compartmental$format()

Method clone()

The objects of this class are cloneable with this method.

Usage
Compartmental$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Examples


## ------------------------------------------------
## 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)

junlingm/REpiSim documentation built on June 15, 2025, 7:36 a.m.