OdeModelFit: An ODE model fit (R6 class)

OdeModelFitR Documentation

An ODE model fit (R6 class)

Description

The fields cmdstanr_time, cmdstanr_summary, and cmdstanr_draws store the output of cmdstanr_fit's methods ⁠$time()⁠, ⁠$summary()⁠, and ⁠$draws()⁠ methods, respectively, in memory in case cmdstanr_fit gets corrupted (for example if the CSV files that it reads the data from are destroyed).

Public fields

model

An object of class OdeModel.

t0

Used initial time.

t

Used time points.

solver

Used solver.

data

Given additional data.

cmdstanr_fit

A cmdstanr::CmdStanMCMC or cmdstanr::CmdStanGQ object.

cmdstanr_time

A list containing output of the ⁠$time()⁠ method of cmdstanr_fit.

cmdstanr_summary

A tibble containing output of the ⁠$summary()⁠ method of cmdstanr_fit.

cmdstanr_draws

A posterior::draws_array object containing the output of the ⁠$draws()⁠ method of cmdstanr_fit.

cmdstanr_metadata

A list containing output of the ⁠$metadata()⁠ method of cmdstanr_fit.

cmdstanr_output

A list containing output of the ⁠$output()⁠ method of cmdstanr_fit.

setup_time

Time it took to call ⁠$initialize()⁠ when the OdeModelFit object was created (in seconds).

Methods

Public methods


Method new()

Create an OdeModelFit object.

Usage
OdeModelFit$new(model, t0, t, solver, data, cmdstanr_fit)
Arguments
model

An object of class OdeModel (will be deepcopied).

t0

Used initial time.

t

Used time points.

solver

Used solver. An object of class OdeSolver.

data

Given additional data.

cmdstanr_fit

A cmdstanr::CmdStanMCMC or cmdstanr::CmdStanGQ object (will be deepcopied).


Method time()

Get time information.

Usage
OdeModelFit$time()
Returns

A list.


Method info()

Get various information.

Usage
OdeModelFit$info()
Returns

A string.


Method draws()

Get draws (parameters and generated quantities).

Usage
OdeModelFit$draws(variable = NULL, iteration = NULL)
Arguments
variable

Name of variable.

iteration

Index of iteration.

Returns

A posterior::draws_array object.


Method summary()

Get summary

Usage
OdeModelFit$summary()
Returns

A tibble.


Method niterations()

Get number of post-warmup iterations per MCMC chain.

Usage
OdeModelFit$niterations()

Method nchains()

Get number of MCMC chains.

Usage
OdeModelFit$nchains()

Method ndraws()

Get total number of post-warmup draws.

Usage
OdeModelFit$ndraws()

Method cmdstan_seed()

Get used 'CmdStan' rng seed.

Usage
OdeModelFit$cmdstan_seed()

Method cmdstan_version()

Get used 'CmdStan' version.

Usage
OdeModelFit$cmdstan_version()
Returns

A string.


Method get_t()

Get time points where the model was fitted.

Usage
OdeModelFit$get_t(include_t0 = FALSE)
Arguments
include_t0

Should the initial time point be included?

Returns

A numeric vector of length N. If include_t0 is TRUE, length will be N+1.


Method get_t0()

Get used initial time point t0.

Usage
OdeModelFit$get_t0()
Returns

A numeric value.


Method dim()

Get dimensions of a variable.

Usage
OdeModelFit$dim(variable)
Arguments
variable

Name of variable.

Returns

A numeric vector, which is the 'Stan' variable dimension, obtained as metadata$stan_variable_dims[[variable]], where metadata is the metadata of the cmdstanr::CmdStanMCMC or cmdstanr::CmdStanGQ object.


Method dim_odesol()

Extract the dimensions of the ODE solution variable.

Usage
OdeModelFit$dim_odesol()
Returns

A numeric vector of length 2, where first element is the number of time points and second element is the ODE system dimension.


Method extract_unflattened()

Extract array variable draws so that the array is unflattened.

Usage
OdeModelFit$extract_unflattened(variable)
Arguments
variable

Name of variable.

Returns

A base R array of dimension c(num_draws, ...) where num_draws is the total number of draws and ... is the 'Stan' variable dimension, obtained as self$dim(variable).


Method extract_odesol()

Extract the ODE solutions using each parameter draw, in an unflattened base R array format.

Usage
OdeModelFit$extract_odesol(include_y0 = FALSE)
Arguments
include_y0

Should the initial state be included?

Returns

A base R array of dimension c(num_draws, N, D) where num_draws is the total number of draws and N is the number of time points and D is the number of ODE system dimensions. If include_y0 is TRUE, then the N dimension grows to N+1.


Method extract_y0()

Extract the ODE initial states using each parameter draw, in a base R array format.

Usage
OdeModelFit$extract_y0()
Returns

A base R array of dimension c(num_draws, D) where num_draws is the total number of draws and D is the number of ODE system dimensions.


Method extract_odesol_quantile()

Extract quantiles of the ODE solutions in a base R array format.

Usage
OdeModelFit$extract_odesol_quantile(p, include_y0 = FALSE)
Arguments
p

Percentile. A number between 0 and 1. For example p=0.5 corresponds to median.

include_y0

Should the initial state be included?

Returns

A base R array of dimension c(N, D) where N is the number of time points and D is the number of ODE system dimensions. If include_y0 is TRUE, then the N dimension grows to N+1.


Method loglik()

Extract the log likelihood using each parameter draw.

Usage
OdeModelFit$loglik()
Returns

A posterior::draws_array.


Method extract_odesol_df()

Extract the ODE solutions using each parameter draw, in a flattened data frame format that is easy to pass as data to for example ggplot2::ggplot().

Usage
OdeModelFit$extract_odesol_df(
  draw_inds = NULL,
  include_y0 = FALSE,
  ydim_names = NULL
)
Arguments
draw_inds

If this is not NULL, returns ode solutions corresponding only to given draws.

include_y0

Should the initial state be included?

ydim_names

Names of the ODE dimensioins. If NULL, these are automatically set as "y1", "y2", etc.

Returns

A data.frame.


Method extract_odesol_df_dist()

Extract (quantiles of) the marginal distribution of ODE solutions in a data frame format that is easy to pass as data to for example ggplot2::ggplot().

Usage
OdeModelFit$extract_odesol_df_dist(
  probs = c(0.1, 0.5, 0.9),
  include_y0 = FALSE,
  ydim_names = NULL
)
Arguments
probs

The percentile values. A numeric vector where all values are between 0 and 1.

include_y0

Should the initial state be included?

ydim_names

Names of the ODE dimensions. If NULL, these are automatically set as "y1", "y2", etc.

Returns

A data.frame.


Method plot_odesol()

A quick way to plot the ODE solutions.

Usage
OdeModelFit$plot_odesol(
  draw_inds = NULL,
  alpha = 0.75,
  color = "firebrick",
  ...
)
Arguments
draw_inds

If this numeric and positive, plots ODE solutions corresponding only to given draws. If this is 0, all draws are plotted. If this is NULL, a random subset of at most 100 draws are plotted.

alpha

line alpha

color

line color

...

other arguments passed to extract_odesol_df

Returns

A ggplot object.


Method plot_odesol_dist()

A quick way to plot the marginal distribution of ODE solutions at each time point.

Usage
OdeModelFit$plot_odesol_dist(
  p = 0.8,
  alpha = 0.75,
  color = "firebrick",
  fill_color = "firebrick",
  ...
)
Arguments
p

Which percentage central interval to plot?

alpha

fill alpha

color

line color

fill_color

fill color

...

other arguments passed to extract_odesol_df_dist

Returns

A ggplot object.


Method clone()

The objects of this class are cloneable with this method.

Usage
OdeModelFit$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

See Also

Other model fit classes: OdeModelGQ, OdeModelMCMC


jtimonen/odemodeling documentation built on Sept. 15, 2024, 4:29 a.m.