define_rng: Define and evaluate random number generation expressions

View source: R/model_def.R

define_rngR Documentation

Define and evaluate random number generation expressions

Description

Random number generation expressions are used to randomly sample model parameters from suitable distributions for probabilistic sensitivity analysis. These functions are typically used when evaluating an object of class model_def defined using define_model().

Usage

define_rng(expr, n = 1, ...)

eval_rng(x, params = NULL, check = TRUE)

Arguments

expr

An expression used to randomly draw variates for each parameter of interest in the model. Braces should be used so that the result of the last expression within the braces is evaluated. The expression must return a list where each element is either a vector or tabular object ( matrix, data.frame, or data.table). The length of the vector must either be or n and the number of rows in the tabular object must be n.

n

Number of samples of the parameters to draw.

...

Additional arguments to pass to the environment used to evaluate expr.

x

An object of class rng_def created with define_rng().

params

A list containing the values of parameters for random number generation. Each element of the list should either be a vector, matrix, data.frame, or data.table

check

Whether to check the returned output so that (i) it returns a list and (ii) each element has the correct length or number of rows. Default is TRUE, meaning that any output can be returned. This is always TRUE when used inside define_model().

Details

hesim contains a number of random number generation functions that return parameter samples in convenient formats and do not typically require the number of samples, n, as arguments (see rng_distributions). The random number generation expressions are evaluated using eval_rng() and used within expr in define_rng(). If a multivariate object is returned by eval_rng(), then the rows are random samples and columns are distinct parameters (e.g., costs for each health state, elements of a transition probability matrix).

Value

define_rng() returns an object of class rng_def, which is a list containing the unevaluated random number generation expressions passed to expr, n, and any additional arguments passed to ... . eval_rng() evaluates the rng_def object and returns an eval_rng object containing the evaluated expression.

See Also

Parameters can be conveniently sampled from probability distributions using a number of random number generation functions (see rng_distributions). An economic model can be created with create_CohortDtstm() by using define_rng() (or a previously evaluated eval_rng object) alongside define_tparams() to define a model with define_model(). It can be useful to summarize an evaluated expression with summary.eval_rng().

Examples

 
params <- list(
  alpha = matrix(c(75, 25, 33, 67), byrow = TRUE, ncol = 2),
  inptcost_mean = c(A = 900, B = 1500, C = 2000),
  outptcost_mean = matrix(c(300, 600, 800,
                            400, 700, 700),
                           ncol = 3, byrow = TRUE)
)
rng_def <- define_rng({
  aecost_mean <- c(500, 800, 1000) # Local object not 
                                   # not returned by eval_rng()
  list( # Sampled values of parameters returned by eval_rng()
    p = dirichlet_rng(alpha), # Default column names
    inptcost = gamma_rng(mean = inptcost_mean, # Column names based on 
                         sd = inptcost_mean),  # named vector
    outptcost = outptcost_mean, # No column names because
                                # outptcost_mean has none.
    aecost = gamma_rng(mean = aecost_mean, # Explicit naming of columns
                       sd = aecost_mean,
                       names = aecost_colnames)
  )
}, n = 2, aecost_colnames = c("A", "B", "C")) # Add aecost_colnames to environment
params_sample <- eval_rng(x = rng_def, params)
summary(params_sample)
params_sample

dincerti/cea documentation built on Feb. 16, 2024, 1:15 p.m.