Learn R Programming

hesim (version 0.5.0)

define_rng: 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 = FALSE)

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, matrix, data.frame, or data.table. The length of the vector and number of rows in the matrix/data.frame/data.table, must either be 1 or 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 FALSE, meaning that any output can be returned. This is always TRUE when used inside define_model().

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 should return a list.

Details

hesim contains a number of random number generation functions that return parameter samples in convenient formats and do not 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 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).

See Also

rng_distributions, define_model(), define_tparams()

Examples

Run this code
# NOT RUN {
 
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
eval_rng(x = rng_def, params)
# }

Run the code above in your browser using DataLab