VB fit object
VB fit object
Details
An R6 class storing posterior samples and related information from Automatic Differentiation Variational Inference (ADVI).
Super class
BayesRTMB::RTMB_Fit_Base -> advi_fit
Public fields
modelAn `RTMB_Model` object used for estimation.
fitA 3D array of posterior draws for fixed model parameters.
random_fitA 3D array of posterior draws for random effects, if available.
transform_fitA 3D array of posterior draws for transformed parameters, if available.
generate_fitA 3D array of posterior draws for generated quantities, if available.
transform_dimsA list storing dimension information for transformed parameters.
generate_dimsA list storing dimension information for generated quantities.
elbo_historyA list of numeric vectors storing the Evidence Lower Bound (ELBO) history during optimization for each chain.
laplaceLogical; whether Laplace approximation was used to marginalize random effects.
posterior_meanA named numeric vector of posterior mean estimates.
ELBOA numeric vector of final ELBO values for each chain.
rel_obj_valsA numeric vector of final relative objective tolerance values for each chain.
best_chainInteger; the index of the chain with the maximum ELBO.
mu_historyMatrix of the parameter trajectory from the final window.
Methods
Inherited methods
Method get_point_estimate()
Get point estimate for a target parameter.
Method estimate()
Get point estimates from variational draws.
Arguments
parsOptional character or numeric vector of parameter names or indices to extract. Supports special keywords: "parameters", "transform", "generate", and "all".
typeCharacter string specifying the estimation type.
componentCharacter string specifying the component to filter by.
chainsNumeric vector of chains to include. If NULL, the best chain is used.
best_chainsNumber of best chains to include.
dropLogical; if TRUE and only one parameter is selected, return the value directly instead of a list.
...Additional arguments passed to draws().
Method EAP()
Calculate Expected A Posteriori (EAP) estimates from the best variational estimate by default.
Arguments
parsOptional character vector of parameter names to extract.
chainsNumeric vector of chains to include. If NULL, the best chain is used.
best_chainsNumber of best chains to include.
dropLogical; whether to drop the list if only one parameter is selected.
...Additional arguments passed to `estimate()`.
Method MAP()
Calculate Maximum A Posteriori (MAP) estimates from the best variational estimate by default.
Usage
VB_Fit$MAP(
pars = "parameters",
chains = NULL,
best_chains = NULL,
type = c("marginal", "joint"),
drop = TRUE,
...
)Arguments
parsOptional character vector of parameter names to extract.
chainsNumeric vector of chains to include. If NULL, the best chain is used.
best_chainsNumber of best chains to include.
typeCharacter string; "marginal" or "joint" MAP.
dropLogical; whether to drop the list if only one parameter is selected.
...Additional arguments passed to `estimate()`.
Method new()
Create a new `VB_Fit` object.
Usage
VB_Fit$new(
model,
fit,
random_fit,
elbo_history,
laplace,
posterior_mean,
ELBO,
rel_obj_vals,
best_chain,
mu_history
)Arguments
modelAn `RTMB_Model` object.
fitA 3D array of parameter draws.
random_fitA 3D array of random effect draws.
elbo_historyA list of numeric vectors of ELBO values for each chain.
laplaceLogical; indicates if Laplace approximation was used.
posterior_meanA named numeric vector of posterior means.
ELBOA numeric vector of final ELBO values for each chain.
rel_obj_valsA numeric vector of final relative objective tolerance values for each chain.
best_chainInteger; the index of the chain with the maximum ELBO.
mu_historyMatrix of the parameter trajectory from the final window.
Method print()
Print a brief summary of the fitted object.
Method draws()
Extract posterior draws for selected parameters.
Usage
VB_Fit$draws(
pars = NULL,
chains = NULL,
best_chains = NULL,
inc_random = FALSE,
inc_transform = TRUE,
inc_generate = TRUE,
best_only = FALSE
)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 ELBO. If supplied, chains with the highest ELBO are retained.
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.
best_onlyLogical; whether to extract only from the chain with the maximum ELBO. Default is FALSE unless explicitly requested.
Method summary()
Summarize posterior draws.
Usage
VB_Fit$summary(
pars = 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.
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 plot_elbo()
Plot the ELBO history to diagnose convergence.
Arguments
tail_nInteger; the number of recent iterations to plot. If NULL, plots the entire history. Default is 2000.
estsCharacter string `"best"`, numeric vector of estimate indices (e.g., `c(1, 3)`), or `NULL` to plot all. Default is `NULL`.
typeCharacter string; the type of plot. Default is "l" (lines).
...Additional arguments passed to the `plot` function.
Method plot_trajectory()
Plot the parameter trajectory from the final optimization window.
Method transformed_draws()
Compute transformed parameters from posterior draws.
Arguments
tran_fnAn optional user-supplied function that takes data and parameter lists to return transformed quantities.
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.