Title: | Estimate Parameters for Arbitrary R Functions using 'Stan' |
---|---|
Description: | Allows for the estimation of parameters for 'R' functions using the various algorithms implemented in the 'Stan' probabilistic programming language. |
Authors: | Andrew R. Johnson [aut, cre] , Jonah Gabry [ctb], Rok Češnovar [ctb], Stan Development Team [cph] (CmdStan sources and headers), Lawrence Livermore National Security [cph] (SUNDIALS sources and headers) |
Maintainer: | Andrew R. Johnson <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.2.9000 |
Built: | 2024-11-07 05:45:12 UTC |
Source: | https://github.com/andrjohns/StanEstimators |
ToDO
Maintainer: Andrew R. Johnson [email protected] (ORCID)
Other contributors:
Jonah Gabry [email protected] [contributor]
Rok Češnovar [email protected] [contributor]
Stan Development Team (CmdStan sources and headers) [copyright holder]
Lawrence Livermore National Security (SUNDIALS sources and headers) [copyright holder]
Constrain a vector of variables.
constrain_variables(stan_object, unconstrained_variables) ## S4 method for signature 'StanBase' constrain_variables(stan_object, unconstrained_variables)
constrain_variables(stan_object, unconstrained_variables) ## S4 method for signature 'StanBase' constrain_variables(stan_object, unconstrained_variables)
stan_object |
A |
unconstrained_variables |
Vector of unconstrained variables. |
Calculate the log probability and its gradient of the model given a vector of unconstrained variables.
grad_log_prob(stan_object, unconstrained_variables, jacobian = TRUE) ## S4 method for signature 'StanBase' grad_log_prob(stan_object, unconstrained_variables, jacobian = TRUE)
grad_log_prob(stan_object, unconstrained_variables, jacobian = TRUE) ## S4 method for signature 'StanBase' grad_log_prob(stan_object, unconstrained_variables, jacobian = TRUE)
stan_object |
A |
unconstrained_variables |
Vector of unconstrained variables. |
jacobian |
Whether to include the Jacobian adjustment. |
Calculate the log probability, its gradient, and its hessian matrix of the model given a vector of unconstrained variables.
hessian(stan_object, unconstrained_variables, jacobian = TRUE) ## S4 method for signature 'StanBase' hessian(stan_object, unconstrained_variables, jacobian = TRUE)
hessian(stan_object, unconstrained_variables, jacobian = TRUE) ## S4 method for signature 'StanBase' hessian(stan_object, unconstrained_variables, jacobian = TRUE)
stan_object |
A |
unconstrained_variables |
Vector of unconstrained variables. |
jacobian |
Whether to include the Jacobian adjustment. |
Calculate the log probability of the model given a vector of unconstrained variables.
log_prob(stan_object, unconstrained_variables, jacobian = TRUE) ## S4 method for signature 'StanBase' log_prob(stan_object, unconstrained_variables, jacobian = TRUE)
log_prob(stan_object, unconstrained_variables, jacobian = TRUE) ## S4 method for signature 'StanBase' log_prob(stan_object, unconstrained_variables, jacobian = TRUE)
stan_object |
A |
unconstrained_variables |
Vector of unconstrained variables. |
jacobian |
Whether to include the Jacobian adjustment. |
Calculate approximate leave-one-out cross-validation (LOO-CV) for a model.
## S3 method for class 'StanBase' loo(x, pointwise_ll_fun, additional_args = list(), moment_match = FALSE, ...)
## S3 method for class 'StanBase' loo(x, pointwise_ll_fun, additional_args = list(), moment_match = FALSE, ...)
x |
A |
pointwise_ll_fun |
Function that calculates the pointwise log-likelihood given a vector of parameter values. |
additional_args |
List of additional arguments to be passed to
|
moment_match |
(logical) Whether to use a moment-matching correction for problematic observations. |
... |
Additional arguments to be passed to |
Check gradient estimation using Stan's 'Diagnose' method
stan_diagnose( fn, par_inits = NULL, n_pars = NULL, additional_args = list(), grad_fun = NULL, lower = -Inf, upper = Inf, eval_standalone = FALSE, globals = TRUE, packages = NULL, seed = NULL, refresh = NULL, quiet = FALSE, output_dir = NULL, output_basename = NULL, sig_figs = NULL )
stan_diagnose( fn, par_inits = NULL, n_pars = NULL, additional_args = list(), grad_fun = NULL, lower = -Inf, upper = Inf, eval_standalone = FALSE, globals = TRUE, packages = NULL, seed = NULL, refresh = NULL, quiet = FALSE, output_dir = NULL, output_basename = NULL, sig_figs = NULL )
fn |
Function to estimate parameters for |
par_inits |
Initial values for parameters
(must be specified if |
n_pars |
Number of parameters to estimate
(must be specified if |
additional_args |
List of additional arguments to pass to the function |
grad_fun |
Function calculating gradients w.r.t. each parameter |
lower |
Lower bound constraint(s) for parameters |
upper |
Upper bound constraint(s) for parameters |
eval_standalone |
(logical) Whether to evaluate the function in a
separate R session. Defaults to |
globals |
(optional) a logical, a character vector, or a named list
to control how globals are handled when evaluating functions in a
separate R session. Ignored if |
packages |
(optional) a character vector specifying packages
to be attached in the R environment evaluating the function.
Ignored if |
seed |
Random seed |
refresh |
Number of iterations for printing |
quiet |
(logical) Whether to suppress Stan's output |
output_dir |
Directory to store outputs |
output_basename |
Basename to use for output files |
sig_figs |
Number of significant digits to use for printing |
StanLaplace
object
Estimate parameters using Stan's laplace algorithm
stan_laplace( fn, par_inits = NULL, n_pars = NULL, additional_args = list(), grad_fun = NULL, lower = -Inf, upper = Inf, eval_standalone = FALSE, globals = TRUE, packages = NULL, seed = NULL, refresh = NULL, quiet = FALSE, output_dir = NULL, output_basename = NULL, sig_figs = NULL, mode = NULL, jacobian = NULL, draws = NULL, opt_args = NULL )
stan_laplace( fn, par_inits = NULL, n_pars = NULL, additional_args = list(), grad_fun = NULL, lower = -Inf, upper = Inf, eval_standalone = FALSE, globals = TRUE, packages = NULL, seed = NULL, refresh = NULL, quiet = FALSE, output_dir = NULL, output_basename = NULL, sig_figs = NULL, mode = NULL, jacobian = NULL, draws = NULL, opt_args = NULL )
fn |
Function to estimate parameters for |
par_inits |
Initial values for parameters
(must be specified if |
n_pars |
Number of parameters to estimate
(must be specified if |
additional_args |
List of additional arguments to pass to the function |
grad_fun |
Function calculating gradients w.r.t. each parameter |
lower |
Lower bound constraint(s) for parameters |
upper |
Upper bound constraint(s) for parameters |
eval_standalone |
(logical) Whether to evaluate the function in a
separate R session. Defaults to |
globals |
(optional) a logical, a character vector, or a named list
to control how globals are handled when evaluating functions in a
separate R session. Ignored if |
packages |
(optional) a character vector specifying packages
to be attached in the R environment evaluating the function.
Ignored if |
seed |
Random seed |
refresh |
Number of iterations for printing |
quiet |
(logical) Whether to suppress Stan's output |
output_dir |
Directory to store outputs |
output_basename |
Basename to use for output files |
sig_figs |
Number of significant digits to use for printing |
mode |
Mode for the laplace approximation, can either be a vector of values, a StanOptimize object, or NULL. |
jacobian |
(logical) Whether or not to use the Jacobian adjustment for constrained variables. |
draws |
(positive integer) Number of approximate posterior samples to draw and save. |
opt_args |
(named list) A named list of optional arguments to pass to
|
StanLaplace
object
Estimate parameters using Stan's optimization algorithms
stan_optimize( fn, par_inits = NULL, n_pars = NULL, additional_args = list(), algorithm = "lbfgs", grad_fun = NULL, lower = -Inf, upper = Inf, eval_standalone = FALSE, globals = TRUE, packages = NULL, seed = NULL, refresh = NULL, quiet = FALSE, output_dir = NULL, output_basename = NULL, sig_figs = NULL, save_iterations = NULL, jacobian = NULL, init_alpha = NULL, iter = NULL, tol_obj = NULL, tol_rel_obj = NULL, tol_grad = NULL, tol_rel_grad = NULL, tol_param = NULL, history_size = NULL )
stan_optimize( fn, par_inits = NULL, n_pars = NULL, additional_args = list(), algorithm = "lbfgs", grad_fun = NULL, lower = -Inf, upper = Inf, eval_standalone = FALSE, globals = TRUE, packages = NULL, seed = NULL, refresh = NULL, quiet = FALSE, output_dir = NULL, output_basename = NULL, sig_figs = NULL, save_iterations = NULL, jacobian = NULL, init_alpha = NULL, iter = NULL, tol_obj = NULL, tol_rel_obj = NULL, tol_grad = NULL, tol_rel_grad = NULL, tol_param = NULL, history_size = NULL )
fn |
Function to estimate parameters for |
par_inits |
Initial values for parameters
(must be specified if |
n_pars |
Number of parameters to estimate
(must be specified if |
additional_args |
List of additional arguments to pass to the function |
algorithm |
(string) The optimization algorithm. One of |
grad_fun |
Function calculating gradients w.r.t. each parameter |
lower |
Lower bound constraint(s) for parameters |
upper |
Upper bound constraint(s) for parameters |
eval_standalone |
(logical) Whether to evaluate the function in a
separate R session. Defaults to |
globals |
(optional) a logical, a character vector, or a named list
to control how globals are handled when evaluating functions in a
separate R session. Ignored if |
packages |
(optional) a character vector specifying packages
to be attached in the R environment evaluating the function.
Ignored if |
seed |
Random seed |
refresh |
Number of iterations for printing |
quiet |
(logical) Whether to suppress Stan's output |
output_dir |
Directory to store outputs |
output_basename |
Basename to use for output files |
sig_figs |
Number of significant digits to use for printing |
save_iterations |
Save optimization iterations to output file |
jacobian |
(logical) Whether or not to use the Jacobian adjustment for
constrained variables. For historical reasons, the default is |
init_alpha |
(positive real) The initial step size parameter. |
iter |
(positive integer) The maximum number of iterations. |
tol_obj |
(positive real) Convergence tolerance on changes in objective function value. |
tol_rel_obj |
(positive real) Convergence tolerance on relative changes in objective function value. |
tol_grad |
(positive real) Convergence tolerance on the norm of the gradient. |
tol_rel_grad |
(positive real) Convergence tolerance on the relative norm of the gradient. |
tol_param |
(positive real) Convergence tolerance on changes in parameter value. |
history_size |
(positive integer) The size of the history used when approximating the Hessian. Only available for L-BFGS. |
StanOptimize
object
Estimate parameters using Stan's pathfinder algorithm
stan_pathfinder( fn, par_inits = NULL, n_pars = NULL, additional_args = list(), grad_fun = NULL, lower = -Inf, upper = Inf, eval_standalone = FALSE, globals = TRUE, packages = NULL, seed = NULL, refresh = NULL, quiet = FALSE, output_dir = NULL, output_basename = NULL, sig_figs = NULL, init_alpha = NULL, tol_obj = NULL, tol_rel_obj = NULL, tol_grad = NULL, tol_rel_grad = NULL, tol_param = NULL, history_size = NULL, num_psis_draws = NULL, num_paths = NULL, save_single_paths = NULL, max_lbfgs_iters = NULL, num_draws = NULL, num_elbo_draws = NULL )
stan_pathfinder( fn, par_inits = NULL, n_pars = NULL, additional_args = list(), grad_fun = NULL, lower = -Inf, upper = Inf, eval_standalone = FALSE, globals = TRUE, packages = NULL, seed = NULL, refresh = NULL, quiet = FALSE, output_dir = NULL, output_basename = NULL, sig_figs = NULL, init_alpha = NULL, tol_obj = NULL, tol_rel_obj = NULL, tol_grad = NULL, tol_rel_grad = NULL, tol_param = NULL, history_size = NULL, num_psis_draws = NULL, num_paths = NULL, save_single_paths = NULL, max_lbfgs_iters = NULL, num_draws = NULL, num_elbo_draws = NULL )
fn |
Function to estimate parameters for |
par_inits |
Initial values for parameters
(must be specified if |
n_pars |
Number of parameters to estimate
(must be specified if |
additional_args |
List of additional arguments to pass to the function |
grad_fun |
Function calculating gradients w.r.t. each parameter |
lower |
Lower bound constraint(s) for parameters |
upper |
Upper bound constraint(s) for parameters |
eval_standalone |
(logical) Whether to evaluate the function in a
separate R session. Defaults to |
globals |
(optional) a logical, a character vector, or a named list
to control how globals are handled when evaluating functions in a
separate R session. Ignored if |
packages |
(optional) a character vector specifying packages
to be attached in the R environment evaluating the function.
Ignored if |
seed |
Random seed |
refresh |
Number of iterations for printing |
quiet |
(logical) Whether to suppress Stan's output |
output_dir |
Directory to store outputs |
output_basename |
Basename to use for output files |
sig_figs |
Number of significant digits to use for printing |
init_alpha |
(positive real) The initial step size parameter. |
tol_obj |
(positive real) Convergence tolerance on changes in objective function value. |
tol_rel_obj |
(positive real) Convergence tolerance on relative changes in objective function value. |
tol_grad |
(positive real) Convergence tolerance on the norm of the gradient. |
tol_rel_grad |
(positive real) Convergence tolerance on the relative norm of the gradient. |
tol_param |
(positive real) Convergence tolerance on changes in parameter value. |
history_size |
(positive integer) The size of the history used when approximating the Hessian. |
num_psis_draws |
(positive integer) Number PSIS draws to return. |
num_paths |
(positive integer) Number of single pathfinders to run. |
save_single_paths |
(logical) Whether to save the results of single pathfinder runs in multi-pathfinder. |
max_lbfgs_iters |
(positive integer) The maximum number of iterations for LBFGS. |
num_draws |
(positive integer) Number of draws to return after performing pareto smooted importance sampling (PSIS). |
num_elbo_draws |
(positive integer) Number of draws to make when calculating the ELBO of the approximation at each iteration of LBFGS. |
StanPathfinder
object
Estimate parameters using Stan's sampling algorithms
stan_sample( fn, par_inits = NULL, n_pars = NULL, additional_args = list(), algorithm = "hmc", engine = "nuts", grad_fun = NULL, lower = -Inf, upper = Inf, eval_standalone = (parallel_chains > 1), globals = TRUE, packages = NULL, seed = NULL, refresh = NULL, quiet = FALSE, output_dir = NULL, output_basename = NULL, sig_figs = NULL, num_chains = 4, parallel_chains = 1, num_samples = 1000, num_warmup = 1000, save_warmup = NULL, thin = NULL, adapt_engaged = NULL, adapt_gamma = NULL, adapt_delta = NULL, adapt_kappa = NULL, adapt_t0 = NULL, adapt_init_buffer = NULL, adapt_term_buffer = NULL, adapt_window = NULL, int_time = NULL, max_treedepth = NULL, metric = NULL, metric_file = NULL, stepsize = NULL, stepsize_jitter = NULL )
stan_sample( fn, par_inits = NULL, n_pars = NULL, additional_args = list(), algorithm = "hmc", engine = "nuts", grad_fun = NULL, lower = -Inf, upper = Inf, eval_standalone = (parallel_chains > 1), globals = TRUE, packages = NULL, seed = NULL, refresh = NULL, quiet = FALSE, output_dir = NULL, output_basename = NULL, sig_figs = NULL, num_chains = 4, parallel_chains = 1, num_samples = 1000, num_warmup = 1000, save_warmup = NULL, thin = NULL, adapt_engaged = NULL, adapt_gamma = NULL, adapt_delta = NULL, adapt_kappa = NULL, adapt_t0 = NULL, adapt_init_buffer = NULL, adapt_term_buffer = NULL, adapt_window = NULL, int_time = NULL, max_treedepth = NULL, metric = NULL, metric_file = NULL, stepsize = NULL, stepsize_jitter = NULL )
fn |
Function to estimate parameters for |
par_inits |
Initial values for parameters
(must be specified if |
n_pars |
Number of parameters to estimate
(must be specified if |
additional_args |
List of additional arguments to pass to the function |
algorithm |
(string) The sampling algorithm. One of |
engine |
(string) The |
grad_fun |
Function calculating gradients w.r.t. each parameter |
lower |
Lower bound constraint(s) for parameters |
upper |
Upper bound constraint(s) for parameters |
eval_standalone |
(logical) Whether to evaluate the function in a
separate R session. Defaults to |
globals |
(optional) a logical, a character vector, or a named list
to control how globals are handled when evaluating functions in a
separate R session. Ignored if |
packages |
(optional) a character vector specifying packages
to be attached in the R environment evaluating the function.
Ignored if |
seed |
Random seed |
refresh |
Number of iterations for printing |
quiet |
(logical) Whether to suppress Stan's output |
output_dir |
Directory to store outputs |
output_basename |
Basename to use for output files |
sig_figs |
Number of significant digits to use for printing |
num_chains |
(positive integer) The number of Markov chains to run. The default is 4. |
parallel_chains |
(positive integer) The number of chains to run in parallel, the default is 1. |
num_samples |
(positive integer) The number of post-warmup iterations to run per chain. |
num_warmup |
(positive integer) The number of warmup iterations to run per chain. |
save_warmup |
(logical) Should warmup iterations be saved? The default
is |
thin |
(positive integer) The period between saved samples. This should typically be left at its default (no thinning) unless memory is a problem. |
adapt_engaged |
(logical) Do warmup adaptation? The default is |
adapt_gamma |
(positive real) Adaptation regularization scale. |
adapt_delta |
(real in |
adapt_kappa |
(positive real) Adaptation relaxation exponent. |
adapt_t0 |
(positive real) Adaptation iteration offset. |
adapt_init_buffer |
(nonnegative integer) Width of initial fast timestep adaptation interval during warmup. |
adapt_term_buffer |
(nonnegative integer) Width of final fast timestep adaptation interval during warmup. |
adapt_window |
(nonnegative integer) Initial width of slow timestep/metric adaptation interval. |
int_time |
(positive real) Total integration time |
max_treedepth |
(positive integer) The maximum allowed tree depth for the NUTS engine. |
metric |
(string) One of |
metric_file |
(character vector) The paths to JSON or Rdump files (one per chain) compatible with CmdStan that contain precomputed inverse metrics. |
stepsize |
(positive real) The initial step size for the discrete approximation to continuous Hamiltonian dynamics. |
stepsize_jitter |
(real in |
StanMCMC
object
Estimate parameters using Stan's variational inference algorithms
stan_variational( fn, par_inits = NULL, n_pars = NULL, additional_args = list(), algorithm = "meanfield", grad_fun = NULL, lower = -Inf, upper = Inf, eval_standalone = FALSE, globals = TRUE, packages = NULL, seed = NULL, refresh = NULL, quiet = FALSE, output_dir = NULL, output_basename = NULL, sig_figs = NULL, iter = NULL, grad_samples = NULL, elbo_samples = NULL, eta = NULL, adapt_engaged = NULL, adapt_iter = NULL, tol_rel_obj = NULL, eval_elbo = NULL, output_samples = NULL )
stan_variational( fn, par_inits = NULL, n_pars = NULL, additional_args = list(), algorithm = "meanfield", grad_fun = NULL, lower = -Inf, upper = Inf, eval_standalone = FALSE, globals = TRUE, packages = NULL, seed = NULL, refresh = NULL, quiet = FALSE, output_dir = NULL, output_basename = NULL, sig_figs = NULL, iter = NULL, grad_samples = NULL, elbo_samples = NULL, eta = NULL, adapt_engaged = NULL, adapt_iter = NULL, tol_rel_obj = NULL, eval_elbo = NULL, output_samples = NULL )
fn |
Function to estimate parameters for |
par_inits |
Initial values for parameters
(must be specified if |
n_pars |
Number of parameters to estimate
(must be specified if |
additional_args |
List of additional arguments to pass to the function |
algorithm |
(string) The variational inference algorithm. One of
|
grad_fun |
Function calculating gradients w.r.t. each parameter |
lower |
Lower bound constraint(s) for parameters |
upper |
Upper bound constraint(s) for parameters |
eval_standalone |
(logical) Whether to evaluate the function in a
separate R session. Defaults to |
globals |
(optional) a logical, a character vector, or a named list
to control how globals are handled when evaluating functions in a
separate R session. Ignored if |
packages |
(optional) a character vector specifying packages
to be attached in the R environment evaluating the function.
Ignored if |
seed |
Random seed |
refresh |
Number of iterations for printing |
quiet |
(logical) Whether to suppress Stan's output |
output_dir |
Directory to store outputs |
output_basename |
Basename to use for output files |
sig_figs |
Number of significant digits to use for printing |
iter |
(positive integer) The maximum number of iterations. |
grad_samples |
(positive integer) The number of samples for Monte Carlo estimate of gradients. |
elbo_samples |
(positive integer) The number of samples for Monte Carlo estimate of ELBO (objective function). |
eta |
(positive real) The step size weighting parameter for adaptive step size sequence. |
adapt_engaged |
(logical) Do warmup adaptation? |
adapt_iter |
(positive integer) The maximum number of adaptation iterations. |
tol_rel_obj |
(positive real) Convergence tolerance on the relative norm of the objective. |
eval_elbo |
(positive integer) Evaluate ELBO every Nth iteration. |
output_samples |
(positive integer) Number of approximate posterior samples to draw and save. |
StanVariational
object
stan_versions
stan_versions()
stan_versions()
A named list with the Stan and Stan Math library versions
StanLaplace
.Summary method for objects of class StanLaplace
.
## S4 method for signature 'StanLaplace' summary(object, ...)
## S4 method for signature 'StanLaplace' summary(object, ...)
object |
A |
... |
Additional arguments, currently unused. |
StanMCMC
.Summary method for objects of class StanMCMC
.
## S4 method for signature 'StanMCMC' summary(object, ...)
## S4 method for signature 'StanMCMC' summary(object, ...)
object |
A |
... |
Additional arguments, currently unused. |
StanOptimize
.Summary method for objects of class StanOptimize
.
## S4 method for signature 'StanOptimize' summary(object, ...)
## S4 method for signature 'StanOptimize' summary(object, ...)
object |
A |
... |
Additional arguments, currently unused. |
StanPathfinder
.Summary method for objects of class StanPathfinder
.
## S4 method for signature 'StanPathfinder' summary(object, ...)
## S4 method for signature 'StanPathfinder' summary(object, ...)
object |
A |
... |
Additional arguments, currently unused. |
StanVariational
.Summary method for objects of class StanVariational
.
## S4 method for signature 'StanVariational' summary(object, ...)
## S4 method for signature 'StanVariational' summary(object, ...)
object |
A |
... |
Additional arguments, currently unused. |
Unconstrain all parameter draws.
unconstrain_draws(stan_object, draws = NULL) ## S4 method for signature 'StanBase' unconstrain_draws(stan_object, draws = NULL) ## S4 method for signature 'StanOptimize' unconstrain_draws(stan_object, draws = NULL)
unconstrain_draws(stan_object, draws = NULL) ## S4 method for signature 'StanBase' unconstrain_draws(stan_object, draws = NULL) ## S4 method for signature 'StanOptimize' unconstrain_draws(stan_object, draws = NULL)
stan_object |
A |
draws |
(optional) A |
Unconstrain a vector of variables.
unconstrain_variables(stan_object, variables) ## S4 method for signature 'StanBase' unconstrain_variables(stan_object, variables)
unconstrain_variables(stan_object, variables) ## S4 method for signature 'StanBase' unconstrain_variables(stan_object, variables)
stan_object |
A |
variables |
Vector of variables to be unconstrained. |