Model | R Documentation |
R6 class representing a mathematical model.
R6 class representing a mathematical model.
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
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
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.
new()
constructor
It constructs a Model object with compartments and substitutions.
Model$new(..., file = NULL, .restricted = FALSE)
...
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
# 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)
compartment()
define a compartment using a formula
Model$compartment(eq)
eq
a formula in the form of 'name ~ rate'. This defined a compartment with a given name and rate of change.
The invisible object 'self' for chained operations
# 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)
where()
define parameters as substitutions
Model$where(..., pairs = NULL)
...
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
# 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)
delete()
delete a compartment or a substitution
Model$delete(name)
name
a character specifying the name of the compartment or the substitution to be deleted.
an invisible self to chain methods.
# 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)
rename()
rename a compartment, a substitution or a parameter
Model$rename(formula = NULL, from = NULL, to = NULL)
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
an invisible self to chain methods
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)
format()
format the class for printing
Model$format()
clone()
The objects of this class are cloneable with this method.
Model$clone(deep = FALSE)
deep
Whether to make a deep clone.
## ------------------------------------------------
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.