MCMC fit object
MCMC fit object
Details
An R6 class storing posterior samples and related information from MCMC estimation.
Bayes factors are computed as ratios of marginal likelihoods estimated by bridge sampling. The comparison model can be constructed by fixing parameters with `fixed = list(...)`, or supplied as an already fitted `MCMC_Fit` object via `comparison_fit`. Bayes factors are computed only by marginal-likelihood model comparison.
Super class
BayesRTMB::RTMB_Fit_Base -> mcmc_fit
Public fields
modelAn `RTMB_Model` object used for estimation.
fitPosterior draws for model parameters.
random_fitPosterior draws for random effects.
transform_fitPosterior draws for transformed parameters.
transform_dimsDimension information for transformed parameters.
generate_fitPosterior draws for generated quantities.
generate_dimsDimension information for generated quantities.
epsStep size used by the sampler.
acceptAcceptance statistics from sampling.
treedepthTree depth used in HMC/NUTS sampling.
n_leapfrogNumber of leapfrog steps for retained draws.
divergentLogical matrix indicating divergent transitions.
energyHamiltonian energy for retained draws.
metricPer-chain inverse mass matrix used by NUTS.
metric_typeEffective mass matrix adaptation type used by NUTS.
metric_requestedMass matrix adaptation type requested by the user.
metric_effectiveEffective mass matrix adaptation type by chain.
metric_autoAutomatic metric selection details by chain, if used.
metric_initInitial metric source used by NUTS.
metric_adaptationMetric learning mode used during warmup.
nuts_variantNUTS proposal selection variant used for sampling.
warmup_diagnosticsPer-chain warmup diagnostics.
laplaceLogical; whether Laplace approximation was used.
posterior_meanPosterior mean estimates.
log_mlNumeric value storing the calculated log marginal likelihood from bridge sampling.
comparison_fitAn
MCMC_Fitobject containing the fitted comparison model.max_treedepthMaximum tree depth requested for NUTS/HMC.
pd_error_countPositive-definite/singularity errors treated as lp = -Inf by chain.
Methods
Method get_point_estimate()
Get point estimate for a target parameter.
Method new()
Create a new `MCMC_Fit` object.
Usage
MCMC_Fit$new(
model,
fit,
random_fit,
eps,
accept,
treedepth,
laplace,
posterior_mean,
max_treedepth = NULL,
pd_error_count = NULL,
n_leapfrog = NULL,
divergent = NULL,
energy = NULL,
metric = NULL,
metric_type = NULL,
metric_init = NULL,
metric_requested = NULL,
metric_effective = NULL,
metric_auto = NULL,
metric_adaptation = NULL,
nuts_variant = NULL,
warmup_diagnostics = NULL
)Arguments
modelAn `RTMB_Model` object used for estimation.
fitPosterior draws for model parameters.
random_fitPosterior draws for random effects, if available.
epsStep size used by the sampler.
acceptAcceptance statistics from sampling.
treedepthTree depth used in HMC/NUTS sampling.
laplaceLogical; whether Laplace approximation was used.
posterior_meanPosterior mean estimates.
max_treedepthMaximum tree depth requested for NUTS/HMC.
pd_error_countPositive-definite/singularity errors treated as `lp = -Inf` by chain.
n_leapfrogNumber of leapfrog steps for retained draws.
divergentLogical matrix indicating divergent transitions.
energyHamiltonian energy for retained draws.
metricPer-chain inverse mass matrix used by NUTS.
metric_typeEffective mass matrix adaptation type used by NUTS.
metric_initInitial metric source used by NUTS.
metric_requestedMass matrix adaptation type requested by the user.
metric_effectiveEffective mass matrix adaptation type by chain.
metric_autoAutomatic metric selection details by chain, if used.
metric_adaptationMetric learning mode used during warmup.
nuts_variantNUTS proposal selection variant used for sampling.
warmup_diagnosticsPer-chain warmup diagnostics.
Method print()
Print a brief summary of the fitted object.
Method draws()
Extract posterior draws for selected parameters.
Usage
MCMC_Fit$draws(
pars = NULL,
chains = NULL,
best_chains = NULL,
inc_random = FALSE,
inc_transform = TRUE,
inc_generate = TRUE
)Arguments
parsCharacter or numeric vector specifying the names or indices of parameters to extract. If NULL, all available parameters are extracted.
chainsNumeric vector specifying the chains to extract. If NULL, draws from all chains are returned.
best_chainsInteger; number of best chains to retain based on mean log-posterior (lp).
inc_randomLogical; whether to include random effects in the output. Default is FALSE.
inc_transformLogical; whether to include transformed parameters in the output. Default is TRUE.
inc_generateLogical; whether to include generated quantities in the output. Default is TRUE.
Method summary()
Summarize posterior draws.
Usage
MCMC_Fit$summary(
pars = NULL,
chains = NULL,
best_chains = NULL,
max_rows = 10,
digits = 2,
inc_random = FALSE,
inc_transform = TRUE,
inc_generate = TRUE
)Arguments
parsCharacter or numeric vector specifying the names or indices of parameters to summarize. If NULL, all available parameters are summarized.
chainsNumeric vector specifying the chains to extract. If NULL, draws from all chains are used.
best_chainsInteger; number of best chains to retain based on mean log-posterior (lp).
max_rowsInteger; maximum number of rows to print in the summary table. Default is 10.
digitsInteger; number of decimal places to print. Default is 2.
inc_randomLogical; whether to include random effects in the summary. Default is FALSE.
inc_transformLogical; whether to include transformed parameters in the summary. Default is TRUE.
inc_generateLogical; whether to include generated quantities in the summary. Default is TRUE.
Method rhat_summary()
Summarize R-hat values.
Usage
MCMC_Fit$rhat_summary(
pars = NULL,
chains = NULL,
best_chains = NULL,
inc_random = FALSE,
inc_transform = TRUE,
inc_generate = FALSE,
finite = TRUE,
...
)Arguments
parsCharacter or numeric vector specifying parameters to include.
chainsNumeric vector specifying chains to include.
best_chainsInteger; number of best chains to retain based on mean log-posterior.
inc_randomLogical; whether to include random effects. Default is FALSE.
inc_transformLogical; whether to include transformed parameters. Default is TRUE.
inc_generateLogical; whether to include generated quantities. Default is FALSE.
finiteLogical; whether to drop non-finite or missing R-hat values. Default is TRUE.
...Additional arguments.
Method log_prob()
Evaluate log-probability values.
Method bridgesampling()
Estimate the marginal likelihood by bridge sampling.
Arguments
methodCharacter; the method to use for bridge sampling (e.g., "warp3", "normal"). Default is "warp3".
use_neffLogical; whether to use the effective sample size (ESS) to adjust for autocorrelation. Default is TRUE.
seedInteger; random seed for reproducibility. Default is NULL.
max_iterInteger; maximum number of iterations for the estimation algorithm. Default is 100.
Method bayes_factor()
Calculate the Bayes factor by marginal-likelihood model comparison.
Usage
MCMC_Fit$bayes_factor(
fixed = NULL,
comparison_fit = NULL,
bs_method = "normal",
error_threshold = 0.2,
...
)Arguments
fixedNamed list of parameter values used to construct the comparison model. For example,
fixed = list(delta = 0)orfixed = list("b[x]" = 0).comparison_fitOptional `MCMC_Fit` object for an already fitted comparison model.
bs_methodCharacter; the method to use for bridge sampling ("normal" or "warp3").
error_thresholdNumeric; threshold for the approximate error warning.
...Additional arguments passed to `sample()` when fitting the comparison model (e.g.,
chains = 4,sampling = 4000).
Method transformed_draws()
Compute transformed parameters from posterior draws.
Arguments
tran_fnA function for transformed parameters.
progressProgress reporting style: `"auto"`, `"none"`, `"bar"`, or `"message"`. `"auto"` and `"bar"` use line-based messages.
tapeDerived-quantity evaluation mode. `"auto"` tries RTMB tape evaluation and falls back to R evaluation on error; `"none"` always uses R evaluation; `"force"` requires tape evaluation and errors if it fails.
Method generated_quantities()
Compute generated quantities from posterior draws.
Arguments
codeAn `rtmb_code({ ... })` or `{ ... }` block containing the logic to be calculated using posterior samples.
progressProgress reporting style: `"auto"`, `"none"`, `"bar"`, or `"message"`. `"auto"` and `"bar"` use line-based messages.
tapeDerived-quantity evaluation mode. `"auto"` tries RTMB tape evaluation and falls back to R evaluation on error; `"none"` always uses R evaluation; `"force"` requires tape evaluation and errors if it fails.
Method resolve_switching()
Resolve label switching in posterior draws.
Usage
MCMC_Fit$resolve_switching(
target,
linked = NULL,
overwrite = TRUE,
scalar_fns = list()
)Arguments
targetCharacter string specifying the target variable to base the relabeling on.
linkedCharacter vector of variable names to be relabeled in the same order as the target. Default is NULL.
overwriteLogical; whether to overwrite the stored draws in the current object. Default is TRUE.
scalar_fnsA named list of functions to apply to scalar variables for relabeling. Default is an empty list.