| Title: | Sensitivity Analysis with 'mrgsolve' |
|---|---|
| Description: | Perform sensitivity analysis on ordinary differential equation based models, including ad-hoc graphical analyses based on structured sequences of parameters as well as local sensitivity analysis. Functions are provided for creating inputs, simulating scenarios and plotting outputs. |
| Authors: | Kyle Baron [aut, cre] |
| Maintainer: | Kyle Baron <[email protected]> |
| License: | GPL (>= 2) |
| Version: | 0.3.0 |
| Built: | 2026-06-03 11:21:17 UTC |
| Source: | https://github.com/kylebaron/mrgsim.sa |
Perform local sensitivity analysis
lsa(mod, par, var, fun = .lsa_fun, eps = 1e-07, ...) lsa_plot(x, ...)lsa(mod, par, var, fun = .lsa_fun, eps = 1e-07, ...) lsa_plot(x, ...)
mod |
a mrgsolve model object. |
par |
parameter names as character vector or comma-separated string. |
var |
output names (compartment or capture) as character vector or comma-separated string. |
fun |
a function for generating simulated output for sensitivity analysis (see details). |
eps |
parameter change value for sensitivity analysis. |
... |
passed to |
x |
output from |
A tibble with class lsa.
mod <- mrgsolve::house(delta = 0.1, end = 48) par <- "CL,VC,KA" var <- "CP" dose <- ev(amt = 100) fun <- function(mod, ...) mrgsolve::mrgsim_e(mod, dose, output = "df") out <- lsa(mod, par, var, events = dose) head(out) lsa_plot(out)mod <- mrgsolve::house(delta = 0.1, end = 48) par <- "CL,VC,KA" var <- "CP" dose <- ev(amt = 100) fun <- function(mod, ...) mrgsolve::mrgsim_e(mod, dose, output = "df") out <- lsa(mod, par, var, events = dose) head(out) lsa_plot(out)
Perform local sensitivity analysis on ordinary differential equation based models, including ad-hoc graphical analyses based on sequences of parameters as well as local sensitivity analysis. Functions are provided for creating inputs, simulating scenarios and plotting outputs.
Local sensitivity analysis: lsa(), lsa_plot()
Run ad-hoc sensitivity analyses: sens_each(), sens_grid(), sens_run()
Use sens_each_data() and sens_grid_data() to pass in data sets
Parameter sequence generation:
In a pipeline: parseq_cv(), parseq_fct(), parseq_range(), parseq_manual()
Stand alone: seq_cv(), seq_fct(), seq_geo(), seq_even()
Plot ad-hoc sensitivity analysis results
Use sens_plot()
Select parameters or results
Use select_par() to choose which parameters to vary
Use select_sens() to subset sensitivity analysis results
vignette("mrgsim.sa", package = "mrgsim.sa") for a complete tutorial.
Generate a sequence of parameters based on CV
parseq_cv(mod, ..., .cv = 30, .n = 5, .nsd = 2, .digits = NULL)parseq_cv(mod, ..., .cv = 30, .n = 5, .nsd = 2, .digits = NULL)
mod |
mrgsolve model object. |
... |
unquoted parameter names. |
.cv |
a coefficient of variation used to determine range of test parameters. |
.n |
number of parameters to simulate in the sequence. |
.nsd |
number of standard deviations used to determine the range. |
.digits |
if |
parseq_fct(), parseq_range(), parseq_manual()
mod <- mrgsolve::house() mod %>% parseq_cv(CL,VC) %>% sens_each()mod <- mrgsolve::house() mod %>% parseq_cv(CL,VC) %>% sens_each()
Generate a sequence of parameters
parseq_fct(mod, ..., .n = 5, .factor = 2, .geo = TRUE, .digits = NULL) parseq_factor(mod, ..., .n = 5, .factor = 2, .geo = TRUE, .digits = NULL)parseq_fct(mod, ..., .n = 5, .factor = 2, .geo = TRUE, .digits = NULL) parseq_factor(mod, ..., .n = 5, .factor = 2, .geo = TRUE, .digits = NULL)
mod |
mrgsolve model object. |
... |
unquoted parameter names. |
.n |
number of parameters to simulate between the minimum and maximum parameter values. |
.factor |
a numeric vector used to divide and multiply the
parameter value thus generating the minimum and maximum parameter values,
respectively, for the sequence; if |
.geo |
if |
.digits |
if |
parseq_cv(), parseq_range(), parseq_manual()
mod <- mrgsolve::house() mod %>% parseq_fct(CL,VC) %>% sens_each()mod <- mrgsolve::house() mod %>% parseq_fct(CL,VC) %>% sens_each()
Simulation helper to manually specify parameter sequences
parseq_manual(mod, ...)parseq_manual(mod, ...)
mod |
mrgsolve model object. |
... |
named numeric vectors of parameter values to simulate; names must correspond to parameters in the model object. |
Parameter value vectors passed via ... will be sorted prior to simulation.
parseq_cv(), parseq_range(), parseq_fct()
mod <- mrgsolve::house() mod %>% parseq_manual(CL = c(0.5, 1, 1.5)) %>% sens_each()mod <- mrgsolve::house() mod %>% parseq_manual(CL = c(0.5, 1, 1.5)) %>% sens_each()
Simulation helper to generate a sequence of parameters from a range
parseq_range(mod, ..., .n = 5, .geo = TRUE, .digits = NULL)parseq_range(mod, ..., .n = 5, .geo = TRUE, .digits = NULL)
mod |
mrgsolve model object. |
... |
named parameter range vectors (minimum and maximum) for model parameters; each vector must have length 2 and names must correspond to model parameters. |
.n |
number of values to simulate for each parameter sequence; passed
to |
.geo |
if |
.digits |
if |
Parameter range vectors passed via ... will be sorted prior to simulation.
parseq_cv(), parseq_fct(), parseq_manual()
mod <- mrgsolve::house() mod %>% parseq_range(CL = c(0.5,1),VC = c(10,40)) %>% sens_each()mod <- mrgsolve::house() mod %>% parseq_range(CL = c(0.5,1),VC = c(10,40)) %>% sens_each()
Set reference values for each parameter
parseq_reference(mod, auto = TRUE)parseq_reference(mod, auto = TRUE)
mod |
a model object. |
auto |
if |
Identify parameters in a model for sensitivity analysis
select_par(mod, ...)select_par(mod, ...)
mod |
an mrgsolve model object. |
... |
unquoted parameter names. |
The model object with the selected parameters stored for use by
parseq_fct(), parseq_cv(), parseq_range(), or parseq_manual().
mod <- mrgsolve::house() select_par(mod, CL, VC)mod <- mrgsolve::house() select_par(mod, CL, VC)
Select sensitivity runs from a sens_each object
select_sens(x, dv_name = NULL, p_name = NULL)select_sens(x, dv_name = NULL, p_name = NULL)
x |
a |
dv_name |
character names of dependent variables to select; can be a comma-separated string. |
p_name |
character names of parameters to select; can be a comma-separated string. |
The updated sens_each object is returned.
library(dplyr) mod <- mrgsolve::house() out1 <- mod %>% parseq_factor(CL,VC) %>% sens_each() out2 <- select_sens(out1, dv_name = "CP,RESP", p_name = "CL")library(dplyr) mod <- mrgsolve::house() out1 <- mod %>% parseq_factor(CL,VC) %>% sens_each() out2 <- select_sens(out1, dv_name = "CP,RESP", p_name = "CL")
Use sens_each() to examine sequences of parameters, one
at a time. Use sens_grid() to examine all combinations of
sequences of parameters. The sens_each_data() and sens_grid_data()
variants allow you to pass in a data set to simulate from.
sens_each(mod, idata = NULL, ...) sens_each_data(mod, data, idata = NULL, ...) sens_grid(mod, idata = NULL, ...) sens_grid_data(mod, data, idata = NULL, ...)sens_each(mod, idata = NULL, ...) sens_each_data(mod, data, idata = NULL, ...) sens_grid(mod, idata = NULL, ...) sens_grid_data(mod, data, idata = NULL, ...)
mod |
an mrgsolve model object (usually read in with
|
idata |
included only to prevent users from passing through; the
function will create an |
... |
passed to |
data |
a simulation input data set (see |
A tibble-like object with class sens_each or sens_grid, depending on the
vary method that was used. These objects will look just like a tibble, but
they can be plotted with sens_plot().
mod <- mrgsolve::house() mod <- mrgsolve::ev(mod, amt = 100) out_each <- parseq_cv(mod, CL, VC, .n = 3) %>% sens_each() sens_plot(out_each, dv_name = "CP,RESP", layout = "facet_grid") out_grid <- parseq_cv(mod, CL, VC) %>% sens_grid() sens_plot(out_grid, dv_name = "CP")mod <- mrgsolve::house() mod <- mrgsolve::ev(mod, amt = 100) out_each <- parseq_cv(mod, CL, VC, .n = 3) %>% sens_each() sens_plot(out_each, dv_name = "CP,RESP", layout = "facet_grid") out_grid <- parseq_cv(mod, CL, VC) %>% sens_grid() sens_plot(out_grid, dv_name = "CP")
Plot sensitivity analysis results
sens_plot(data, ...) ## S3 method for class 'sens_each' sens_plot( data, dv_name = NULL, p_name = NULL, logy = FALSE, ncol = NULL, lwd = 0.8, digits = 3, plot_ref = TRUE, xlab = "time", ylab = NULL, layout = c("default", "facet_grid", "facet_wrap", "list"), grid = FALSE, palette = NULL, ... ) ## S3 method for class 'sens_grid' sens_plot( data, dv_name = NULL, logy = FALSE, ncol = NULL, lwd = 0.8, digits = 2, plot_ref = TRUE, xlab = "time", ylab = dv_name, group = NULL, facet = NULL, palette = NULL, ... )sens_plot(data, ...) ## S3 method for class 'sens_each' sens_plot( data, dv_name = NULL, p_name = NULL, logy = FALSE, ncol = NULL, lwd = 0.8, digits = 3, plot_ref = TRUE, xlab = "time", ylab = NULL, layout = c("default", "facet_grid", "facet_wrap", "list"), grid = FALSE, palette = NULL, ... ) ## S3 method for class 'sens_grid' sens_plot( data, dv_name = NULL, logy = FALSE, ncol = NULL, lwd = 0.8, digits = 2, plot_ref = TRUE, xlab = "time", ylab = dv_name, group = NULL, facet = NULL, palette = NULL, ... )
data |
output from |
... |
arguments passed on to methods. |
dv_name |
dependent variable names to plot; can be a comma-separated
string; if |
p_name |
parameter names to plot; can be a comma-separates string. |
logy |
if |
ncol |
passed to |
lwd |
passed to |
digits |
used to format numbers on the strips. |
plot_ref |
if |
xlab |
x-axis title. |
ylab |
y-axis title; not used for |
layout |
specifies how plots should be returned when |
grid |
if |
palette |
a discrete color scale; like what you get from calling
|
group |
sensitivity variable for within-panel grouping; defaults to the first sensitivity variable. |
facet |
sensitivity variable for faceting when 3 sensitivity variables
are being plotted; the |
The layout argument is only used for the sens_each method. It lets
you get the plots back in different formats when multiple dependent
variables are requested via dv_name.
Use default to get the plots back in a list if multiple dependent
variables are requested otherwise a single plot is returned.
Use facet_grid to get a single plot, with parameters in columns and
dependent variables in rows.
Use facet_wrap to get a plot with faceted using ggplot2::facet_wrap(),
with both the parameter name and the dependent variable name in the strip.
Use list to force output to be a list of plots; this output can be
further arranged using patchwork::wrap_plots() if desired.
When grid is TRUE, a list of plots will be returned when multiple
dependent variables are requested.
A ggplot object when one dv_name is specified or a list of ggplot
objects when multiple dv_names are specified.
mod <- mrgsolve::house() dose <- mrgsolve::ev(amt = 100) out <- sens_run( mod, sargs = list(events = dose), par = "CL,VC" ) sens_plot(out, "CP") out <- sens_run( mod, sargs = list(events = dose), par = "CL,VC", vary = "grid", .n = 3 ) sens_plot(out, "CP") sens_plot(out, "CP", group = "VC") if(requireNamespace("ggsci")) { color <- ggsci::scale_color_atlassian() sens_plot(out, "CP", palette = color) }mod <- mrgsolve::house() dose <- mrgsolve::ev(amt = 100) out <- sens_run( mod, sargs = list(events = dose), par = "CL,VC" ) sens_plot(out, "CP") out <- sens_run( mod, sargs = list(events = dose), par = "CL,VC", vary = "grid", .n = 3 ) sens_plot(out, "CP") sens_plot(out, "CP", group = "VC") if(requireNamespace("ggsci")) { color <- ggsci::scale_color_atlassian() sens_plot(out, "CP", palette = color) }
Run ad-hoc parameter sensitivity analyses with mrgsolve
sens_run( mod, par = NULL, var = NULL, method = c("factor", "cv", "range", "manual"), vary = c("each", "grid"), ..., sargs = list() )sens_run( mod, par = NULL, var = NULL, method = c("factor", "cv", "range", "manual"), vary = c("each", "grid"), ..., sargs = list() )
mod |
a mrgsolve model object. |
par |
parameter names for sensitivity analysis; this can be a character vector or a comma-separated string (see examples). |
var |
names of model output variables to include in simulated output;
this could be the name of a compartment or another output derived inside
of the model (e.g. |
method |
parameter sequence generation method. |
vary |
use |
... |
passed to |
sargs |
a named list of arguments passed to |
A tibble-like object with class sens_each or sens_grid, depending on
the value of vary.
mod <- mrgsolve::house() dose <- mrgsolve::ev(amt = 100) sens_run( mod, par = "CL,VC", method = "cv", vary = "each", sargs = list(events = dose) )mod <- mrgsolve::house() dose <- mrgsolve::ev(amt = 100) sens_run( mod, par = "CL,VC", method = "cv", vary = "each", sargs = list(events = dose) )
Generate a sequence based on coefficient of variation
seq_cv(point, cv = 30, n = 5, nsd = 2, digits = NULL)seq_cv(point, cv = 30, n = 5, nsd = 2, digits = NULL)
point |
reference parameter value. |
cv |
coefficient of variation. |
n |
number of values to simulate in the sequence. |
nsd |
number of standard deviations defining the range of simulated parameter values. |
digits |
number of significant digits in the answer; if |
seq_cv(10) seq_cv(5, n = 10)seq_cv(10) seq_cv(5, n = 10)
Generate evenly spaced sequence
seq_even(from, to, n = 5, digits = NULL)seq_even(from, to, n = 5, digits = NULL)
from |
passed to |
to |
passed to |
n |
passed to |
digits |
number of significant digits in the answer; if |
seq_even(1, 10, 4)seq_even(1, 10, 4)
Generate a sequence by fold increase and decrease from a point
seq_fct(point, n = 5, factor = c(3, 3), geo = TRUE, digits = NULL)seq_fct(point, n = 5, factor = c(3, 3), geo = TRUE, digits = NULL)
point |
a numeric vector of length 1. |
n |
number of elements in the sequence. |
factor |
a numeric vector of length 1 or 2; if length 1,
values will be recycled to length 2; the first number used to divide
|
geo |
if |
digits |
number of significant digits in the answer; if |
seq_fct(10) seq_fct(10, n = 4, factor = 2) seq_fct(10, n = 4, factor = 2, geo = TRUE)seq_fct(10) seq_fct(10, n = 4, factor = 2) seq_fct(10, n = 4, factor = 2, geo = TRUE)
Generate a geometric sequence of parameter values
seq_geo(from, to, n = 5, digits = NULL)seq_geo(from, to, n = 5, digits = NULL)
from |
passed to |
to |
passed to |
n |
passed to |
digits |
number of significant digits in the answer; if |
seq_geo(from = 1, to = 10, n = 10)seq_geo(from = 1, to = 10, n = 10)