| Title: | Fitting Shared Atoms Nested Models via MCMC or Variational Bayes |
|---|---|
| Description: | An efficient tool for fitting nested mixture models based on a shared set of atoms via Markov Chain Monte Carlo and variational inference algorithms. Specifically, the package implements the common atoms model (Denti et al., 2023), its finite version (similar to D'Angelo et al., 2023), and a hybrid finite-infinite model (D'Angelo and Denti, 2024). All models implement univariate nested mixtures with Gaussian kernels equipped with a normal-inverse gamma prior distribution on the parameters. Additional functions are provided to help analyze the results of the fitting procedure. References: Denti, Camerlenghi, Guindani, Mira (2023) <doi:10.1080/01621459.2021.1933499>, D’Angelo, Canale, Yu, Guindani (2023) <doi:10.1111/biom.13626>, D’Angelo, Denti (2024) <doi:10.1214/24-BA1458>. |
| Authors: | Francesco Denti [aut, cre, cph] (ORCID: <https://orcid.org/0000-0001-5034-7414>), Laura D'Angelo [aut] (ORCID: <https://orcid.org/0000-0003-2978-4702>) |
| Maintainer: | Francesco Denti <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.0.4 |
| Built: | 2026-06-06 15:13:39 UTC |
| Source: | https://github.com/fradenti/sanba |
Computes posterior predictive density estimates for a selected group from a fitted SAN model estimated via MCMC. For each retained MCMC iteration, the function evaluates the corresponding mixture density on a user-defined grid and stores the resulting density curves.
The returned object can be visualized with
plot.SANmcmc_postdens, which displays the collection of
posterior density curves together with pointwise posterior summaries.
compute_postdens( object, group_ind = 1, mcmc_considered = 500, lim = 1, length_yseq = 500 )compute_postdens( object, group_ind = 1, mcmc_considered = 500, lim = 1, length_yseq = 500 )
object |
An object of class |
group_ind |
Integer specifying the group for which posterior densities should be computed. |
mcmc_considered |
Number of MCMC iterations (counting backward from the last saved iteration) used to compute posterior density samples. |
lim |
Non-negative numeric value controlling the extension of the
evaluation grid beyond the observed data range. The density is evaluated
on
|
length_yseq |
Number of grid points used to evaluate the density. |
An object of class SANmcmc_postdens, containing:
A matrix of dimension length_yseq x mcmc_considered, where each
column contains the posterior predictive density evaluated on the grid
for one MCMC iteration.
Numeric vector containing the evaluation grid.
Two-column matrix containing the observations and corresponding group labels for the selected group.
# Generate example data set.seed(123) y <- c(rnorm(100), rnorm(100, 5)) g <- rep(1:2, each = 100) # Fit fiSAN via MCMC est <- fit_fiSAN(y, g, est_method = "MCMC") # Compute posterior density samples for group 1 postdens <- compute_postdens( est, group_ind = 1, mcmc_considered = 50 ) # Plot posterior density summaries plot(postdens)# Generate example data set.seed(123) y <- c(rnorm(100), rnorm(100, 5)) g <- rep(1:2, each = 100) # Fit fiSAN via MCMC est <- fit_fiSAN(y, g, est_method = "MCMC") # Compute posterior density samples for group 1 postdens <- compute_postdens( est, group_ind = 1, mcmc_considered = 50 ) # Plot posterior density summaries plot(postdens)
The function computes and plots the posterior similarity matrix (PSM), either for the whole dataset, or separately for each group.
The function takes as input an object from fit_CAM, fit_fiSAN, or fit_fSAN,
used with the est_method = "MCMC" argument.
compute_psm( object, distributional = FALSE, group_specific = FALSE, plot = TRUE, ncores = 0 )compute_psm( object, distributional = FALSE, group_specific = FALSE, plot = TRUE, ncores = 0 )
object |
An object of class |
distributional |
Logical (default |
group_specific |
Logical (default |
plot |
Logical (default |
ncores |
A parameter to pass to the |
The function compute_psm returns and plots the posterior similarity matrix.
When distributional = FALSE, if group_specific = FALSE, the output is a matrix of dimension N x N;
if group_specific = TRUE, the output is a list on length J (the number of groups), where each entry contains a matrix of dimension Nj x Nj.
If distributional = TRUE, the output is a matrix of dimension J x J.
# Generate example data set.seed(123) y <- c(rnorm(100),rnorm(100,5)) g <- rep(1:2,rep(100,2)) plot(y,col=g) # Fitting fiSAN via MCMC est <- fit_fiSAN(y, g, est_method = "MCMC") est # Estimate PSM psm_overall <- compute_psm(est) # Estimate distributional PSM psm_distrib <- compute_psm(est, distributional = TRUE)# Generate example data set.seed(123) y <- c(rnorm(100),rnorm(100,5)) g <- rep(1:2,rep(100,2)) plot(y,col=g) # Fitting fiSAN via MCMC est <- fit_fiSAN(y, g, est_method = "MCMC") est # Estimate PSM psm_overall <- compute_psm(est) # Estimate distributional PSM psm_distrib <- compute_psm(est, distributional = TRUE)
The function computes the posterior means of the atoms and weights characterizing the discrete mixing distributions.
The function takes as input an object from fit_CAM, fit_fiSAN,
or fit_fSAN, used with the est_method = "VI" argument, and returns an object of class SANvi_G.
estimate_G(object) ## S3 method for class 'SANvi_G' plot(x, DC_num = NULL, lim = 2, ...) ## S3 method for class 'SANvi_G' summary(object, thr = 0.01, ...) ## S3 method for class 'SANvi_G' print(x, thr = 0.01, ...)estimate_G(object) ## S3 method for class 'SANvi_G' plot(x, DC_num = NULL, lim = 2, ...) ## S3 method for class 'SANvi_G' summary(object, thr = 0.01, ...) ## S3 method for class 'SANvi_G' print(x, thr = 0.01, ...)
object |
an object of class |
x |
an object of class |
DC_num |
an integer or a vector of integers indicating which distributional clusters to plot. |
lim |
optional value for the |
... |
ignored. |
thr |
argument for the |
The function estimate_G returns an object of class SANvi_G, which is a matrix comprising the posterior means,
variances, and weights of each estimated DC (one mixture component for each row).
# Generate example data set.seed(123) y <- c(rnorm(100),rnorm(100,5)) g <- rep(1:2,rep(100,2)) plot(y,col=g) # Fitting fiSAN via variational inference est <- fit_fiSAN(y,g,vi_param= list(n_runs = 10)) est summary(est) # Estimate posterior atoms and weights G <- estimate_G(est) summary(G)# Generate example data set.seed(123) y <- c(rnorm(100),rnorm(100,5)) g <- rep(1:2,rep(100,2)) plot(y,col=g) # Fitting fiSAN via variational inference est <- fit_fiSAN(y,g,vi_param= list(n_runs = 10)) est summary(est) # Estimate posterior atoms and weights G <- estimate_G(est) summary(G)
Given the output of a sanba model-fitting function, this method estimates both the observational and distributional partitions.
For MCMC objects, it computes a point estimate using salso::salso();
for Variational Inference (VI) objects, the cluster allocation is determined by the label with the highest estimated variational probability.
estimate_partition(object, ...) ## S3 method for class 'SANvi' estimate_partition(object, ordered = TRUE, ...) ## S3 method for class 'SANmcmc' estimate_partition(object, ordered = TRUE, add_burnin = 0, ncores = 0, ...) ## S3 method for class 'partition_mcmc' summary(object, ...) ## S3 method for class 'partition_vi' summary(object, ...) ## S3 method for class 'partition_mcmc' print(x, ...) ## S3 method for class 'partition_vi' print(x, ...) ## S3 method for class 'partition_mcmc' plot( x, DC_num = NULL, type = c("ecdf", "boxplot", "scatter"), alt_palette = FALSE, ... ) ## S3 method for class 'partition_vi' plot( x, DC_num = NULL, type = c("ecdf", "boxplot", "scatter"), alt_palette = FALSE, ... )estimate_partition(object, ...) ## S3 method for class 'SANvi' estimate_partition(object, ordered = TRUE, ...) ## S3 method for class 'SANmcmc' estimate_partition(object, ordered = TRUE, add_burnin = 0, ncores = 0, ...) ## S3 method for class 'partition_mcmc' summary(object, ...) ## S3 method for class 'partition_vi' summary(object, ...) ## S3 method for class 'partition_mcmc' print(x, ...) ## S3 method for class 'partition_vi' print(x, ...) ## S3 method for class 'partition_mcmc' plot( x, DC_num = NULL, type = c("ecdf", "boxplot", "scatter"), alt_palette = FALSE, ... ) ## S3 method for class 'partition_vi' plot( x, DC_num = NULL, type = c("ecdf", "boxplot", "scatter"), alt_palette = FALSE, ... )
object |
Object of class |
... |
Additional graphical parameters to be passed to the |
ordered |
Logical, if |
add_burnin |
Integer (default = 0). Number of observations to discard as additional burn-in (only for |
ncores |
A parameter to pass to the |
x |
The result of a call to |
DC_num |
An integer or a vector of integers indicating which distributional clusters to plot. |
type |
What type of plot should be drawn. Available types are |
alt_palette |
Logical, the color palette to be used. Default is |
A list of class partition_vi or partition_mcmc containing
obs_level: a data frame containing the data values, their group indexes, and the observational and distributional clustering assignments for each observation.
dis_level: a vector with the distributional clustering assignment for each unit.
set.seed(123) y <- c(rnorm(40,0,0.3), rnorm(20,5,0.3)) g <- c(rep(1:6, each = 10)) out <- fit_fSAN(y = y, group = g, "VI", vi_param = list(n_runs = 10)) plot(out) clust <- estimate_partition(out) summary(clust) plot(clust, lwd = 2, alt_palette = TRUE) plot(clust, type = "scatter", alt_palette = FALSE, cex = 2) set.seed(123) y <- c(rnorm(40,0,0.3), rnorm(20,5,0.3)) g <- c(rep(1:6, each = 10)) out <- fit_fSAN(y = y, group = g, "MCMC", mcmc_param=list(nrep=500,burn=200)) plot(out) clust <- estimate_partition(out) summary(clust) plot(clust, lwd = 2) plot(clust, type = "boxplot", alt_palette = TRUE) plot(clust, type = "scatter", alt_palette = TRUE, cex = 2, pch = 4)set.seed(123) y <- c(rnorm(40,0,0.3), rnorm(20,5,0.3)) g <- c(rep(1:6, each = 10)) out <- fit_fSAN(y = y, group = g, "VI", vi_param = list(n_runs = 10)) plot(out) clust <- estimate_partition(out) summary(clust) plot(clust, lwd = 2, alt_palette = TRUE) plot(clust, type = "scatter", alt_palette = FALSE, cex = 2) set.seed(123) y <- c(rnorm(40,0,0.3), rnorm(20,5,0.3)) g <- c(rep(1:6, each = 10)) out <- fit_fSAN(y = y, group = g, "MCMC", mcmc_param=list(nrep=500,burn=200)) plot(out) clust <- estimate_partition(out) summary(clust) plot(clust, lwd = 2) plot(clust, type = "boxplot", alt_palette = TRUE) plot(clust, type = "scatter", alt_palette = TRUE, cex = 2, pch = 4)
fit_CAM fits the common atoms mixture model (CAM) of Denti et al. (2023) with Gaussian kernels and normal-inverse gamma priors on the unknown means and variances.
The function returns an object of class SANmcmc or SANvi depending on the chosen computational approach (MCMC or VI).
fit_CAM(y, group, est_method = c("VI", "MCMC"), prior_param = list(), vi_param = list(), mcmc_param = list())fit_CAM(y, group, est_method = c("VI", "MCMC"), prior_param = list(), vi_param = list(), mcmc_param = list())
y |
Numerical vector of observations (required). |
group |
Numerical vector of the same length of |
est_method |
Character, specifying the preferred estimation method. It can be either |
prior_param |
A list containing
|
vi_param |
A list of variational inference-specific settings containing
|
mcmc_param |
A list of MCMC inference-specific settings containing
|
The common atoms mixture model is used to perform inference in nested settings, where the data are organized into groups.
The data should be continuous observations , where each
contains the observations from group , for .
The function takes as input the data as a numeric vector y in this concatenated form. Hence, y should be a vector of length
. The group parameter is a numeric vector of the same size as y, indicating the group membership for each
individual observation.
Notice that with this specification, the observations in the same group need not be contiguous as long as the correspondence between the variables
y and group is maintained.
Model
The data are modeled using a Gaussian likelihood, where both the mean and the variance are observational cluster-specific, i.e.,
where is the observational cluster indicator of observation in group .
The prior on the model parameters is a normal-inverse gamma distribution ,
i.e., , (shape, rate).
Clustering
The model clusters both observations and groups.
The clustering of groups (distributional clustering) is provided by the allocation variables , with
The distribution of the probabilities is ,
where GEM is the Griffiths-Engen-McCloskey distribution of parameter ,
which characterizes the stick-breaking construction of the DP (Sethuraman, 1994).
The clustering of observations (observational clustering) is provided by the allocation variables , with
The distribution of the probabilities is for all
fit_CAM returns a list of class SANvi, if est_method = "VI", or SANmcmc, if est_method = "MCMC". The list contains the following elements:
modelName of the fitted model.
paramsList containing the data and the parameters used in the simulation. Details below.
simList containing the optimized variational parameters or the simulated values. Details below.
timeTotal computation time.
Data and parameters:
params is a list with the following components:
y, group, Nj, J: Data, group labels, group frequencies, and number of groups.
K, L: Number of distributional and observational mixture components.
m0, tau0, lambda0, gamma0: Model hyperparameters.
(hyp_alpha1, hyp_alpha2) or alpha: hyperparameters on (if random); or provided value for (if fixed).
(hyp_beta1, hyp_beta2) or beta: hyperparameters on (if random); or provided value for (if fixed).
seed: The random seed adopted to replicate the run.
epsilon, n_runs: The threshold controlling the convergence criterion and the number of
starting points considered.
nrep, burnin: If est_method = "MCMC", the number of total MCMC iterations, and the number of discarded ones.
Simulated values: depending on the algorithm, it returns a list with the optimized variational parameters or a list with the chains of the simulated values.
Variational inference: sim is a list with the following components:
theta_l: Matrix of size (maxL, 4).
Each row is a posterior variational estimate of the four normal-inverse gamma hyperparameters.
XI: A list of length J. Each element is a matrix of size (Nj, maxL) containing the
posterior variational probability of assignment of the i-th observation in the j-th group to the l-th OC,
i.e., .
RHO: Matrix of size (J, maxK).
Each row is a posterior variational probability of assignment of the j-th group to the k-th DC, i.e., .
a_tilde_k, b_tilde_k: Vector of updated variational parameters of the beta distributions governing the distributional stick-breaking process.
a_bar_lk, b_bar_lk: Matrix of updated variational parameters of the beta distributions governing the observational stick-breaking
process (arranged by column).
conc_hyper: If the concentration parameters are chosen to be random, a vector with the four updated hyperparameters.
Elbo_val: Vector containing the values of the ELBO.
MCMC inference: sim is a list with the following components:
mu: Matrix of size (nrep, maxL).
Each row is a posterior sample of the mean parameter for each observational cluster .
sigma2: Matrix of size (nrep, maxL).
Each row is a posterior sample of the variance parameter for each observational cluster .
obs_cluster: Matrix of size (nrep, n), with n = length(y).
Each row is a posterior sample of the observational cluster allocation variables .
distr_cluster: Matrix of size (nrep, J), with J = length(unique(group))
Each row is a posterior sample of the distributional cluster allocation variables .
pi: Matrix of size (nrep, maxK).
Each row is a posterior sample of the distributional cluster probabilities .
omega: 3-d array of size (maxL, maxK, nrep).
Each slice is a posterior sample of the observational cluster probabilities.
In each slice, each column is a vector (of length maxL) observational cluster probabilities
for distributional cluster .
alpha: Vector of length nrep of posterior samples of the parameter .
beta: Vector of length nrep of posterior samples of the parameter .
maxK: Vector of length nrep of the number of distributional DP components used by the slice sampler.
maxL: Vector of length nrep of the number of observational DP components used by the slice sampler.
Denti, F., Camerlenghi, F., Guindani, M., and Mira, A. (2023). A Common Atoms Model for the Bayesian Nonparametric Analysis of Nested Data. Journal of the American Statistical Association, 118(541), 405-416. DOI: 10.1080/01621459.2021.1933499
Sethuraman, A.J. (1994). A Constructive Definition of Dirichlet Priors, Statistica Sinica, 4, 639–650.
set.seed(123) y <- c(rnorm(60), rnorm(40, 5)) g <- rep(1:2, rep(50, 2)) out_vi <- fit_CAM(y, group = g, est_method = "VI", vi_param = list(n_runs = 1, epsilon = .01 )) out_vi out_mcmc <- fit_CAM(y = y, group = g, est_method = "MCMC", mcmc_param = list(nrep = 50, burn = 20)) out_mcmcset.seed(123) y <- c(rnorm(60), rnorm(40, 5)) g <- rep(1:2, rep(50, 2)) out_vi <- fit_CAM(y, group = g, est_method = "VI", vi_param = list(n_runs = 1, epsilon = .01 )) out_vi out_mcmc <- fit_CAM(y = y, group = g, est_method = "MCMC", mcmc_param = list(nrep = 50, burn = 20)) out_mcmc
fit_fiSAN fits the finite-infinite shared atoms nested (fiSAN) mixture model with Gaussian kernels and normal-inverse gamma priors on the unknown means and variances.
The function returns an object of class SANmcmc or SANvi depending on the chosen computational approach (MCMC or VI).
fit_fiSAN(y, group, est_method = c("VI", "MCMC"), prior_param = list(), vi_param = list(), mcmc_param = list())fit_fiSAN(y, group, est_method = c("VI", "MCMC"), prior_param = list(), vi_param = list(), mcmc_param = list())
y |
Numerical vector of observations (required). |
group |
Numerical vector of the same length of |
est_method |
Character, specifying the preferred estimation method. It can be either |
prior_param |
A list containing:
|
vi_param |
A list of variational inference-specific settings containing:
|
mcmc_param |
A list of MCMC inference-specific settings containing:
|
Data structure
The finite-infinite common atoms mixture model is used to perform inference in nested settings, where the data are organized into groups.
The data should be continuous observations , where each
contains the observations from group , for .
The function takes as input the data as a numeric vector y in this concatenated form. Hence, y should be a vector of length
. The group parameter is a numeric vector of the same size as y, indicating the group membership for each
individual observation.
Notice that with this specification, the observations in the same group need not be contiguous as long as the correspondence between the variables
y and group is maintained.
Model
The data are modeled using a Gaussian likelihood, where both the mean and the variance are observational-cluster-specific:
where is the observational cluster indicator of observation in group .
The prior on the model parameters is a normal-inverse gamma distribution ,
i.e., , (shape, rate).
Clustering
The model clusters both observations and groups.
The clustering of groups (distributional clustering) is provided by the allocation variables , with:
The distribution of the probabilities is ,
where GEM is the Griffiths-Engen-McCloskey distribution of parameter ,
which characterizes the stick-breaking construction of the DP (Sethuraman, 1994).
The clustering of observations (observational clustering) is provided by the allocation variables , with:
The distribution of the probabilities is for all .
Here, the dimension is fixed.
fit_fiSAN returns a list of class SANvi, if est_method = "VI", or SANmcmc, if est_method = "MCMC". The list contains the following elements:
modelName of the fitted model.
paramsList containing the data and the parameters used in the simulation. Details below.
simList containing the optimized variational parameters or the simulated values. Details below.
timeTotal computation time.
Data and parameters:
params is a list with the following components:
y, group, Nj, J: Data, group labels, group frequencies, and number of groups.
K, L: Number of distributional and observational mixture components.
m0, tau0, lambda0, gamma0: Model hyperparameters.
(hyp_alpha1, hyp_alpha2) or alpha: Hyperparameters on (if random);
or provided value for (if fixed).
b_dirichlet: Provided value for .
seed: The random seed adopted to replicate the run.
epsilon, n_runs: If est_method = "VI", the threshold controlling the convergence criterion and the number of
starting points considered.
nrep, burnin: If est_method = "MCMC", the number of total MCMC iterations, and the number of discarded ones.
Simulated values: Depending on the algorithm, it returns a list with the optimized variational parameters or a list with the chains of the simulated values.
Variational inference: sim is a list with the following components:
theta_l: Matrix of size (maxL, 4). Each row is a posterior variational estimate of the four normal-inverse gamma hyperparameters.
XI: A list of length J. Each element is a matrix of size (Nj, maxL), the posterior variational assignment probabilities .
RHO: Matrix of size (J, maxK), with the posterior variational assignment probabilities .
a_tilde_k, b_tilde_k: Vector of updated variational parameters of the beta distributions governing the distributional stick-breaking process.
conc_hyper: If the concentration parameter is random, this contains its updated hyperparameters.
b_dirichlet_lk: Matrix of updated variational parameters of the Dirichlet distributions governing observational clustering.
Elbo_val: Vector containing the values of the ELBO.
MCMC inference: sim is a list with the following components:
mu: Matrix of size (nrep, maxL) with samples of the observational cluster means.
sigma2: Matrix of size (nrep, maxL) with samples of the observational cluster variances.
obs_cluster: Matrix of size (nrep, n) with posterior samples of the observational cluster allocations.
distr_cluster: Matrix of size (nrep, J) with posterior samples of the distributional cluster allocations.
pi: Matrix of size (nrep, maxK) with posterior samples of the distributional cluster weights.
omega: Array of size (maxL, maxK, nrep) with observational cluster weights.
alpha: Vector of length nrep with posterior samples of .
maxK: Vector of length nrep with number of active distributional components.
set.seed(123) y <- c(rnorm(60), rnorm(40, 5)) g <- rep(1:2, rep(50, 2)) plot(density(y[g==1]), xlim = c(-5,10), main = "Group-specific density") lines(density(y[g==2]), col = 2) out_vi <- fit_fiSAN(y, group = g, est_method = "VI", vi_param = list(n_runs = 1)) out_vi out_mcmc <- fit_fiSAN(y = y, group = g, est_method = "MCMC") out_mcmcset.seed(123) y <- c(rnorm(60), rnorm(40, 5)) g <- rep(1:2, rep(50, 2)) plot(density(y[g==1]), xlim = c(-5,10), main = "Group-specific density") lines(density(y[g==2]), col = 2) out_vi <- fit_fiSAN(y, group = g, est_method = "VI", vi_param = list(n_runs = 1)) out_vi out_mcmc <- fit_fiSAN(y = y, group = g, est_method = "MCMC") out_mcmc
fit_fSAN fits the finite shared atoms nested (fSAN) mixture model with Gaussian kernels and normal-inverse gamma priors on the unknown means and variances.
The function returns an object of class SANmcmc or SANvi depending on the chosen computational approach (MCMC or VI).
fit_fSAN(y, group, est_method = c("VI", "MCMC"), prior_param = list(), vi_param = list(), mcmc_param = list())fit_fSAN(y, group, est_method = c("VI", "MCMC"), prior_param = list(), vi_param = list(), mcmc_param = list())
y |
Numerical vector of observations (required). |
group |
Numerical vector of the same length of |
est_method |
Character, specifying the preferred estimation method. It can be either |
prior_param |
A list containing
|
vi_param |
A list of variational inference-specific settings, containing
|
mcmc_param |
A list of MCMC inference-specific settings, containing
|
Data structure
The finite common atoms mixture model is used to perform inference in nested settings, where the data are organized into groups.
The data should be continuous observations , where each
contains the observations from group , for .
The function takes as input the data as a numeric vector y in this concatenated form. Hence y should be a vector of length
. The group parameter is a numeric vector of the same size as y indicating the group membership for each
individual observation.
Notice that with this specification the observations in the same group need not be contiguous as long as the correspondence between the variables
y and group is maintained.
Model
The data are modeled using a Gaussian likelihood, where both the mean and the variance are observational-cluster-specific, i.e.,
where is the observational cluster indicator of observation in group .
The prior on the model parameters is a normal-inverse gamma distribution ,
i.e., , (shape, rate).
Clustering
The model performs a clustering of both observations and groups.
The clustering of groups (distributional clustering) is provided by the allocation variables , with
The distribution of the probabilities is .
Here, the dimension is fixed.
The clustering of observations (observational clustering) is provided by the allocation variables , with
The distribution of the probabilities is for all .
Here, the dimension is fixed.
fit_fSAN returns a list of class SANvi, if est_method = "VI", or SANmcmc, if est_method = "MCMC". The list contains the following elements:
model
Name of the fitted model.
params
List containing the data and the parameters used in the simulation. Details below.
sim
List containing the optimized variational parameters or the simulated values. Details below.
time
Total computation time.
Data and parameters:
params is a list with the following components:
y, group, Nj, J: Data, group labels, group frequencies, and number of groups.
K, L: Number of distributional and observational mixture components.
m0, tau0, lambda0, gamma0: Model hyperparameters.
a_dirichlet: Provided value for .
b_dirichlet: Provided value for .
seed: The random seed adopted to replicate the run.
epsilon, n_runs: The threshold controlling the convergence criterion and the number of starting points considered
nrep, burnin: If est_method = "MCMC", the number of total MCMC iterations, and the number of discarded ones.
Simulated values: depending on the algorithm, it returns a list with the optimized variational parameters or a list with the chains of the simulated values.
Variational inference: sim is a list with the following components:
theta_l: Matrix of size (maxL, 4).
Each row is a posterior variational estimate of the four normal-inverse gamma hyperparameters.
XI : A list of length J. Each element is a matrix of size (Nj, maxL)
posterior variational probability of assignment of assignment of the i-th observation in the j-th group to the l-th OC,
i.e., .
RHO: Matrix of size (J, maxK).
Each row is a posterior variational probability of assignment of the j-th group to the k-th DC, i.e., .
a_dirichlet_k: Vector of updated variational parameters of the Dirichlet distribution governing the distributional clustering.
b_dirichlet_lk: Matrix of updated variational parameters of the Dirichlet distributions governing the observational clustering (arranged by column).
Elbo_val: Vector containing the values of the ELBO.
MCMC inference: sim is a list with the following components:
mu: Matrix of size (nrep, maxL).
Each row is a posterior sample of the mean parameter of each observational cluster .
sigma2: Matrix of size (nrep, maxL).
Each row is a posterior sample of the variance parameter of each observational cluster .
obs_cluster: Matrix of size (nrep, n), with n = length(y).
Each row is a posterior sample of the observational cluster allocation variables .
distr_cluster: Matrix of size (nrep, J), with J = length(unique(group)).
Each row is a posterior sample of the distributional cluster allocation variables .
pi: Matrix of size (nrep, maxK).
Each row is a posterior sample of the distributional cluster probabilities .
omega: 3-d array of size (maxL, maxK, nrep).
Each slice is a posterior sample of the observational cluster probabilities.
In each slice, each column is a vector (of length maxL) observational cluster probabilities
for distributional cluster .
set.seed(123) y <- c(rnorm(60), rnorm(40, 5)) g <- rep(1:2, rep(50, 2)) plot(density(y[g==1]), xlim = c(-5,10), main = "Group-specific density") lines(density(y[g==2]), col = 2) out_vi <- fit_fSAN(y, group = g, est_method = "VI", vi_param = list(n_runs = 1)) out_vi out_mcmc <- fit_fSAN(y = y, group = g, est_method = "MCMC", mcmc_param = list(nrep = 100, burn= 50)) out_mcmcset.seed(123) y <- c(rnorm(60), rnorm(40, 5)) g <- rep(1:2, rep(50, 2)) plot(density(y[g==1]), xlim = c(-5,10), main = "Group-specific density") lines(density(y[g==2]), col = 2) out_vi <- fit_fSAN(y, group = g, est_method = "VI", vi_param = list(n_runs = 1)) out_vi out_mcmc <- fit_fSAN(y = y, group = g, est_method = "MCMC", mcmc_param = list(nrep = 100, burn= 50)) out_mcmc
The functions get_model, get_time, get_params, get_sim, and get_seed_best_run provide convenient access to specific components of model output objects of class SANmcmc (fitted via MCMC) or SANvi (fitted via variational inference).
Specifically:
get_model: returns a character string specifying the model type used;
get_time: returns the total runtime of the estimation procedure;
get_params: returns a list containing data and prior hyperparameters;
get_sim: returns the fitted quantities, such as posterior draws (MCMC) or variational estimates (VI);
get_seed_best_run: (only for SANvi) returns the random seed associated with the run that achieved the highest ELBO.
get_model(object, ...) ## S3 method for class 'SANvi' get_model(object, ...) ## S3 method for class 'SANmcmc' get_model(object, ...) get_time(object, ...) ## S3 method for class 'SANvi' get_time(object, ...) ## S3 method for class 'SANmcmc' get_time(object, ...) get_params(object, ...) ## S3 method for class 'SANvi' get_params(object, ...) ## S3 method for class 'SANmcmc' get_params(object, ...) get_sim(object, ...) ## S3 method for class 'SANvi' get_sim(object, ...) ## S3 method for class 'SANmcmc' get_sim(object, ...) get_seed_best_run(object, ...) ## S3 method for class 'SANvi' get_seed_best_run(object, ...)get_model(object, ...) ## S3 method for class 'SANvi' get_model(object, ...) ## S3 method for class 'SANmcmc' get_model(object, ...) get_time(object, ...) ## S3 method for class 'SANvi' get_time(object, ...) ## S3 method for class 'SANmcmc' get_time(object, ...) get_params(object, ...) ## S3 method for class 'SANvi' get_params(object, ...) ## S3 method for class 'SANmcmc' get_params(object, ...) get_sim(object, ...) ## S3 method for class 'SANvi' get_sim(object, ...) ## S3 method for class 'SANmcmc' get_sim(object, ...) get_seed_best_run(object, ...) ## S3 method for class 'SANvi' get_seed_best_run(object, ...)
object |
An object of class |
... |
ignored. |
The requested component from the fitted model object. See the function descriptions above for details.
set.seed(123) y <- c(rnorm(40, 0, 0.3), rnorm(20, 5, 0.3)) g <- c(rep(1:6, each = 10)) out <- fit_fSAN(y = y, group = g, est_method = "MCMC", mcmc_param = list(nrep = 500, burn = 200)) get_model(out) get_time(out) hp <- get_params(out) sims <- get_sim(out)set.seed(123) y <- c(rnorm(40, 0, 0.3), rnorm(20, 5, 0.3)) g <- c(rep(1:6, each = 10)) out <- fit_fSAN(y = y, group = g, est_method = "MCMC", mcmc_param = list(nrep = 500, burn = 200)) get_model(out) get_time(out) hp <- get_params(out) sims <- get_sim(out)
Computes the estimated number of observational clusters (OC) and distributional clusters (DC) from a fitted SAN model object.
For variational inference (SANvi objects), the function returns point estimates based on posterior mode assignments.
For MCMC-based inference (SANmcmc objects), it returns the mean, median, and variance of the number of clusters across posterior samples.
number_clusters(object, ...)number_clusters(object, ...)
object |
An object of class |
... |
ignored. |
A data frame reporting the estimated number of observational (OC) and distributional (DC) clusters.
For SANvi: a single-row data frame with the point estimates.
For SANmcmc: a three-row data frame with the mean, median, and variance across MCMC samples.
# Generate example data set.seed(123) y <- c(rnorm(60), rnorm(40, 5)) g <- rep(1:2, each = 50) plot(density(y[g == 1]), xlim = c(-5, 10), main = "Group-specific density") lines(density(y[g == 2]), col = 2) # Fit fiSAN via MCMC est_mcmc <- fit_fiSAN(y, g, est_method = "MCMC") number_clusters(est_mcmc) # Fit fiSAN via Variational Inference est_vi <- fit_fiSAN(y, g, est_method = "VI") number_clusters(est_vi)# Generate example data set.seed(123) y <- c(rnorm(60), rnorm(40, 5)) g <- rep(1:2, each = 50) plot(density(y[g == 1]), xlim = c(-5, 10), main = "Group-specific density") lines(density(y[g == 2]), col = 2) # Fit fiSAN via MCMC est_mcmc <- fit_fiSAN(y, g, est_method = "MCMC") number_clusters(est_mcmc) # Fit fiSAN via Variational Inference est_vi <- fit_fiSAN(y, g, est_method = "VI") number_clusters(est_vi)
Produces visualizations of the posterior cluster allocation probabilities from a SAN model fitted via variational inference.
The function supports plotting either the observation-level (OC) or distribution-level (DC) allocation probabilities, depending on the argument distributional.
This function applies to objects returned by fit_CAM, fit_fiSAN, or fit_fSAN when used with est_method = "VI".
plot_vi_allocation_prob(object, distributional = FALSE, ...)plot_vi_allocation_prob(object, distributional = FALSE, ...)
object |
An object of class |
distributional |
Logical (default |
... |
Additional graphical parameters passed to the underlying |
The function plots the variational cluster allocation probabilities.
# Generate example data set.seed(123) y <- c(rnorm(60), rnorm(40, 5)) g <- rep(1:2, each = 50) # Fit fiSAN via VI est <- fit_fiSAN(y, g, est_method = "VI") # Plot observational cluster probabilities plot_vi_allocation_prob(est) # Plot distributional cluster probabilities plot_vi_allocation_prob(est, distributional = TRUE)# Generate example data set.seed(123) y <- c(rnorm(60), rnorm(40, 5)) g <- rep(1:2, each = 50) # Fit fiSAN via VI est <- fit_fiSAN(y, g, est_method = "VI") # Plot observational cluster probabilities plot_vi_allocation_prob(est) # Plot distributional cluster probabilities plot_vi_allocation_prob(est, distributional = TRUE)
Plot method for objects of class SANmcmc.
Check the convergence of the MCMC through visual inspection of the chains.
## S3 method for class 'SANmcmc' plot( x, param = c("mu", "sigma2", "pi", "num_clust", "alpha", "beta"), show_density = TRUE, add_burnin = 0, show_convergence = TRUE, trunc_plot = 2, ... )## S3 method for class 'SANmcmc' plot( x, param = c("mu", "sigma2", "pi", "num_clust", "alpha", "beta"), show_density = TRUE, add_burnin = 0, show_convergence = TRUE, trunc_plot = 2, ... )
x |
Object of class |
param |
String with the names of the parameters to check. It can be one of |
show_density |
Logical (default |
add_burnin |
Integer (default = 0). Additional number of observations to discard in the burn-in. |
show_convergence |
Logical (default |
trunc_plot |
Integer (default = 10). For multidimensional parameters, the maximum number of components to be plotted. |
... |
Ignored. |
The function displays the traceplots and posterior density estimates of the parameters sampled in the MCMC algorithm.
The function is not available for the observational weights .
set.seed(123) y <- c(rnorm(40,0,0.3), rnorm(20,5,0.3)) g <- c(rep(1,30), rep(2, 30)) out <- fit_fiSAN(y = y, group = g, "MCMC", mcmc_param = list(nrep = 500, burn = 200)) plot(out, param = "mu", trunc_plot = 2) plot(out, param = "sigma2", trunc_plot = 2) plot(out, param = "alpha", trunc_plot = 1) plot(out, param = "alpha", add_burnin = 100) plot(out, param = "pi", trunc_plot = 4, show_density = FALSE) out <- fit_CAM(y = y, group = g, "MCMC", mcmc_param = list(nrep = 500, burn = 200, seed= 1234)) plot(out, param = "mu", trunc_plot = 2) plot(out, param = "sigma2", trunc_plot = 2) plot(out, param = "alpha") plot(out, param = "pi", trunc_plot = 2) plot(out, param = "pi", trunc_plot = 5) plot(out, param = "num_clust", trunc_plot = 5) plot(out, param = "beta", trunc_plot = 2) out <- fit_fSAN(y = y, group = g, "MCMC", mcmc_param = list(nrep = 500, burn = 200)) plot(out, param = "mu", trunc_plot = 2) plot(out, param = "sigma2", trunc_plot = 2) plot(out, param = "pi", trunc_plot = 4, show_convergence = FALSE, show_density = FALSE)set.seed(123) y <- c(rnorm(40,0,0.3), rnorm(20,5,0.3)) g <- c(rep(1,30), rep(2, 30)) out <- fit_fiSAN(y = y, group = g, "MCMC", mcmc_param = list(nrep = 500, burn = 200)) plot(out, param = "mu", trunc_plot = 2) plot(out, param = "sigma2", trunc_plot = 2) plot(out, param = "alpha", trunc_plot = 1) plot(out, param = "alpha", add_burnin = 100) plot(out, param = "pi", trunc_plot = 4, show_density = FALSE) out <- fit_CAM(y = y, group = g, "MCMC", mcmc_param = list(nrep = 500, burn = 200, seed= 1234)) plot(out, param = "mu", trunc_plot = 2) plot(out, param = "sigma2", trunc_plot = 2) plot(out, param = "alpha") plot(out, param = "pi", trunc_plot = 2) plot(out, param = "pi", trunc_plot = 5) plot(out, param = "num_clust", trunc_plot = 5) plot(out, param = "beta", trunc_plot = 2) out <- fit_fSAN(y = y, group = g, "MCMC", mcmc_param = list(nrep = 500, burn = 200)) plot(out, param = "mu", trunc_plot = 2) plot(out, param = "sigma2", trunc_plot = 2) plot(out, param = "pi", trunc_plot = 4, show_convergence = FALSE, show_density = FALSE)
Visualizes the output of compute_postdens. The plot displays
all posterior density curves obtained from the selected MCMC iterations,
together with pointwise posterior summaries and the observed data values.
Specifically, the plot includes:
all sampled density curves (gray lines),
the posterior median density (black line),
the posterior mean density (red line),
pointwise credible interval bounds (blue lines),
observed data values shown as rug-like points on the horizontal axis.
## S3 method for class 'SANmcmc_postdens' plot(x, alpha = 0.025, ...)## S3 method for class 'SANmcmc_postdens' plot(x, alpha = 0.025, ...)
x |
An object of class |
alpha |
Significance level used to construct pointwise credible
intervals. The default |
... |
Additional graphical arguments passed to plotting methods. |
Invisibly returns NULL. The function is called for its side effect of
producing a plot.
Plot method for objects of class SANvi.
The function displays two graphs. The left plot shows the progression of all the ELBO values as a function of the iterations.
The right plots shows the ELBO increments between successive iterations of the best run on a log scale (note: increments should always be positive).
## S3 method for class 'SANvi' plot(x, ...)## S3 method for class 'SANvi' plot(x, ...)
x |
Object of class |
... |
Ignored. |
The function plots the path followed by the ELBO and its subsequent differences.
set.seed(123) y <- c(rnorm(200,0,0.3), rnorm(100,5,0.3)) g <- c(rep(1,150), rep(2, 150)) out <- fit_fSAN(y = y, group = g, "VI", vi_param = list(n_runs = 2)) plot(out)set.seed(123) y <- c(rnorm(200,0,0.3), rnorm(100,5,0.3)) g <- c(rep(1,150), rep(2, 150)) out <- fit_fSAN(y = y, group = g, "VI", vi_param = list(n_runs = 2)) plot(out)
Print method for objects of class SANmcmc.
## S3 method for class 'SANmcmc' print(x, ...)## S3 method for class 'SANmcmc' print(x, ...)
x |
Object of class |
... |
Ignored. |
The function prints a summary of the fitted model.
Print method for objects of class SANvi.
## S3 method for class 'SANvi' print(x, ...)## S3 method for class 'SANvi' print(x, ...)
x |
Object of class |
... |
Further arguments passed to or from other methods. |
The function prints a summary of the fitted model.
Summary method for objects of class SANmcmc.
## S3 method for class 'SANmcmc' summary(object, ...)## S3 method for class 'SANmcmc' summary(object, ...)
object |
of class |
... |
Ignored. |
The function prints a summary of the fitted model.
Summary method for objects of class SANvi.
## S3 method for class 'SANvi' summary(object, ...)## S3 method for class 'SANvi' summary(object, ...)
object |
Object of class |
... |
Further arguments passed to or from other methods. |
The function prints a summary of the fitted model.