Fits a compact density approximation to a matrix of MCMC draws and
returns an object of class posterior_compressed. The compressed
object can be sampled from via sample_posterior() and evaluated via
density_posterior().
compress_posterior(
draws,
method = c("mclust", "mvdens_gmm", "mvdens_kde"),
variables = NULL,
n_components = 3L,
model_name = NULL,
verbose = FALSE,
...
)An object containing posterior draws. Accepted:
a numeric matrix (rows = draws, cols = parameters);
a data.frame of draws (one column per parameter);
any object the posterior package can convert with
posterior::as_draws_matrix() (draws_matrix, draws_array, ...);
a 3-D array of shape iter x chain x var.
One of 'mclust', 'mvdens_gmm', 'mvdens_kde'.
See compression_methods().
Optional character vector of parameter names to keep.
If NULL (default) all columns are used.
Integer number of mixture components (used by
"mclust" and "mvdens_gmm"). Default 3.
mclust covariance structure (e.g. "VVV", "EEE",
or a vector of allowed model names). Ignored by other methods.
When NULL (default) poco auto-selects a sensible set: the
spherical and diagonal models c("EII", "VII", "EEI", "EVI", "VEI", "VVI") are used when nrow(draws) <= ncol(draws) so covariances
remain identifiable, otherwise mclust's full default set is used
and BIC picks the best.
Logical; print backend progress. Default FALSE.
Additional arguments forwarded to the backend (e.g.
mclust::Mclust()).
An S3 list of class c("posterior_compressed_<method>", "posterior_compressed", "list") containing the parameters of the
fitted approximation.
set.seed(1)
draws <- matrix(rnorm(2000 * 3), ncol = 3,
dimnames = list(NULL, c("alpha", "beta", "sigma")))
comp <- compress_posterior(draws, method = "mclust", n_components = 2)
#> ℹ mclust: trying all 14 covariance models c(EII, VII, EEI, VEI, EVI, VVI, EEE, VEE, EVE, VVE, EEV, VEV, EVV, VVV) and picking the best by BIC (n = 2000, d = 3).
#> ℹ mclust: selected model 'EII' with G = 2 (BIC = -17,298.09) out of 14 candidate models: EII, VII, EEI, VEI, EVI, VVI, EEE, VEE, EVE, VVE, EEV, VEV, EVV, VVV.
comp
#> <posterior_compressed: mclust >
#> parameters: 3
#> components: 2
#> original draws: 2000
#> mclust model: EII
#> BIC: -17298.09
new_samples <- sample_posterior(comp, n_draws = 500)
dim(new_samples)
#> [1] 500 3