Type: | Package |
Title: | Statistical Inference for Systems of Ordinary Differential Equations using Separable Integral-Matching |
Version: | 1.2.2 |
Description: | Implements statistical inference for systems of ordinary differential equations, that uses the integral-matching criterion and takes advantage of the separability of parameters, in order to obtain initial parameter estimates for nonlinear least squares optimization. Dattner & Yaari (2018) <doi:10.48550/arXiv.1807.04202>. Dattner et al. (2017) <doi:10.1098/rsif.2016.0525>. Dattner & Klaassen (2015) <doi:10.1214/15-EJS1053>. |
Depends: | R (≥ 3.4.0) |
Imports: | deSolve, pracma, quadprog, glmnet, ncvreg, |
Suggests: | parallel, knitr, testthat (≥ 3.0.0), rmarkdown |
License: | GPL-2 | GPL-3 [expanded from: GPL (≥ 2)] |
Encoding: | UTF-8 |
LazyData: | true |
RoxygenNote: | 7.3.1 |
VignetteBuilder: | knitr |
NeedsCompilation: | no |
Packaged: | 2024-02-16 20:32:54 UTC; ry2460 |
Author: | Itai Dattner [aut], Rami Yaari [aut, cre] |
Maintainer: | Rami Yaari <ramiyaari@gmail.com> |
Config/testthat/edition: | 3 |
Repository: | CRAN |
Date/Publication: | 2024-02-16 20:50:02 UTC |
Calculates confidence intervals for the model parameters
Description
Calculates confidence intervals for the model parameters, based on the given likelihood profiles
Usage
## S3 method for class 'profile.simode'
confint(object, parm = NULL, level = 0.95, ...)
Arguments
object |
A fitted model object: |
parm |
A specification of which parameters are to be given confidence intervals (named vector). If missing, all parameters are considered. |
level |
The confidence level required. |
... |
Additional argument(s) for methods. |
Value
The confidence intervals.
Plot confidence intervals for the model parameters
Description
Plot confidence intervals for the model parameters of a simode
object,
calculated based on likelihood profiles
Usage
## S3 method for class 'confint.simode'
plot(
x,
which = NULL,
pars_true = NULL,
legend = F,
cols = list(fit = "blue", true = "black"),
...
)
Arguments
x |
|
which |
Which parameters to plot the confidence intervals for. If empty, the plot will include all of the parameters in x. |
pars_true |
The true parameter values (if are known). |
legend |
Whether or not to add a figure legend. |
cols |
List of colors for each element of the plot. |
... |
Additional argument(s) for methods. |
Plot the fit/estimates of a list.simode
object
Description
Plot the fit or parameter estimates obtained from a call to simode
with obs_sets>1
.
Usage
## S3 method for class 'list.simode'
plot(
x,
type = c("fit", "est"),
show = c("nls", "im", "both"),
which = NULL,
pars_true = NULL,
time = NULL,
plot_mean_sd = F,
plot_im_smooth = F,
legend = F,
mfrow = par("mfrow"),
cols = list(nls_fit = "blue", im_fit = "green", true = "black", obs = "red", im_smooth
= "magenta"),
...
)
Arguments
x |
|
type |
Type of plot - 'fit' to plot the fitted equations and 'est' to plot the parameter estimates. |
show |
Whether to plot the fit/estimates obtained using nonlinear least squares ('nls'), integral-matching ('im') or both ('both'). |
which |
Which variables to plot in case |
pars_true |
The true parameter values (if are known). Should be named using the parameter names. If given, the true values for the variables/parameters will be added to the plot. |
time |
The time points to use for the fitted curves
(relevant only for |
plot_mean_sd |
Plot the mean and standard deviation for the fit/estimates
of the |
plot_im_smooth |
Whether or not to plot the smoothed curves created and
used by the integral-matching procedure (relevant only for |
legend |
Whether or not to add a figure legend. |
mfrow |
A vector of the form c(nr,nc) setting the layout of
subplots in one plot (see also |
cols |
List of colors for each element of the plot. |
... |
Additional argument(s) for methods. |
Plot the likelihood profiles for the model parameters
Description
Plot the likelihood profiles for the model parameters
Usage
## S3 method for class 'profile.simode'
plot(
x,
which = NULL,
mfrow = par("mfrow"),
cols = list(fit = "blue", threshold = "red"),
...
)
Arguments
x |
|
which |
Which parameters to plot the likelihood profiles for. If empty, the plot will include all of the parameters in x. |
mfrow |
A vector of the form c(nr,nc) setting the layout of
subplots in one plot (see also |
cols |
List of colors for each element of the plot. |
... |
Additional argument(s) for methods. |
Plot the fit/estimates of a simode
object
Description
Plot the fit or parameter estimates obtained from a call to simode
.
Usage
## S3 method for class 'simode'
plot(
x,
type = c("fit", "est"),
show = c("nls", "im", "both"),
which = NULL,
pars_true = NULL,
time = NULL,
plot_im_smooth = F,
legend = F,
mfrow = par("mfrow"),
cols = list(nls_fit = "blue", im_fit = "green", true = "black", obs = "red", im_smooth
= "magenta"),
...
)
Arguments
x |
|
type |
Type of plot - 'fit' to plot the fitted variables and 'est' to plot the parameter estimates. |
show |
Whether to plot the fit/estimates obtained using nonlinear least squares ('nls'), integral-matching ('im') or both ('both'). |
which |
Which variables to plot in case |
pars_true |
The true parameter values (if are known). Should be named using the parameter names. If given, the true values for the variables/parameters will be added to the plot. |
time |
The time points to use for the fitted curves
(relevant only for |
plot_im_smooth |
Whether or not to plot the smoothed curves created and
used by the integral-matching procedure (relevant only for |
legend |
Whether or not to add a figure legend. |
mfrow |
A vector of the form c(nr,nc) setting the layout of
subplots in one plot (see also |
cols |
List of colors for each element of the plot. |
... |
Additional argument(s) for methods. |
Plot optimization trace of a call to simode
Description
Plot a trace of the loss values and parameter estimates during
the integral-matching/nonlinear least squares optimization within a call to simode
.
For the traces to exist, the arguments save_im_trace
and/or
save_nls_trace
in simode.control
should be set to true,
when calling simode
.
Usage
plot_trace(
x,
show = c("nls", "im", "both"),
which = NULL,
mfrow = par("mfrow"),
cols = list(nls_fit = "blue", im_fit = "green"),
...
)
Arguments
x |
|
show |
Whether to plot the estimates obtained using nonlinear least squares ('nls'), integral-matching ('im') or both ('both'). |
which |
Which parameters' traces to plot. If NULL, the
trace for all the parameters in |
mfrow |
A vector of the form c(nr,nc) setting the layout of
subplots in one plot (see also |
cols |
List of colors for each element of the plot. |
... |
Additional argument(s) for methods. |
Print method for confint.simode
objects
Description
Print method for confint.simode
objects
Usage
## S3 method for class 'confint.simode'
print(x, ...)
Arguments
x |
The |
... |
Additional argument(s) for methods. |
Print method for profile.simode
objects
Description
Print method for profile.simode
objects
Usage
## S3 method for class 'profile.simode'
print(x, ...)
Arguments
x |
The |
... |
Additional argument(s)for methods. |
Print method for simode
objects
Description
Print method for simode
objects
Usage
## S3 method for class 'simode'
print(x, ...)
Arguments
x |
The |
... |
Additional argument(s) for methods. |
Print method for summary.simode
objects
Description
Print method for summary.simode
objects
Usage
## S3 method for class 'summary.simode'
print(x, ...)
Arguments
x |
The |
... |
Additional argument(s) for methods. |
Calculate likelihood profiles for the model parameters
Description
Calculate likelihood profiles for the model parameters
Usage
## S3 method for class 'simode'
profile(
fitted,
which = NULL,
optim_type = c("nls", "both"),
step_size,
max_steps = 100,
alpha = 0.05,
skip_err = T,
trace = 0,
save_to_log = F,
...
)
Arguments
fitted |
|
which |
Which parameters to estimate the profile for. |
optim_type |
Whether to calculate the profiles based on maximum-likelihood optimization only ('nls') or based on integral-matching followed by maximum-likelihood optimization ('both'). |
step_size |
Step size for profiling (one value for all parameters or
a value for each parameter in |
max_steps |
Maximum number of steps to take in each direction. |
alpha |
Maximum (two-sided) likelihood ratio test confidence level to find. |
skip_err |
Whether on not to stop the calculation if encountering a problem with one point in the profile. |
trace |
Report level (0-4), with higher values producing more tracing information. |
save_to_log |
Whether to redirect output to log file. The log file will be saved to tempdir(). |
... |
Additional argument(s) for methods. |
Details
If the call to simode
, which returned the fitted object given to
this method, included a user-defined likelihood function (with the calc_nll
argument),
then the likelihood profiles will be calculated using this function. Otherwise,
the profiles will be calculated using a likelihood based on a Gaussian distribuion
with fixed sigma, where sigma will be estimated in the background together with
the rest of the model parameters.
Value
The likelihood profiles.
Statistical inference of ordinary differential equations using separable integral-matching
Description
Estimating the parameters of an ODE system in two stages: 1) Estimate the parameters using separable integral-matching, 2) Estimate the parameters using nonlinear least squares starting from the values obtained in stage 1.
Usage
simode(
equations,
pars,
time,
obs,
obs_sets = 1,
nlin_pars = NULL,
likelihood_pars = NULL,
fixed = NULL,
start = NULL,
lower = NULL,
upper = NULL,
im_method = c("separable", "non-separable"),
decouple_equations = F,
gen_obs = NULL,
calc_nll = NULL,
simode_ctrl = simode.control(),
...
)
Arguments
equations |
Named vector. The equations describing the ODE system.
Each element of the vector should contain a character representation
of the right-hand side of an equation, and should be named according to
the left-hand side of the equation (i.e., the variable name).
An equation can contain parameters appearing in |
pars |
The names of the parameters and initial conditions
to be estimated. An initial condition name for a certain variable
is the name given to the relevant equation in |
time |
Time points of the observations. Either a vector,
if the same time points were used for observing all variables,
or a list of vectors the length of |
obs |
Named list. The observations. When |
obs_sets |
Number of observations sets. When |
nlin_pars |
Names of parameters or initial conditions
that will be estimated in stage 1 using nonlinear least squares optimization.
The parameter names in |
likelihood_pars |
Names of likelihood parameters not appearing in the ODE system,
which are needed for the the user-defined function |
fixed |
Named vector. Fixed values for one or more of the ODE system parameters or initial conditions. Parameters in this list will not be estimated. |
start |
Named vector. Starting values for optimization of parameters/initial conditions.
Must contain starting values for all the parameters in |
lower |
Named vector. Lower bounds for any parameter/initial condition. |
upper |
Named vector. Upper bounds for any parameter/initial condition. |
im_method |
The method to use for integral-matching. Default "separable" means that linear parameters are estimated directly while "non-separable" means that linear parameters are estimated using nonlinear least squares optimization. If none of the parameters are linear then the default can be used. |
decouple_equations |
Whether to fit each equation separately in the integral-matching stage. |
gen_obs |
A user-defined function for completing missing observations (see Details). |
calc_nll |
A user-defined function for calculating negative log-likelihood for the model (see Details). |
simode_ctrl |
Various control parameters. See |
... |
Additional arguments passed to |
Details
gen_obs
can be used in cases of a partially observed system,
for which observations of the missing variables can be generated given values for the
system parameters. The function will be called during the optimization using integral-matching.
It must be defined as
gen_obs <- function(equations, pars, x0, time, obs, ...)
, where:
-
equations
the ODE equations -
pars
the parameter values -
x0
the initial conditions -
time
the timing of the observations (vector or list) -
obs
the observations -
...
additional parameters passed from the call tosimode
The function should return a list with two items:
-
time
the vector or list of time points of the observations -
obs
the list of observations with the newly generated observations
calc_nll
allows the user to pass his own likelihood function to be used in the
optimization in the second stage (if not defined, the default nonlinear least squares
optimization will be used). The likelihood function will also be used in a following
call to profile
, for the calculation of likelihood profiles.
It must be defined as
calc_nll <- function(pars, time, obs, model_out, ...)
, where:
-
pars
the parameter values -
time
the timing of the observations (vector or list) -
obs
the observations -
model_out
the model output returned from a call tosolve_ode
. Iftime
is a list with possibly different times for each variable thenmodel_out
will contain a union of all these times. -
...
additional parameters passed from the call tosimode
The function should return the negative log-likelihood.
Value
If obs_sets=1
, the function returns a simode
object containing the
parameter estimates after integral-matching (stage 1) and after
nonlinear least squares optimization (stage 2). If obs_sets>1
and
obs_sets_fit!="together"
in simode_ctrl
, the function
returns a list.simode
object which is a list of simode
objects
the length of obs_sets
.
References
Dattner & Klaassen (2015). Optimal Rate of Direct Estimators in Systems of Ordinary Differential Equations Linear in Functions of the Parameters, Electronic Journal of Statistics, Vol. 9, No. 2, 1939-1973.
Dattner, Miller, Petrenko, Kadouriz, Jurkevitch & Huppert (2017). Modelling and Parameter Inference of Predator-prey Dynamics in Heterogeneous Environments Using The Direct Integral Approach, Journal of The Royal Society Interface 14.126: 20160525.
Examples
## =================================================
## Predator-Prey Lotka-Volterra model
## =================================================
## generate model equations and parameters (X=Prey,Y=Predator)
pars <- c('alpha','beta','gamma','delta')
vars <- c('X','Y')
eq_X <- 'alpha*X-beta*X*Y'
eq_Y <- 'delta*X*Y-gamma*Y'
equations <- c(eq_X,eq_Y)
names(equations) <- vars
x0 <- c(0.9,0.9)
names(x0) <- vars
theta <- c(2/3,4/3,1,1)
names(theta) <- pars
## generate observations
n <- 50
time <- seq(0,25,length.out=n)
model_out <- solve_ode(equations,theta,x0,time)
x_det <- model_out[,vars]
set.seed(1000)
sigma <- 0.05
obs <- list()
for(i in 1:length(vars)) {
obs[[i]] <- pmax(0, rnorm(n,x_det[,i],sigma))
}
names(obs) <- vars
## estimate model parameters with known initial conditions
simode_fit1 <- simode(equations=equations, pars=pars, fixed=x0, time=time, obs=obs)
plot(simode_fit1, type='fit', time=seq(0,25,length.out=100), pars_true=theta, mfrow=c(2,1))
plot(simode_fit1, type='est', pars_true=theta)
## estimate model parameters and initial conditions
simode_fit2 <- simode(equations=equations, pars=c(pars,vars), time=time, obs=obs)
plot(simode_fit2, type='fit', time=seq(0,25,length.out=100), pars_true=c(theta,x0), mfrow=c(2,1))
plot(simode_fit2, type='est', pars_true=c(theta,x0))
profiles_fit2 <- profile(simode_fit2,step_size=0.01,max_steps=50)
plot(profiles_fit2,mfrow=c(2,3))
ci_fit2 <- confint(profiles_fit2)
ci_fit2
plot(ci_fit2,pars_true=c(theta,x0),legend=T)
Class containing control parameters for a call to simode
Description
Class containing control parameters for a call to simode
Usage
simode.control(
optim_type = c("both", "im", "nls"),
im_optim_method = c("BFGS", "Nelder-Mead", "CG", "L-BFGS-B", "SANN", "Brent"),
nls_optim_method = c("BFGS", "Nelder-Mead", "CG", "L-BFGS-B", "SANN", "Brent"),
im_optim_control = list(),
nls_optim_control = list(),
ode_control = list(method = "lsoda"),
im_smoothing = c("splines", "kernel", "none"),
im_grid_size = 0,
bw_factor = 1.5,
use_pars2vars_mapping = F,
trace = 0,
save_im_trace = F,
save_nls_trace = F,
obs_sets_fit = c("separate", "separate_x0", "together"),
parallel = F,
save_to_log = F,
reg_alpha = -1,
reg_pkg = c("glmnet", "ncvreg")
)
Arguments
optim_type |
Controls what optimization will be performed: either only integral-matching ('im'), only nonlinear least squares ('nls') or both (the default, i.e., first integral-matching then nonlinear least squares starting from the integral-matching estimates). |
im_optim_method |
Method for optimization during the integral-matching stage.
Accepted values are any method supported by the |
nls_optim_method |
Method for optimization during the nonlinear least squares stage.
Accepted values are the same as in |
im_optim_control |
A list with control parameters for optimization during the
integral-matching stage. Can include anything that would appear in the |
nls_optim_control |
Control parameters for optimization during the
nonlinear least squares stage (as in |
ode_control |
A list with control parameters for the ODE solver. Can include the argument
|
im_smoothing |
Choice of type of smoothing during the integral-matching stage (see Details). |
im_grid_size |
Number of points used in integral-matching grid
(not relevant when |
bw_factor |
Controls the bandwidth when |
use_pars2vars_mapping |
Whether to use pars2vars mapping (see Details). |
trace |
Report level (0-4), with higher values producing more tracing information (see Details). |
save_im_trace |
Whether to save trace information of integral-matching optimization,
which can then be plotted using |
save_nls_trace |
Whether to save trace information of nonlinear least squares optimization,
which can then be plotted using |
obs_sets_fit |
Controls the way multiple observation sets are fitted: either "separate" (each set can be fitted with its own parameter values and initial conditions), "separate_x0" (same parameter values fitted for all sets while initial conditions may be different for each set) or "together" (fitting the mean of all observations sets). |
parallel |
Controls whether to fit sequentially or in parallel multiple observation
sets ( |
save_to_log |
Controls whether to redirect output to a log file. If true, output will be saved to the file 'simode.log' in tempdir. |
reg_alpha |
Value of tuning parameter alpha for regularization during the
integral-matching stage. Negative value means no regularization. A value between
0 and 1 controls the type of regularization (see |
reg_pkg |
What package to use for regularization (in case reg_alpha>=0). |
Details
Possible values for im_smoothing
are “splines” (the default),
in which case smoothing will be performed using smooth.spline
with generalized cross-validation, “kernel”, using own kernel smoother function,
or “none” (using the observations as is, with interpolation if necessary).
use_pars2vars_mapping
controls whether to use a mapping of which equations
are affected by each of the parameters. When set to true, previous matrices computed as part of
the integral-matching estimation are stored during the integral-matching optimization,
and are updated only for the equations that were affected by the change in the
parameter estimates from the previous iteration.
When the number of equations is large and some of the parameters affect only a few equations,
setting this option to true can significantly reduce the optimization time during
the integral-matching stage (while increasing the storage usage).
This is especially true with derivative based optimization methods (such as “BFGS” of optim)
which updates only one of the optimized parameters in each iteration.
trace
has 5 possible levels:
With trace=0, there would be no output displayed if there are no errors.
With trace=1, a message will be displayed at the beginning and end of each optimization stage.
With trace=2, non-critical errors occurring during the optimization iterations will be displayed.
With trace=3, non-critical warnings occurring during the optimization iterations will be displayed.
With trace=4, the calculated loss value for each iteration of the integral-matching and
nonlinear least squares optimizations will be displayed.
Example dataset for a multi-group SIR model
Description
A model for the spread of a seasonal influenza epidemics in two groups over five seasons.
Usage
sir_example
Format
A list containing the following variables:
- equations
The ODE equations describing the system, including ten equations for the susceptible dynamics (2 groups * 5 seasons) and ten equations for the infected dynamics.
- beta
The 2x2 transmission matrix parameters (in time unit of weeks).
- gamma
The recovery rate parameter (in time unit of weeks).
- kappa
The relative infectiousness of seasons 2-5 compared to season 1.
- S0
The initial conditions for the susceptible variables.
- I0
The initial conditions for the infected variables.
- time
Times in which the observations were made (in weeks).
- obs
A list of observations of the infected variables, generated using Gaussian measurement error with sigma=1e-3.
Ordinary differential equations solver
Description
A wrapper for the ode function that solves a system of ordinary differential equations described using symbolic equations.
Usage
solve_ode(equations, pars, x0, time, xvars = NULL, ...)
Arguments
equations |
The equations describing the ODE system. See simode. |
pars |
The parameter values. Named according to their names in |
x0 |
The initial conditions. Named accroding to the names of |
time |
The time points for which the ODE variables' values will be computed. |
xvars |
External observations of time-dependant variables refered to in |
... |
Additional argument(s) for methods. |
Value
A matrix whose first column contains the given time points and subsequent columns hold the computed ODE equations' values at these time points.
Summary method for list.simode
objects
Description
Summary method for list.simode
objects
Usage
## S3 method for class 'list.simode'
summary(
object,
sum_mean_sd = F,
pars_true = NULL,
digits = max(3, getOption("digits") - 3),
...
)
Arguments
object |
|
sum_mean_sd |
Whether to calculate mean and standard deviation
for the parameter estimates in the fits included in the given |
pars_true |
The true parameter values (relevant only for
when |
digits |
The number of significant digits to use. |
... |
Additional argument(s) for methods. |
Value
The mean and standard deviation for the
loss values and parameter estimates obtained from the integral-matching
and nonlinear least squares optimizations. If pars_true
is given, then
will also calculate bias and RMSE for the parameter estimates.
Summary method for simode
objects
Description
Summary method for simode
objects
Usage
## S3 method for class 'simode'
summary(object, digits = max(3, getOption("digits") - 3), ...)
Arguments
object |
The |
digits |
The number of significant digits to use. |
... |
Additional argument(s) for methods. |