Model: R6 class representing a mathematical model.

ModelR Documentation

R6 class representing a mathematical model.

Description

R6 class representing a mathematical model.

R6 class representing a mathematical model.

Details

A mathematical model is described by a system of ODEs. The state variables are called compartments, and the equations give the rate of change (i.e., the time derivatives of the states). An equation is specified by an R formula, the parameters are automatically extracted. A model can then be used to construct numerical or stochastic simulations, or be typeset as latex equations.

The compartment name can coincide with a parameter name, in which case the parameter is converted into a compartment. But the name cannot conflict with another compartment or a substitution.

If the expression uses a compartment, then the parameter is considered a compartment when simulating the model, i.e., it is returned as a column in the data frame returned by the simulation.

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 new name cannot conflict with another compartment, substitution or paramter

Public fields

restricted

a boolean variable indicating whether to restrict functions allowed to be used in the ODE system. Default to FALSE

attached.functions

the list of provided R functions to be used in the model

Active bindings

compartments

A read-only field that returns a character vector of compartment names

equations

A read-only field that returns a named list model equations

parameters

A read-only field that returns a character vector of parameter names

substitutions

A read-only field that returns a named list of expressions

representation

a read-only active field that returns the representation of the model it returns a list that contains the equations substitutions. The compartmental model can then be reconstructed from the representation.

order

a read-only active field that returns the order of the equations and aliases that must appear to satisfy dependencies in calculation

missing

a read-only field that returns the names of functions defined neither in the global environment, nor in the attached.functions list.

Methods

Public methods


Method new()

constructor

It constructs a Model object with compartments and substitutions.

Usage
Model$new(..., file = NULL, .restricted = FALSE)
Arguments
...

Each extra parameter is either passed to the 'compartment' method if it is a formula with the form 'name ~ value', or passed to the 'where' methods to define a substitution if it is a named expression.

file

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

.restricted

a boolean variable indicating whether the ODE system only has access to a limited set of functions. Default to TRUE

Examples
# An SIR model 
SIR = Model$new(
  S ~ -beta*S*I/N, # the dS/dt equation
  I ~ beta*S*I/N - gamma*I, # the dI/dt equation
  R ~ gamma*I, # the dR/dt equation
  N = S + I + R # the total population N
)
print(SIR)

Method compartment()

define a compartment using a formula

Usage
Model$compartment(eq)
Arguments
eq

a formula in the form of 'name ~ rate'. This defined a compartment with a given name and rate of change.

Returns

The invisible object 'self' for chained operations

Examples
# an SIR model
SIR = Model$new()
SIR$compartment(S ~ -beta*S*I/N)$
  compartment(I ~ beta*S*I/N - gamma*I)$
  compartment(R ~ gamma*I)$
  where(N = S + I + R)
print(SIR)

Method where()

define parameters as substitutions

Usage
Model$where(..., pairs = NULL)
Arguments
...

Each extra parameter must be named, which name is a parameter and the value is the substitution.

pairs

a named list, which is an alternative form to provide substitutions

Examples
# an SIR model
SIR = Model$new()
SIR$compartment(S ~ -beta*S*I)$
  compartment(I ~ beta*S*I - gamma*I)$
  compartment(R ~ gamma*I)$
  where(pairs=list(beta=quote(b/N)))$
  where(N = S + I + R)
print(SIR)

Method delete()

delete a compartment or a substitution

Usage
Model$delete(name)
Arguments
name

a character specifying the name of the compartment or the substitution to be deleted.

Returns

an invisible self to chain methods.

Examples
# an SIR model
SIR = Model$new()
SIR$compartment(S ~ -beta*S*I/N)$
  compartment(I ~ beta*S*I/N - gamma*I)$
  compartment(R ~ gamma*I)$
  where(N = S + I + R)
SIR$delete("N")
print(SIR)

Method rename()

rename a compartment, a substitution or a parameter

Usage
Model$rename(formula = NULL, from = NULL, to = NULL)
Arguments
formula

the @param from or @param to can be specified by a formula in the form of 'from -> to' or 'to <- from'.

from

is the name of the compartment, substitution or paramter to be changed.

to

is the new name

Returns

an invisible self to chain methods

Examples
an SIR model
SIR = Model$new()
SIR$compartment(S ~ -beta*S*I/N)$
  compartment(I ~ beta*S*I/N - gamma*I)$
  compartment(R ~ gamma*I)$
  where(N = S + I + R)
SIR$rename(S->U)$rename(beta->b)
print(SIR)

Method format()

format the class for printing

Usage
Model$format()

Method clone()

The objects of this class are cloneable with this method.

Usage
Model$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Examples


## ------------------------------------------------
## Method `Model$new`
## ------------------------------------------------

# An SIR model 
SIR = Model$new(
  S ~ -beta*S*I/N, # the dS/dt equation
  I ~ beta*S*I/N - gamma*I, # the dI/dt equation
  R ~ gamma*I, # the dR/dt equation
  N = S + I + R # the total population N
)
print(SIR)

## ------------------------------------------------
## Method `Model$compartment`
## ------------------------------------------------

# an SIR model
SIR = Model$new()
SIR$compartment(S ~ -beta*S*I/N)$
  compartment(I ~ beta*S*I/N - gamma*I)$
  compartment(R ~ gamma*I)$
  where(N = S + I + R)
print(SIR)

## ------------------------------------------------
## Method `Model$where`
## ------------------------------------------------

# an SIR model
SIR = Model$new()
SIR$compartment(S ~ -beta*S*I)$
  compartment(I ~ beta*S*I - gamma*I)$
  compartment(R ~ gamma*I)$
  where(pairs=list(beta=quote(b/N)))$
  where(N = S + I + R)
print(SIR)

## ------------------------------------------------
## Method `Model$delete`
## ------------------------------------------------

# an SIR model
SIR = Model$new()
SIR$compartment(S ~ -beta*S*I/N)$
  compartment(I ~ beta*S*I/N - gamma*I)$
  compartment(R ~ gamma*I)$
  where(N = S + I + R)
SIR$delete("N")
print(SIR)

## ------------------------------------------------
## Method `Model$rename`
## ------------------------------------------------

an SIR model
SIR = Model$new()
SIR$compartment(S ~ -beta*S*I/N)$
  compartment(I ~ beta*S*I/N - gamma*I)$
  compartment(R ~ gamma*I)$
  where(N = S + I + R)
SIR$rename(S->U)$rename(beta->b)
print(SIR)

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