Movatterモバイル変換


[0]ホーム

URL:


Type:Package
Title:Fitting Shared Atoms Nested Models via MCMC or Variational Bayes
Version:0.0.3
Maintainer:Francesco Denti <francescodenti.personal@gmail.com>
URL:https://github.com/fradenti/sanba
BugReports:https://github.com/fradenti/sanba/issues
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>.
License:MIT + file LICENSE
Encoding:UTF-8
RoxygenNote:7.3.3
Imports:Rcpp, matrixStats, salso, scales, RColorBrewer
LinkingTo:Rcpp, RcppArmadillo, RcppProgress
Language:en-US
Suggests:spelling
NeedsCompilation:yes
Packaged:2025-09-24 18:21:38 UTC; fradenti
Author:Francesco DentiORCID iD [aut, cre, cph], Laura D'AngeloORCID iD [aut]
Repository:CRAN
Date/Publication:2025-09-24 18:40:02 UTC

sanba: 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.

Author(s)

Maintainer: Francesco Dentifrancescodenti.personal@gmail.com (ORCID) [copyright holder]

Authors:

See Also

Useful links:


Compute and Plot the Posterior Similarity Matrix

Description

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 fromfit_CAM,fit_fiSAN, orfit_fSAN,used with theest_method = "MCMC" argument.

Usage

compute_psm(  object,  distributional = FALSE,  group_specific = FALSE,  plot = TRUE,  ncores = 0)

Arguments

object

An object of classSANmcmc.

distributional

Logical (defaultFALSE). IfFALSE, the function computes the posterior similarity matrix (PSM) for the observational partition (i.e., between individual observations). IfTRUE, it computes the PSM at the distributional level, that is, between groups.

group_specific

Logical (defaultFALSE). IfFALSE, the function considers the overall PSM.IfTRUE, the function considers the group-specific PSMs. This argument only affects the observational partition, i.e., whendistributional isFALSE.

plot

Logical (defaultTRUE). Whether to plot the PSM.

ncores

A parameter to pass to thesalso::salso() function. The number of CPU cores to use for parallel computing; a value of zero indicates the use of all cores of the system.

Value

The functioncompute_psm returns and plots the posterior similarity matrix.Whendistributional = FALSE, ifgroup_specific = FALSE, the output is a matrix of dimensionN x N;ifgroup_specific = TRUE, the output is a list on lengthJ (the number of groups), where each entry contains a matrix of dimensionNj x Nj.Ifdistributional = TRUE, the output is a matrix of dimensionJ x J.

Examples

# Generate example dataset.seed(123)y <- c(rnorm(100),rnorm(100,5))g <- rep(1:2,rep(100,2))plot(y,col=g)# Fitting fiSAN via MCMCest <- fit_fiSAN(y, g, est_method = "MCMC")est# Estimate PSMpsm_overall <- compute_psm(est)# Estimate distributional PSMpsm_distrib <- compute_psm(est, distributional = TRUE)

Estimate the Atoms and Weights of the Discrete Mixing Distributions

Description

The function computes the posterior means of the atoms and weights characterizing the discrete mixing distributions.The function takes as input an object fromfit_CAM,fit_fiSAN,orfit_fSAN, used with theest_method = "VI" argument, and returns an object of classSANvi_G.

Usage

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, ...)

Arguments

object

an object of classSANvi_G (usually, the result of a call toestimate_G).

x

an object of classSANvi_G (usually, the result of a call toestimate_G).

DC_num

an integer or a vector of integers indicating which distributional clusters to plot.

lim

optional value for theplot method to adjust the limits of the x-axis (the default is 2). The atoms are plotted on a rangegiven bymin(posterior means)-lim, max(posterior means)+lim.

...

ignored.

thr

argument for theprint method. It should be a small positive number,representing a threshold. If the posterior weight of a specific shared atom is below the threshold, theatom is not reported.

Value

The functionestimate_G returns an object of classSANvi_G, which is a matrix comprising the posterior means,variances, and weights of each estimated DC (one mixture component for each row).

Examples

# Generate example dataset.seed(123)y <- c(rnorm(100),rnorm(100,5))g <- rep(1:2,rep(100,2))plot(y,col=g)# Fitting fiSAN via variational inferenceest <- fit_fiSAN(y,g,vi_param= list(n_runs = 10))estsummary(est)# Estimate posterior atoms and weightsG <- estimate_G(est)summary(G)

Estimate the Observational and Distributional Partition

Description

Given the output of asanba model-fitting function, this method estimates both the observational and distributional partitions.For MCMC objects, it computes a point estimate usingsalso::salso();for Variational Inference (VI) objects, the cluster allocation is determined by the label with the highest estimated variational probability.

Usage

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,  ...)

Arguments

object

Object of classSANmcmc (usually, the result of a call tofit_fiSAN,fit_fSAN, orfit_CAM withest_method = "MCMC") orSANvi(the result of a call tofit_fiSAN,fit_fSAN, orfit_CAM withest_method = "VI").

...

Additional graphical parameters to be passed to theplot function.

ordered

Logical, ifTRUE (default), the function sorts the distributional cluster labels reflecting theincreasing values of medians of the data assigned to each DC. IfFALSE, no ordering is applied.

add_burnin

Integer (default = 0). Number of observations to discard as additional burn-in (only forSANmcmc objects).

ncores

A parameter to pass to thesalso::salso() function (only forSANmcmc objects). The number of CPU cores to use for parallel computing; a value of zero indicates the use of all cores on the system.

x

The result of a call toestimate_partition.

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"boxplot","ecdf", and"scatter".

alt_palette

Logical, the color palette to be used. Default isR base colors (alt_palette = FALSE).

Value

A list of classpartition_vi orpartition_mcmc containing

See Also

salso::salso()

Examples

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 the Common Atoms Mixture Model

Description

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 classSANmcmc orSANvi depending on the chosen computational approach (MCMC or VI).

Usage

fit_CAM(y, group, est_method = c("VI", "MCMC"),         prior_param = list(),         vi_param = list(),         mcmc_param = list())

Arguments

y

Numerical vector of observations (required).

group

Numerical vector of the same length ofy, indicating the group membership (required).

est_method

Character, specifying the preferred estimation method. It can be either"VI" or"MCMC".

prior_param

A list containing

m0, tau0, lambda0, gamma0

Hyperparameters on(\mu, \sigma^2) \sim NIG(m_0, \tau_0, \lambda_0,\gamma_0). The default is (0, 0.01, 3, 2).

hyp_alpha1, hyp_alpha2

If a random\alpha is used, (hyp_alpha1,hyp_alpha2) specify the hyperparameters. The default is (1,1). The prior is\alpha ~ Gamma(hyp_alpha1,hyp_alpha2).

alpha

Distributional DP parameter if fixed (optional). The distribution is\pi\sim GEM (\alpha).

hyp_beta1, hyp_beta2

If a random\beta is used, (hyp_beta1,hyp_beta2) specify the hyperparameters. The default is (1,1). The prior is\beta ~ Gamma(hyp_beta1,hyp_beta2).

beta

Observational DP parameter if fixed (optional). The distribution is\omega_k \sim GEM (\beta).

vi_param

A list of variational inference-specific settings containing

maxL, maxK

Integers, the upper bounds for the observational and distributional clusters to fit, respectively. The default is (50, 20).

epsilon

The threshold controlling the convergence criterion.

n_runs

Number of starting points considered for the estimation.

seed

Random seed to control the initialization.

maxSIM

The maximum number of CAVI iterations to perform.

warmstart

Logical, ifTRUE, the observational means of the cluster atoms are initialized with a k-means algorithm.

verbose

Logical, ifTRUE the iterations are printed.

mcmc_param

A list of MCMC inference-specific settings containing

nrep, burn

Integers, the number of total MCMC iterations, and the number of discarded iterations, respectively.

maxL, maxK

Integers, the upper bounds for the observational and distributional clusters to fit, respectively. The default is (50, 20).

seed

Random seed to control the initialization.

warmstart

Logical, ifTRUE, the observational means ofthe cluster atoms are initialized with a k-means algorithm. IfFALSE,the starting points can be passed through the parameters nclus_start, mu_start, sigma2_start, M_start, S_start, alpha_start, beta_start

verbose

Logical, ifTRUE the iterations are printed.

Details

The common atoms mixture model is used to perform inference in nested settings, where the data are organized intoJ groups.The data should be continuous observations(Y_1,\dots,Y_J), where eachY_j = (y_{1,j},\dots,y_{n_j,j})contains then_j observations from groupj, forj=1,\dots,J.The function takes as input the data as a numeric vectory in this concatenated form. Hence,y should be a vector of lengthn_1+\dots+n_J. Thegroup parameter is a numeric vector of the same size asy, indicating the group membership for eachindividual observation.Notice that with this specification, the observations in the same group need not be contiguous as long as the correspondence between the variablesy andgroup is maintained.

Model

The data are modeled using a Gaussian likelihood, where both the mean and the variance are observational cluster-specific, i.e.,

y_{i,j}\mid M_{i,j} = l \sim N(\mu_l,\sigma^2_l)

whereM_{i,j} \in \{1,2,\dots\} is the observational cluster indicator of observationi in groupj.The prior on the model parameters is a normal-inverse gamma distribution(\mu_l,\sigma^2_l)\sim NIG (m_0,\tau_0,\lambda_0,\gamma_0),i.e.,\mu_l\mid\sigma^2_l \sim N(m_0, \sigma^2_l / \tau_0),1/\sigma^2_l \sim Gamma(\lambda_0, \gamma_0) (shape, rate).

Clustering

The model clusters both observations and groups.The clustering of groups (distributional clustering) is provided by the allocation variablesS_j \in \{1,2,\dots\}, with

Pr(S_j = k \mid \dots ) = \pi_k \qquad \text{for } \: k = 1,2,\dots

The distribution of the probabilities is \{\pi_k\}_{k=1}^{\infty} \sim GEM(\alpha),where GEM is the Griffiths-Engen-McCloskey distribution of parameter\alpha,which characterizes the stick-breaking construction of the DP (Sethuraman, 1994).

The clustering of observations (observational clustering) is provided by the allocation variablesM_{i,j} \in \{1,2,\dots\}, with

Pr(M_{i,j} = l \mid S_j = k, \dots ) = \omega_{l,k} \qquad \text{for } \: k = 1,2,\dots \, ; \: l = 1,2,\dots

The distribution of the probabilities is \{\omega_{l,k}\}_{l=1}^{\infty} \sim GEM(\beta) for allk = 1,2,\dots

Value

fit_CAM returns a list of classSANvi, ifest_method = "VI", orSANmcmc, ifest_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:

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:

MCMC inference:sim is a list with the following components:

References

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.

Examples

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_viout_mcmc <- fit_CAM(y = y, group = g, est_method = "MCMC",                    mcmc_param = list(nrep = 50, burn = 20))out_mcmc

Fit the Finite Shared Atoms Mixture Model

Description

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 classSANmcmc orSANvi depending on the chosen computational approach (MCMC or VI).

Usage

fit_fSAN(y, group, est_method = c("VI", "MCMC"),         prior_param = list(),         vi_param = list(),         mcmc_param = list())

Arguments

y

Numerical vector of observations (required).

group

Numerical vector of the same length ofy, indicating the group membership (required).

est_method

Character, specifying the preferred estimation method. It can be either"VI" or"MCMC".

prior_param

A list containing

m0, tau0, lambda0, gamma0

Hyperparameters on(\mu, \sigma^2) \sim NIG(m_0, \tau_0, \lambda_0,\gamma_0). The default is (0, 0.01, 3, 2).

a_dirichlet

The hyperparameter of the symmetric distributional Dirichlet distribution. The default is 1/maxK.

b_dirichlet

The hyperparameter of the symmetric observational Dirichlet distribution. The default is 1/maxL.

vi_param

A list of variational inference-specific settings, containing

maxL, maxK

Integers, the upper bounds for the observational and distributional clusters to fit, respectively. The default is (50, 20).

epsilon

The threshold controlling the convergence criterion.

n_runs

Number of starting points considered for the estimation.

seed

Random seed to control the initialization.

maxSIM

The maximum number of CAVI iteration to perform.

warmstart

Logical, ifTRUE, the observational means of the cluster atoms are initialized with a k-means algorithm.

verbose

Logical, ifTRUE the iterations are printed.

mcmc_param

A list of MCMC inference-specific settings, containing

nrep, burn

Integers, the number of total MCMC iterations, and the number of discarded iterations, respectively.

maxL, maxK

Integers, the upper bounds for the observational and distributional clusters to fit, respectively. The default is (50, 20).

seed

Random seed to control the initialization.

warmstart

Logical, ifTRUE, the observational means of the cluster atoms are initialized with a k-means algorithm. IfFALSE, the starting points can be passed through the parameters nclus_start, mu_start, sigma2_start, M_start, S_start

verbose

Logical, ifTRUE the iterations are printed.

Details

Data structure

The finite common atoms mixture model is used to perform inference in nested settings, where the data are organized intoJ groups.The data should be continuous observations(Y_1,\dots,Y_J), where eachY_j = (y_{1,j},\dots,y_{n_j,j})contains then_j observations from groupj, forj=1,\dots,J.The function takes as input the data as a numeric vectory in this concatenated form. Hencey should be a vector of lengthn_1+\dots+n_J. Thegroup parameter is a numeric vector of the same size asy indicating the group membership for eachindividual observation.Notice that with this specification the observations in the same group need not be contiguous as long as the correspondence between the variablesy andgroup is maintained.

Model

The data are modeled using a Gaussian likelihood, where both the mean and the variance are observational-cluster-specific, i.e.,

y_{i,j}\mid M_{i,j} = l \sim N(\mu_l,\sigma^2_l)

whereM_{i,j} \in \{1,\dots,L \} is the observational cluster indicator of observationi in groupj.The prior on the model parameters is a normal-inverse gamma distribution(\mu_l,\sigma^2_l)\sim NIG (m_0,\tau_0,\lambda_0,\gamma_0),i.e.,\mu_l\mid\sigma^2_l \sim N(m_0, \sigma^2_l / \tau_0),1/\sigma^2_l \sim Gamma(\lambda_0, \gamma_0) (shape, rate).

Clustering

The model performs a clustering of both observations and groups.The clustering of groups (distributional clustering) is provided by the allocation variablesS_j \in \{1,\dots,K\}, with

Pr(S_j = k \mid \dots ) = \pi_k \qquad \text{for } \: k = 1,\dots,K.

The distribution of the probabilities is(\pi_1,\dots,\pi_{K})\sim Dirichlet_K(a,\dots,a).Here, the dimensionK is fixed.

The clustering of observations (observational clustering) is provided by the allocation variablesM_{i,j} \in \{1,\dots,L\}, with

Pr(M_{i,j} = l \mid S_j = k, \dots ) = \omega_{l,k} \qquad \text{for } \: k = 1,\dots,K \, ; \: l = 1,\dots,L.

The distribution of the probabilities is(\omega_{1,k},\dots,\omega_{L,k})\sim Dirichlet_L(b,\dots,b) for allk = 1,\dots,K.Here, the dimensionL is fixed.

Value

fit_fSAN returns a list of classSANvi, ifest_method = "VI", orSANmcmc, ifest_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:

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:

MCMC inference:sim is a list with the following components:

Examples

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_viout_mcmc <- fit_fSAN(y = y, group = g, est_method = "MCMC",                      mcmc_param = list(nrep = 100, burn= 50))out_mcmc

Fit the Finite-Infinite Shared Atoms Mixture Model

Description

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 classSANmcmc orSANvi depending on the chosen computational approach (MCMC or VI).

Usage

fit_fiSAN(y, group, est_method = c("VI", "MCMC"),         prior_param = list(),         vi_param = list(),         mcmc_param = list())

Arguments

y

Numerical vector of observations (required).

group

Numerical vector of the same length ofy, indicating the group membership (required).

est_method

Character, specifying the preferred estimation method. It can be either"VI" or"MCMC".

prior_param

A list containing:

m0, tau0, lambda0, gamma0

Hyperparameters on(\mu, \sigma^2) \sim NIG(m_0, \tau_0, \lambda_0,\gamma_0). The default is (0, 0.01, 3, 2).

hyp_alpha1, hyp_alpha2

If a random\alpha is used, (hyp_alpha1,hyp_alpha2) specify the hyperparameters. The default is (1,1). The prior is\alpha ~ Gamma(hyp_alpha1,hyp_alpha2).

alpha

Distributional DP parameter if fixed (optional). The distribution is\pi\sim \text{GEM} (\alpha).

b_dirichlet

The hyperparameter of the symmetric observational Dirichlet distribution. The default is 1/maxL.

vi_param

A list of variational inference-specific settings containing:

maxL, maxK

Integers, the upper bounds for the observational and distributional clusters to fit, respectively. The default is (50, 20).

epsilon

The threshold controlling the convergence criterion.

n_runs

Number of starting points considered for the estimation.

seed

Random seed to control the initialization.

maxSIM

The maximum number of CAVI iterations to perform.

warmstart

Logical, ifTRUE, the observational means of the cluster atoms are initialized with a k-means algorithm.

verbose

Logical, ifTRUE the iterations are printed.

mcmc_param

A list of MCMC inference-specific settings containing:

nrep, burn

Integers, the number of total MCMC iterations, and the number of discarded iterations, respectively.

maxL, maxK

Integers, the upper bounds for the observational and distributional clusters to fit, respectively. The default is (50, 20).

seed

Random seed to control the initialization.

warmstart

Logical, ifTRUE, the observational means of the cluster atoms are initialized with a k-means algorithm. IfFALSE, the starting points can be passed through the parametersnclus_start, mu_start, sigma2_start, M_start, S_start, alpha_start.

verbose

Logical, ifTRUE the iterations are printed.

Details

Data structure

The finite-infinite common atoms mixture model is used to perform inference in nested settings, where the data are organized intoJ groups.The data should be continuous observations(Y_1,\dots,Y_J), where eachY_j = (y_{1,j},\dots,y_{n_j,j})contains then_j observations from groupj, forj=1,\dots,J.The function takes as input the data as a numeric vectory in this concatenated form. Hence,y should be a vector of lengthn_1+\dots+n_J. Thegroup parameter is a numeric vector of the same size asy, indicating the group membership for eachindividual observation.Notice that with this specification, the observations in the same group need not be contiguous as long as the correspondence between the variablesy andgroup is maintained.

Model

The data are modeled using a Gaussian likelihood, where both the mean and the variance are observational-cluster-specific:

y_{i,j}\mid M_{i,j} = l \sim N(\mu_l,\sigma^2_l)

whereM_{i,j} \in \{1,\dots,L \} is the observational cluster indicator of observationi in groupj.The prior on the model parameters is a normal-inverse gamma distribution(\mu_l,\sigma^2_l)\sim NIG (m_0,\tau_0,\lambda_0,\gamma_0),i.e.,\mu_l\mid\sigma^2_l \sim N(m_0, \sigma^2_l / \tau_0),1/\sigma^2_l \sim \text{Gamma}(\lambda_0, \gamma_0) (shape, rate).

Clustering

The model clusters both observations and groups.The clustering of groups (distributional clustering) is provided by the allocation variablesS_j \in \{1,2,\dots\}, with:

Pr(S_j = k \mid \dots ) = \pi_k \qquad \text{for } \: k = 1,2,\dots

The distribution of the probabilities is \{\pi_k\}_{k=1}^{\infty} \sim GEM(\alpha),where GEM is the Griffiths-Engen-McCloskey distribution of parameter\alpha,which characterizes the stick-breaking construction of the DP (Sethuraman, 1994).

The clustering of observations (observational clustering) is provided by the allocation variablesM_{i,j} \in \{1,\dots,L\}, with:

Pr(M_{i,j} = l \mid S_j = k, \dots ) = \omega_{l,k} \qquad \text{for } \: k = 1,2,\dots \, ; \: l = 1,\dots,L.

The distribution of the probabilities is(\omega_{1,k},\dots,\omega_{L,k})\sim \text{Dirichlet}_L(b,\dots,b) for allk = 1,2,\dots.Here, the dimensionL is fixed.

Value

fit_fiSAN returns a list of classSANvi, ifest_method = "VI", orSANmcmc, ifest_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:

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:

MCMC inference:sim is a list with the following components:

Examples

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_viout_mcmc <- fit_fiSAN(y = y, group = g, est_method = "MCMC")out_mcmc

Accessor Functions for SAN Model Outputs

Description

The functionsget_model,get_time,get_params,get_sim, andget_seed_best_run provide convenient access to specific components of model output objects of classSANmcmc (fitted via MCMC) orSANvi (fitted via variational inference).

Specifically:

Usage

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, ...)

Arguments

object

An object of classSANmcmc orSANvi, as returned byfit_fSAN.

...

ignored.

Value

The requested component from the fitted model object. See the function descriptions above for details.

Examples

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)

Estimate the Number of Observational and Distributional Clusters

Description

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.

Usage

number_clusters(object, ...)

Arguments

object

An object of classSANvi orSANmcmc, typically, the output of a call tofit_fiSAN,fit_fSAN, orfit_CAM.

...

ignored.

Value

A data frame reporting the estimated number of observational (OC) and distributional (DC) clusters.

Examples

# Generate example dataset.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 MCMCest_mcmc <- fit_fiSAN(y, g, est_method = "MCMC")number_clusters(est_mcmc)# Fit fiSAN via Variational Inferenceest_vi <- fit_fiSAN(y, g, est_method = "VI")number_clusters(est_vi)

Visual Check of the Convergence of the MCMC Output

Description

Plot method for objects of classSANmcmc.Check the convergence of the MCMC through visual inspection of the chains.

Usage

## 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,  ...)

Arguments

x

Object of classSANmcmc (usually, the result of a call tofit_CAM,fit_fiSAN, orfit_fSAN, used with theest_method = "MCMC" argument).

param

String with the names of the parameters to check. It can be one of"mu","sigma2","pi","num_clust","alpha","beta".

show_density

Logical (defaultTRUE). Should a kernel estimate of the density be plotted?

add_burnin

Integer (default = 0). Additional number of observations to discard in the burn-in.

show_convergence

Logical (defaultTRUE). Should a superimposed red line of the cumulative mean be plotted?

trunc_plot

Integer (default = 10). For multidimensional parameters, the maximum number of components to be plotted.

...

Ignored.

Value

The function displays the traceplots and posterior density estimates of the parameters sampled in the MCMC algorithm.

Note

The function is not available for the observational weights\omega.

Examples

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)

Visual Check of the Convergence of the VI Output

Description

Plot method for objects of classSANvi.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).

Usage

## S3 method for class 'SANvi'plot(x, ...)

Arguments

x

Object of classSANvi (usually, the result of a call tofit_CAM,fit_fiSAN, orfit_fSAN, used with theest_method = "VI" argument).

...

Ignored.

Value

The function plots the path followed by the ELBO and its subsequent differences.

Examples

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)

Plot Variational Cluster Allocation Probabilities

Description

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 argumentdistributional.

This function applies to objects returned byfit_CAM,fit_fiSAN, orfit_fSAN when used withest_method = "VI".

Usage

plot_vi_allocation_prob(object, distributional = FALSE, ...)

Arguments

object

An object of classSANvi, representing a model fitted via variational inference.

distributional

Logical (defaultFALSE). IfFALSE, plots the allocation probabilities of individual observations to observational clusters (OC). IfTRUE, plots the allocation probabilities of groups to distributional clusters (DC).

...

Additional graphical parameters passed to the underlyingimage() function (or equivalent), for customizing the plot (e.g.,col,main,xlab,ylab).

Value

The function plots the variational cluster allocation probabilities.

Examples

# Generate example dataset.seed(123)y <- c(rnorm(60), rnorm(40, 5))g <- rep(1:2, each = 50)# Fit fiSAN via VIest <- fit_fiSAN(y, g, est_method = "VI")# Plot observational cluster probabilitiesplot_vi_allocation_prob(est)# Plot distributional cluster probabilitiesplot_vi_allocation_prob(est, distributional = TRUE)

Print the MCMC Output

Description

Print method for objects of classSANmcmc.

Usage

## S3 method for class 'SANmcmc'print(x, ...)

Arguments

x

Object of classSANmcmc.

...

Ignored.

Value

The function prints a summary of the fitted model.


Print the Variational Inference Output

Description

Print method for objects of classSANvi.

Usage

## S3 method for class 'SANvi'print(x, ...)

Arguments

x

Object of classSANvi.

...

Further arguments passed to or from other methods.

Value

The function prints a summary of the fitted model.


Summarize the MCMC Output

Description

Summary method for objects of classSANmcmc.

Usage

## S3 method for class 'SANmcmc'summary(object, ...)

Arguments

object

of classSANmcmc.

...

Ignored.

Value

The function prints a summary of the fitted model.


Summarize the Variational Inference Output

Description

Summary method for objects of classSANvi.

Usage

## S3 method for class 'SANvi'summary(object, ...)

Arguments

object

Object of classSANvi.

...

Further arguments passed to or from other methods.

Value

The function prints a summary of the fitted model.


[8]ページ先頭

©2009-2025 Movatter.jp