Title: | Bayesian Mixtures with an Unknown Number of Components |
---|---|
Description: | Fits Bayesian finite mixtures with an unknown number of components using the telescoping sampler and different component distributions. For more details see Frühwirth-Schnatter et al. (2021) <doi:10.1214/21-BA1294>. |
Authors: | Gertraud Malsiner-Walli [aut, cre]
|
Maintainer: | Gertraud Malsiner-Walli <[email protected]> |
License: | GPL-2 |
Version: | 0.2-0 |
Built: | 2025-02-22 05:58:47 UTC |
Source: | https://github.com/cran/telescope |
Clustering of the draws in the point process representation (PPR) using
-means clustering.
identifyLCAMixture(Func, Mu, Phi, Eta, S, centers)
identifyLCAMixture(Func, Mu, Phi, Eta, S, centers)
Func |
A numeric array of dimension |
Mu |
A numeric array of dimension |
Phi |
A numeric array of dimension |
Eta |
A numeric array of dimension |
S |
A numeric matrix of dimension |
centers |
An integer or a numeric matrix of dimension |
The following steps are implemented:
A functional of the draws of the component-specific
parameters (Func
) is passed to the function. The functionals
of each component and iteration are stacked on top of each other in
order to obtain a matrix where each row corresponds to the
functional of one component.
The functionals are clustered into clusters using
-means
clustering. For each functional a group label is obtained.
The obtained labels of the functionals are used to construct
a classification for each MCMC iteration. Those classifications
which are a permutation of are used to reorder
the Mu and Eta draws and the assignment matrix S. This results in an
identified mixture model.
Note that only iterations resulting in permutations
are used for parameter estimation and deriving the final
partition. Those MCMC iterations where the obtained
classifications of the functionals are not a permutation of
are discarded as no unique assignment of functionals
to components can be made. If the non-permutation rate, i.e. the
proportion of MCMC iterations where the obtained classifications
of the functionals are not a permutation, is high, this is an
indication of a poor clustering solution, as the
functionals are not clearly separated.
A named list containing:
"S"
: reordered assignments.
"Mu"
: reordered Mu matrix.
"Phi"
: reordered Phi matrix.
"Eta"
: reordered weights.
"non_perm_rate"
: proportion of draws where the clustering did not
result in a permutation and hence no relabeling could be
performed; this is the proportion of draws discarded.
Clustering of the draws in the point process representation (PPR) using
-means clustering.
identifyMixture(Func, Mu, Eta, S, centers)
identifyMixture(Func, Mu, Eta, S, centers)
Func |
A numeric array of dimension |
Mu |
A numeric array of dimension |
Eta |
A numeric array of dimension |
S |
A numeric matrix of dimension |
centers |
An integer or a numeric matrix of dimension |
The following steps are implemented:
A functional of the draws of the component-specific
parameters (Func
) is passed to the function. The functionals
of each component and iteration are stacked on top of each other in
order to obtain a matrix where each row corresponds to the
functional of one component.
The functionals are clustered into clusters using
-means
clustering. For each functional a group label is obtained.
The obtained labels of the functionals are used to construct
a classification for each MCMC iteration. Those classifications
which are a permutation of are used to reorder
the Mu and Eta draws and the assignment matrix S. This results in an
identified mixture model.
Note that only iterations resulting in permutations
are used for parameter estimation and deriving the final
partition. Those MCMC iterations where the obtained
classifications of the functionals are not a permutation of
are discarded as no unique assignment of functionals
to components can be made. If the non-permutation rate, i.e. the
proportion of MCMC iterations where the obtained classifications
of the functionals are not a permutation, is high, this is an
indication of a poor clustering solution, as the
functionals are not clearly separated.
A named list containing:
"S"
: reordered assignments.
"Mu"
: reordered Mu matrix.
"Eta"
: reordered weights.
"non_perm_rate"
: proportion of draws where the clustering did not
result in a permutation and hence no relabeling could be
performed; this is the proportion of draws discarded.
Plots of the level combinations of pairs of variables are created where the size of the circle indicating a level combination is proportional to the frequency of the level combination.
plotBubble(x, bg = "grey")
plotBubble(x, bg = "grey")
x |
A matrix or data.frame; the data consisting of categorical variables. |
bg |
If specified, the symbols are filled with colour(s), the
vector |
NULL
with(chickwts, plotBubble(data.frame(cut(weight, 100 * 1:5), feed)))
with(chickwts, plotBubble(data.frame(cut(weight, 100 * 1:5), feed)))
Scatter plots of the observations are plotted by selecting pairs of dimensions, potentially colored by a known classification.
plotScatter(x, z, label = "", trim = 0)
plotScatter(x, z, label = "", trim = 0)
x |
A matrix or data.frame; the data consisting of metric variables. |
z |
A vector; indicating the color to use for the observations. |
label |
A character string; the text to include in the axes labels. |
trim |
A scalar numeric in |
NULL
plotScatter(iris[, 1:4], iris$Species, label = "dim")
plotScatter(iris[, 1:4], iris$Species, label = "dim")
.Obtain a function to evaluate the log prior specified
for .
priorOnAlpha_spec(H = c("alpha_const", "gam_05_05", "gam_1_2", "F_6_3"))
priorOnAlpha_spec(H = c("alpha_const", "gam_05_05", "gam_1_2", "F_6_3"))
H |
A character indicating which specification should be used. |
The following prior specifications are supported:
"alpha_const"
: is fixed at 1.
"gam_05_05"
: gamma(0.5, 0.5), i.e., shape = 0.5, rate = 0.5.
"gam_1_2"
: gamma(1, 2), i.e., shape = 1, rate = 2.
"F_6_3"
: F(6, 3), i.e., an F-distribution with degrees of freedom equal to 6 and 3.
A named list containing:
"log_pAlpha"
: a function of the log prior of .
"param"
: a list with the parameters.
Obtain a function to evaluate the log prior specified
for .
priorOnE0_spec(E = c("G_1_20", "e0const"), e0)
priorOnE0_spec(E = c("G_1_20", "e0const"), e0)
E |
A character indicating which specification should be used. |
e0 |
A numeric scalar giving the fixed value of |
The following prior specifications are supported:
"G_1_20"
: gamma(1, 20), i.e., shape = 1, rate = 20.
"e0const"
: is fixed at
e0
.
A named list containing:
"log_p_e0"
: a function of the log prior of .
"param"
: a list with the parameters.
.Obtain a function to evaluate the log prior
specified for .
priorOnK_spec( P = c("fixedK", "Unif", "BNB_111", "BNB_121", "BNB_143", "BNB_443", "BNB_943", "Pois_1", "Pois_4", "Pois_9", "Geom_05", "Geom_02", "Geom_01", "NB_11", "NB_41", "NB_91"), K )
priorOnK_spec( P = c("fixedK", "Unif", "BNB_111", "BNB_121", "BNB_143", "BNB_443", "BNB_943", "Pois_1", "Pois_4", "Pois_9", "Geom_05", "Geom_02", "Geom_01", "NB_11", "NB_41", "NB_91"), K )
P |
A character indicating which specification should be used. See Details for suitable values. |
K |
A numeric or integer scalar specifying the fixed (if |
The following prior specifications are supported:
"fixedK"
: K has the fixed value K (second argument).
"Unif"
: Unif
, where the upper limit is given by K (second argument).
"BNB_111"
: BNB(1,1,1), i.e.,
follows a beta-negative binomial distribution with parameters
.
"BNB_121"
: BNB(1,2,1), i.e.,
follows a beta-negative binomial distribution with parameters
.
"BNB_143"
: BNB(1,2,1), i.e.,
follows a beta-negative binomial distribution with parameters
.
"BNB_443"
: BNB(4,4,3), i.e.,
follows a beta-negative binomial distribution with parameters
.
"BNB_943"
: BNB(9,4,3), i.e.,
follows a beta-negative binomial distribution with parameters
.
"Pois_1"
: pois(1), i.e.,
follows a Poisson distribution with rate 1.
"Pois_4"
: pois(4), i.e.,
follows a Poisson distribution with rate 4.
"Pois_9"
: pois(9), i.e.,
follows a Poisson distribution with rate 9.
"Geom_05"
: geom(0.5), i.e.,
follows a geometric distribution with success probability
and density
.
"Geom_02"
: geom(0.2), i.e.,
follows a geometric distribution with success probability
and density
.
"Geom_01"
: geom(0.1), i.e.,
follows a geometric distribution with success probability
and density
.
"NB_11"
: nbinom(1,0.5), i.e.,
follows a negative-binomial distribution with
and
.
"NB_41"
: nbinom(4,0.5), i.e.,
follows a negative-binomial distribution with
and
.
"NB_91"
: nbinom(9,0.5), i.e.,
follows a negative-binomial distribution with
and
.
A named list containing:
"log_pK"
: a function of the log prior of .
"param"
: a list with the parameters.
Sample conditional on the current
partition and value of
using an Metropolis-Hastings
step with log-normal proposal.
sampleAlpha(N, Nk, K, alpha, s0_proposal, log_pAlpha)
sampleAlpha(N, Nk, K, alpha, s0_proposal, log_pAlpha)
N |
A number; indicating the sample size. |
Nk |
An integer vector; indicating the group sizes in the partition. |
K |
A number; indicating the number of components. |
alpha |
A numeric value; indicating the value for |
s0_proposal |
A numeric value; indicating the standard deviation of the random walk. |
log_pAlpha |
A function; evaluating the log prior of |
A named list containing:
"alpha"
: a numeric, the new value.
"acc"
: logical indicating acceptance.
Sample conditional on the current partition
and value of
using an Metropolis-Hastings step with
log-normal proposal.
sampleE0(K, Kp, N, Nk, s0_proposal, e0, log_p_e0)
sampleE0(K, Kp, N, Nk, s0_proposal, e0, log_p_e0)
K |
A number; indicating the number of components. |
Kp |
A number; indicating the number of filled components |
N |
A number; indicating the sample size. |
Nk |
An integer vector; indicating the group sizes in the partition. |
s0_proposal |
A numeric value; indicating the standard deviation of the random walk proposal. |
e0 |
A numeric value; indicating the current value of |
log_p_e0 |
A function; evaluating the log prior of |
A named list containing:
"e0"
: a numeric, the new value.
"acc"
: logical indicating acceptance.
where
.This sampling step only relies on the current partition and is independent of the current component-specific parameters, see Frühwirth-Schnatter et al (2021).
sampleK_alpha(Kp_j, Kmax, Nk_j, alpha, log_pK)
sampleK_alpha(Kp_j, Kmax, Nk_j, alpha, log_pK)
Kp_j |
A number; indicating the current value of |
Kmax |
A number; indicating the maximum value of |
Nk_j |
A numeric vector; indicating the group sizes in the partition, i.e., the current number of observations in the filled components. |
alpha |
A number; indicating the value of the parameter |
log_pK |
A function; evaluating the log prior of |
A number indicating the new value of .
This sampling step only relies on the current partition and is independent of the current component-specific parameters, see Frühwirth-Schnatter et al (2021).
sampleK_e0(Kp_j, Kmax, log_pK, log_p_e0, e0, N)
sampleK_e0(Kp_j, Kmax, log_pK, log_p_e0, e0, N)
Kp_j |
A number; indicating the current value of |
Kmax |
A number; indicating the maximum value of |
log_pK |
A function; evaluating the prior of |
log_p_e0 |
A function; evaluating the log prior of |
e0 |
A number; indicating the value of |
N |
A number; indicating the number of observations. |
A number indicating the new value of .
The MCMC scheme is implemented as suggested in Frühwirth-Schnatter et al (2021).
The priors on the model parameters are specified as in Frühwirth-Schnatter et al (2021), see the vignette for details and notation.
sampleLCA( y, S, pi, eta, a0, M, burnin, thin, Kmax, G = c("MixDynamic", "MixStatic"), priorOnK, priorOnWeights, verbose = FALSE )
sampleLCA( y, S, pi, eta, a0, M, burnin, thin, Kmax, G = c("MixDynamic", "MixStatic"), priorOnK, priorOnWeights, verbose = FALSE )
y |
A numeric matrix; containing the data. |
S |
A numeric matrix; containing the initial cluster assignments. |
pi |
A numeric vector; containing the initial cluster-specific success probabilities. |
eta |
A numeric vector; containing the initial cluster sizes. |
a0 |
A numeric vector; containing the parameters of the prior on the cluster-specific success probabilities. |
M |
A numeric scalar; specifying the number of recorded iterations. |
burnin |
A numeric scalar; specifying the number of burn-in iterations. |
thin |
A numeric scalar; specifying the thinning used for the iterations. |
Kmax |
A numeric scalar; the maximum number of components. |
G |
A character string; either |
priorOnK |
A named list; providing the prior on the number of components K, see |
priorOnWeights |
A named list; providing the prior on the mixture weights. |
verbose |
A logical; indicating if some intermediate clustering results should be printed. |
A named list containing:
"Pi"
: sampled component-specific success probabilities.
"Eta"
: sampled weights.
"S"
: sampled assignments.
"Nk"
: number of observations assigned to the different components, for each iteration.
"K"
: sampled number of components.
"Kplus"
: number of filled, i.e., non-empty components, for each iteration.
"e0"
: sampled Dirichlet parameter of the prior on the weights (if is random).
"alpha"
: sampled Dirichlet parameter of the prior on the weights (if is random).
"acc"
: logical vector indicating acceptance in the Metropolis-Hastings step when sampling either or
.
if (requireNamespace("poLCA", quietly = TRUE)) { data("carcinoma", package = "poLCA") y <- carcinoma N <- nrow(y) r <- ncol(y) M <- 200 thin <- 1 burnin <- 100 Kmax <- 50 Kinit <- 10 G <- "MixDynamic" priorOnAlpha <- priorOnAlpha_spec("gam_1_2") priorOnK <- priorOnK_spec("Pois_1") cat <- apply(y, 2, max) a0 <- rep(1, sum(cat)) cl_y <- kmeans(y, centers = Kinit, iter.max = 20) S_0 <- cl_y$cluster eta_0 <- cl_y$size/N pi_0 <- do.call("cbind", lapply(1:r, function(j) { prop.table(table(S_0, y[, j]), 1) })) result <- sampleLCA( y, S_0, pi_0, eta_0, a0, M, burnin, thin, Kmax, G, priorOnK, priorOnAlpha) K <- result$K Kplus <- result$Kplus plot(K, type = "l", ylim = c(0, max(K)), xlab = "iteration", main = "", ylab = expression("K" ~ "/" ~ K["+"]), col = 1) lines(Kplus, col = 2) legend("topright", legend = c("K", expression(K["+"])), col = 1:2, lty = 1, box.lwd = 0) }
if (requireNamespace("poLCA", quietly = TRUE)) { data("carcinoma", package = "poLCA") y <- carcinoma N <- nrow(y) r <- ncol(y) M <- 200 thin <- 1 burnin <- 100 Kmax <- 50 Kinit <- 10 G <- "MixDynamic" priorOnAlpha <- priorOnAlpha_spec("gam_1_2") priorOnK <- priorOnK_spec("Pois_1") cat <- apply(y, 2, max) a0 <- rep(1, sum(cat)) cl_y <- kmeans(y, centers = Kinit, iter.max = 20) S_0 <- cl_y$cluster eta_0 <- cl_y$size/N pi_0 <- do.call("cbind", lapply(1:r, function(j) { prop.table(table(S_0, y[, j]), 1) })) result <- sampleLCA( y, S_0, pi_0, eta_0, a0, M, burnin, thin, Kmax, G, priorOnK, priorOnAlpha) K <- result$K Kplus <- result$Kplus plot(K, type = "l", ylim = c(0, max(K)), xlab = "iteration", main = "", ylab = expression("K" ~ "/" ~ K["+"]), col = 1) lines(Kplus, col = 2) legend("topright", legend = c("K", expression(K["+"])), col = 1:2, lty = 1, box.lwd = 0) }
The MCMC scheme is implemented as suggested in Malsiner-Walli et al (2024).
Also the priors on the model parameters are specified as in Malsiner-Walli et al (2024), see the vignette for details and notation.
sampleLCAMixture( y, S, L, pi, eta, mu, phi, a_00, a_mu, a_phi, b_phi, c_phi, d_phi, M, burnin, thin, Kmax, s_mu, s_phi, eps, G, priorOnWeights, d0, priorOnK, verbose = FALSE )
sampleLCAMixture( y, S, L, pi, eta, mu, phi, a_00, a_mu, a_phi, b_phi, c_phi, d_phi, M, burnin, thin, Kmax, s_mu, s_phi, eps, G, priorOnWeights, d0, priorOnK, verbose = FALSE )
y |
A numeric matrix; containing the data where categories are coded with numbers. |
S |
A numeric matrix; containing the initial cluster assignments. |
L |
A numeric scalar; specifiying the number of classes within each component. |
pi |
A numeric matrix; containing the initial class-specific occurrence probabilities. |
eta |
A numeric vector; containing the initial cluster sizes. |
mu |
A numeric matrix; containing the initial central component occurrence probabilities. |
phi |
A numeric matrix; containing the initial component- and variable-specific precisions. |
a_00 |
A numeric scalar; specifying the prior parameter a_00. |
a_mu |
A numeric vector; containing the prior parameter a_mu. |
a_phi |
A numeric vector; containing the prior parameter a_phi for each variable. |
b_phi |
A numeric vector; containing the initial value of b_phi for each variable. |
c_phi |
A numeric vector; containing the prior parameter c_phi for each variable. |
d_phi |
A numeric vector; containing the prior parameter d_phi for each variable. |
M |
A numeric scalar; specifying the number of recorded iterations. |
burnin |
A numeric scalar; specifying the number of burn-in iterations. |
thin |
A numeric scalar; specifying the thinning used for the iterations. |
Kmax |
A numeric scalar; the maximum number of components. |
s_mu |
A numeric scalar; specifying the standard deviation of the proposal in the Metropolis-Hastings step when sampling mu. |
s_phi |
A numeric scalar; specifying the standard deviation of the proposal in the Metropolis-Hastings step when sampling phi. |
eps |
A numeric scalar; a regularizing constant to bound the Dirichlet proposal away from the boundary in the Metropolis-Hastings step when sampling mu. |
G |
A character string; either |
priorOnWeights |
A named list; providing the prior on the mixture weights. |
d0 |
A numeric scalar; containing the Dirichlet prior parameter on the class weights. |
priorOnK |
A named list; providing the prior on the number of components K, see |
verbose |
A logical; indicating if some intermediate clustering results should be printed. |
A named list containing:
"Eta"
: sampled weights.
"S"
: sampled assignments.
"K"
: sampled number of components.
"Kplus"
: number of filled, i.e., non-empty components, for each iteration.
"Nk"
: number of observations assigned to the different components, for each iteration.
"Nl"
: number of observations assigned to the different classes within the components, for each iteration.
"e0"
: sampled Dirichlet parameter of the prior on the weights (if is random).
"alpha"
: sampled Dirichlet parameter of the prior on the weights (if is random).
"acc"
: logical vector indicating acceptance in the Metropolis-Hastings step when sampling either or
.
"Mu"
: sampled central component occurrence probabilities.
"Phi"
: sampled precisions.
"acc_mu"
: the acceptance rate in the Metropolis-Hastings step when sampling .
"acc_phi"
: the acceptance rate in the Metropolis-Hastings step when sampling .
"nonnormpost_mode"
: parameter values corresponding to the mode of the nonnormalized posterior.
"Pi_k"
: sampled weighted component occurrence probabilities.
data("SimData", package = "telescope") y <- as.matrix(SimData[, 1:30]) z <- SimData[, 31] N <- nrow(y) r <- ncol(y) M <- 5 thin <- 1 burnin <- 0 Kmax <- 50 Kinit <- 10 G <- "MixDynamic" priorOnAlpha <- priorOnAlpha_spec("gam_1_2") priorOnK <- priorOnK_spec("Pois_1") d0 <- 1 cat <- apply(y, 2, max) a_mu <- rep(20, sum(cat)) mu_0 <- matrix(rep(rep(1/cat, cat), Kinit), byrow = TRUE, nrow = Kinit) c_phi <- 30; d_phi <- 1; b_phi <- rep(10, r) a_phi <- rep(1, r) phi_0 <- matrix(cat, Kinit, r, byrow = TRUE) a_00 <- 0.05 s_mu <- 2; s_phi <- 2; eps <- 0.01 set.seed(1234) cl_y <- kmeans(y, centers = Kinit, nstart = 100, iter.max = 50) S_0 <- cl_y$cluster eta_0 <- cl_y$size/N I_0 <- rep(1L, N) L <- 2 for (k in 1:Kinit) { cl_size <- sum(S_0 == k) I_0[S_0 == k] <- rep(1:L, length.out = cl_size) } index <- c(0, cumsum(cat)) low <- (index + 1)[-length(index)] up <- index[-1] pi_km <- array(NA_real_, dim = c(Kinit, L, sum(cat))) rownames(pi_km) <- paste0("k_", 1:Kinit) for (k in 1:Kinit) { for (l in 1:L) { index <- (S_0 == k) & (I_0 == l) for (j in 1:r) { pi_km[k, l, low[j]:up[j]] <- tabulate(y[index, j], cat[j]) / sum(index) } } } pi_0 <- pi_km result <- sampleLCAMixture( y, S_0, L, pi_0, eta_0, mu_0, phi_0, a_00, a_mu, a_phi, b_phi, c_phi, d_phi, M, burnin, thin, Kmax, s_mu, s_phi, eps, G, priorOnAlpha, d0, priorOnK)
data("SimData", package = "telescope") y <- as.matrix(SimData[, 1:30]) z <- SimData[, 31] N <- nrow(y) r <- ncol(y) M <- 5 thin <- 1 burnin <- 0 Kmax <- 50 Kinit <- 10 G <- "MixDynamic" priorOnAlpha <- priorOnAlpha_spec("gam_1_2") priorOnK <- priorOnK_spec("Pois_1") d0 <- 1 cat <- apply(y, 2, max) a_mu <- rep(20, sum(cat)) mu_0 <- matrix(rep(rep(1/cat, cat), Kinit), byrow = TRUE, nrow = Kinit) c_phi <- 30; d_phi <- 1; b_phi <- rep(10, r) a_phi <- rep(1, r) phi_0 <- matrix(cat, Kinit, r, byrow = TRUE) a_00 <- 0.05 s_mu <- 2; s_phi <- 2; eps <- 0.01 set.seed(1234) cl_y <- kmeans(y, centers = Kinit, nstart = 100, iter.max = 50) S_0 <- cl_y$cluster eta_0 <- cl_y$size/N I_0 <- rep(1L, N) L <- 2 for (k in 1:Kinit) { cl_size <- sum(S_0 == k) I_0[S_0 == k] <- rep(1:L, length.out = cl_size) } index <- c(0, cumsum(cat)) low <- (index + 1)[-length(index)] up <- index[-1] pi_km <- array(NA_real_, dim = c(Kinit, L, sum(cat))) rownames(pi_km) <- paste0("k_", 1:Kinit) for (k in 1:Kinit) { for (l in 1:L) { index <- (S_0 == k) & (I_0 == l) for (j in 1:r) { pi_km[k, l, low[j]:up[j]] <- tabulate(y[index, j], cat[j]) / sum(index) } } } pi_0 <- pi_km result <- sampleLCAMixture( y, S_0, L, pi_0, eta_0, mu_0, phi_0, a_00, a_mu, a_phi, b_phi, c_phi, d_phi, M, burnin, thin, Kmax, s_mu, s_phi, eps, G, priorOnAlpha, d0, priorOnK)
The MCMC scheme is implemented as suggested in Frühwirth-Schnatter et al (2021).
The priors on the model parameters are specified as in Frühwirth-Schnatter et al (2021), see the vignette for details and notation.
The parameterizations of the Wishart and inverse Wishart distribution are used as in Frühwirth-Schnatter et al (2021), see also the vignette.
sampleMultNormMixture( y, S, mu, Sigma, eta, c0, g0, G0, C0, b0, B0, M, burnin, thin, Kmax, G = c("MixDynamic", "MixStatic"), priorOnK, priorOnWeights, verbose = FALSE )
sampleMultNormMixture( y, S, mu, Sigma, eta, c0, g0, G0, C0, b0, B0, M, burnin, thin, Kmax, G = c("MixDynamic", "MixStatic"), priorOnK, priorOnWeights, verbose = FALSE )
y |
A numeric matrix; containing the data. |
S |
A numeric matrix; containing the initial cluster assignments. |
mu |
A numeric matrix; containing the initial cluster-specific mean values. |
Sigma |
A numeric matrix; containing the initial cluster-specific variance covariance values. |
eta |
A numeric vector; containing the initial cluster sizes. |
c0 |
A numeric vector; hyperparameter of the prior on |
g0 |
A numeric vector; hyperparameter of the prior on |
G0 |
A numeric vector; hyperparameter of the prior on |
C0 |
A numeric vector; initial value of the hyperparameter |
b0 |
A numeric vector; hyperparameter of the prior on |
B0 |
A numeric vector; hyperparameter of the prior on |
M |
A numeric scalar; specifying the number of recorded iterations. |
burnin |
A numeric scalar; specifying the number of burn-in iterations. |
thin |
A numeric scalar; specifying the thinning used for the iterations. |
Kmax |
A numeric scalar; the maximum number of components. |
G |
A character string; either |
priorOnK |
A named list; providing the prior on the number of components K, see |
priorOnWeights |
A named list; providing the prior on the mixture weights. |
verbose |
A logical; indicating if some intermediate clustering results should be printed. |
A named list containing:
"Mu"
: sampled component means.
"Eta"
: sampled weights.
"S"
: sampled assignments.
"Nk"
: number of observations assigned to the different components, for each iteration.
"K"
: sampled number of components.
"Kplus"
: number of filled, i.e., non-empty components, for each iteration.
"e0"
: sampled Dirichlet parameter of the prior on the weights (if is random).
"alpha"
: sampled Dirichlet parameter of the prior on the weights (if is random).
"acc"
: logical vector indicating acceptance in the Metropolis-Hastings step when sampling either or
.
y <- iris[, 1:4] z <- iris$Species r <- ncol(y) M <- 50 thin <- 1 burnin <- 0 Kmax <- 40 Kinit <- 10 G <- "MixStatic" priorOnE0 <- priorOnE0_spec("G_1_20", 1) priorOnK <- priorOnK_spec("BNB_143") R <- apply(y, 2, function(x) diff(range(x))) b0 <- apply(y, 2, median) B_0 <- rep(1, r) B0 <- diag((R^2) * B_0) c0 <- 2.5 + (r-1)/2 g0 <- 0.5 + (r-1)/2 G0 <- 100 * g0/c0 * diag((1/R^2), nrow = r) C0 <- g0 * chol2inv(chol(G0)) cl_y <- kmeans(y, centers = Kinit, nstart = 100) S_0 <- cl_y$cluster mu_0 <- t(cl_y$centers) eta_0 <- rep(1/Kinit, Kinit) Sigma_0 <- array(0, dim = c(r, r, Kinit)) Sigma_0[, , 1:Kinit] <- 0.5 * C0 result <- sampleMultNormMixture( y, S_0, mu_0, Sigma_0, eta_0, c0, g0, G0, C0, b0, B0, M, burnin, thin, Kmax, G, priorOnK, priorOnE0) K <- result$K Kplus <- result$Kplus plot(K, type = "l", ylim = c(0, max(K)), xlab = "iteration", main = "", ylab = expression("K" ~ "/" ~ K["+"]), col = 1) lines(Kplus, col = 2) legend("topright", legend = c("K", expression(K["+"])), col = 1:2, lty = 1, box.lwd = 0)
y <- iris[, 1:4] z <- iris$Species r <- ncol(y) M <- 50 thin <- 1 burnin <- 0 Kmax <- 40 Kinit <- 10 G <- "MixStatic" priorOnE0 <- priorOnE0_spec("G_1_20", 1) priorOnK <- priorOnK_spec("BNB_143") R <- apply(y, 2, function(x) diff(range(x))) b0 <- apply(y, 2, median) B_0 <- rep(1, r) B0 <- diag((R^2) * B_0) c0 <- 2.5 + (r-1)/2 g0 <- 0.5 + (r-1)/2 G0 <- 100 * g0/c0 * diag((1/R^2), nrow = r) C0 <- g0 * chol2inv(chol(G0)) cl_y <- kmeans(y, centers = Kinit, nstart = 100) S_0 <- cl_y$cluster mu_0 <- t(cl_y$centers) eta_0 <- rep(1/Kinit, Kinit) Sigma_0 <- array(0, dim = c(r, r, Kinit)) Sigma_0[, , 1:Kinit] <- 0.5 * C0 result <- sampleMultNormMixture( y, S_0, mu_0, Sigma_0, eta_0, c0, g0, G0, C0, b0, B0, M, burnin, thin, Kmax, G, priorOnK, priorOnE0) K <- result$K Kplus <- result$Kplus plot(K, type = "l", ylim = c(0, max(K)), xlab = "iteration", main = "", ylab = expression("K" ~ "/" ~ K["+"]), col = 1) lines(Kplus, col = 2) legend("topright", legend = c("K", expression(K["+"])), col = 1:2, lty = 1, box.lwd = 0)
The MCMC scheme is implemented as suggested in Frühwirth-Schnatter et al (2021).
The priors on the model parameters are specified as in Frühwirth-Schnatter et al (2021) and Früwirth-Schnatter and Malsiner-Walli (2019), see the vignette for details and notation.
samplePoisMixture( y, S, mu, eta, a0, b0, h0, H0, M, burnin, thin, Kmax, G = c("MixDynamic", "MixStatic"), priorOnK, priorOnWeights, verbose = FALSE )
samplePoisMixture( y, S, mu, eta, a0, b0, h0, H0, M, burnin, thin, Kmax, G = c("MixDynamic", "MixStatic"), priorOnK, priorOnWeights, verbose = FALSE )
y |
A numeric matrix; containing the data. |
S |
A numeric matrix; containing the initial cluster assignments. |
mu |
A numeric matrix; containing the initial cluster-specific rate values. |
eta |
A numeric vector; containing the initial cluster sizes. |
a0 |
A numeric vector; hyperparameter of the prior on the rate |
b0 |
A numeric vector; hyperparameter of the prior on the rate |
h0 |
A numeric vector; hyperparameter of the prior on the rate |
H0 |
A numeric vector; hyperparameter of the prior on the rate |
M |
A numeric scalar; specifying the number of recorded iterations. |
burnin |
A numeric scalar; specifying the number of burn-in iterations. |
thin |
A numeric scalar; specifying the thinning used for the iterations. |
Kmax |
A numeric scalar; the maximum number of components. |
G |
A character string; either |
priorOnK |
A named list; providing the prior on the number of components K, see |
priorOnWeights |
A named list; providing the prior on the mixture weights. |
verbose |
A logical; indicating if some intermediate clustering results should be printed. |
A named list containing:
"Mu"
: sampled rate .
"Eta"
: sampled weights.
"S"
: sampled assignments.
"Nk"
: number of observations assigned to the different components, for each iteration.
"K"
: sampled number of components.
"Kplus"
: number of filled, i.e., non-empty components, for each iteration.
"e0"
: sampled Dirichlet parameter of the prior on the weights (if is random).
"alpha"
: sampled Dirichlet parameter of the prior on the weights (if is random).
"acc"
: logical vector indicating acceptance in the Metropolis-Hastings step when sampling either e0 or .
N <- 200 z <- sample(1:2, N, prob = c(0.5, 0.5), replace = TRUE) y <- rpois(N, c(1, 6)[z]) M <- 200 thin <- 1 burnin <- 100 Kmax <- 50 Kinit <- 10 G <- "MixDynamic" priorOnAlpha <- priorOnAlpha_spec("gam_1_2") priorOnK <- priorOnK_spec("BNB_143") a0 <- 0.1 h0 <- 0.5 b0 <- a0/mean(y) H0 <- h0/b0 cl_y <- kmeans(y, centers = Kinit, nstart = 100) S_0 <- cl_y$cluster mu_0 <- t(cl_y$centers) eta_0 <- rep(1/Kinit, Kinit) result <- samplePoisMixture( y, S_0, mu_0, eta_0, a0, b0, h0, H0, M, burnin, thin, Kmax, G, priorOnK, priorOnAlpha) K <- result$K Kplus <- result$Kplus plot(K, type = "l", ylim = c(0, max(K)), xlab = "iteration", main = "", ylab = expression("K" ~ "/" ~ K["+"]), col = 1) lines(Kplus, col = 2) legend("topright", legend = c("K", expression(K["+"])), col = 1:2, lty = 1, box.lwd = 0)
N <- 200 z <- sample(1:2, N, prob = c(0.5, 0.5), replace = TRUE) y <- rpois(N, c(1, 6)[z]) M <- 200 thin <- 1 burnin <- 100 Kmax <- 50 Kinit <- 10 G <- "MixDynamic" priorOnAlpha <- priorOnAlpha_spec("gam_1_2") priorOnK <- priorOnK_spec("BNB_143") a0 <- 0.1 h0 <- 0.5 b0 <- a0/mean(y) H0 <- h0/b0 cl_y <- kmeans(y, centers = Kinit, nstart = 100) S_0 <- cl_y$cluster mu_0 <- t(cl_y$centers) eta_0 <- rep(1/Kinit, Kinit) result <- samplePoisMixture( y, S_0, mu_0, eta_0, a0, b0, h0, H0, M, burnin, thin, Kmax, G, priorOnK, priorOnAlpha) K <- result$K Kplus <- result$Kplus plot(K, type = "l", ylim = c(0, max(K)), xlab = "iteration", main = "", ylab = expression("K" ~ "/" ~ K["+"]), col = 1) lines(Kplus, col = 2) legend("topright", legend = c("K", expression(K["+"])), col = 1:2, lty = 1, box.lwd = 0)
The MCMC scheme is implemented as suggested in Frühwirth-Schnatter et al (2021).
The priors on the model parameters are specified as in Frühwirth-Schnatter et al (2021), see the vignette for details and notation.
The parametrizations of the gamma and inverse gamma distribution are used as in Frühwirth-Schnatter et al (2021), see also the vignette.
sampleUniNormMixture( y, S, mu, sigma2, eta, c0, g0, G0, C0_0, b0, B0, M, burnin, thin, Kmax, G = c("MixDynamic", "MixStatic"), priorOnK, priorOnWeights, verbose = FALSE )
sampleUniNormMixture( y, S, mu, sigma2, eta, c0, g0, G0, C0_0, b0, B0, M, burnin, thin, Kmax, G = c("MixDynamic", "MixStatic"), priorOnK, priorOnWeights, verbose = FALSE )
y |
A numeric matrix; containing the data. |
S |
A numeric matrix; containing the initial cluster assignments. |
mu |
A numeric matrix; containing the initial cluster-specific mean values. |
sigma2 |
A numeric matrix; containing the initial cluster-specific variance values. |
eta |
A numeric vector; containing the initial cluster sizes. |
c0 |
A numeric vector; hyperparameter of the prior on |
g0 |
A numeric vector; hyperparameter of the prior on |
G0 |
A numeric vector; hyperparameter of the prior on |
C0_0 |
A numeric vector; initial value of hyperparameter |
b0 |
A numeric vector; hyperparameter of the prior on |
B0 |
A numeric vector; hyperparameter of the prior on |
M |
A numeric scalar; specifying the number of recorded iterations. |
burnin |
A numeric scalar; specifying the number of burn-in iterations. |
thin |
A numeric scalar; specifying the thinning used for the iterations. |
Kmax |
A numeric scalar; the maximum number of components. |
G |
A character string; either |
priorOnK |
A named list; providing the prior on the number of
components K, see |
priorOnWeights |
A named list; providing the prior on the mixture weights. |
verbose |
A logical; indicating if some intermediate clustering results should be printed. |
A named list containing:
"Mu"
: sampled component means.
"Sigma2"
: sampled component component variances.
"Eta"
: sampled weights.
"S"
: sampled assignments.
"Nk"
: number of observations assigned to the different components, for each iteration.
"K"
: sampled number of components.
"Kplus"
: number of filled, i.e., non-empty components, for each iteration.
"e0"
: sampled Dirichlet parameter of the prior on the weights (if is random).
"alpha"
: sampled Dirichlet parameter of the prior on the weights (if is random).
"acc"
: logical vector indicating acceptance in the Metropolis-Hastings step when sampling either or
.
if (requireNamespace("mclust", quietly = TRUE)) { data("acidity", package = "mclust") y <- acidity N <- length(y) r <- 1 M <- 200 thin <- 1 burnin <- 100 Kmax <- 50 Kinit <- 10 G <- "MixStatic" priorOnE0 <- priorOnE0_spec("e0const", 0.01) priorOnK <- priorOnK_spec("Pois_1", 50) R <- diff(range(y)) c0 <- 2 + (r-1)/2 C0 <- diag(c(0.02*(R^2)), nrow = r) g0 <- 0.2 + (r-1) / 2 G0 <- diag(10/(R^2), nrow = r) B0 <- diag((R^2), nrow = r) b0 <- as.matrix((max(y) + min(y))/2, ncol = 1) cl_y <- kmeans(y, centers = Kinit, nstart = 100) S_0 <- cl_y$cluster mu_0 <- t(cl_y$centers) eta_0 <- rep(1/Kinit, Kinit) sigma2_0 <- array(0, dim = c(1, 1, Kinit)) sigma2_0[1, 1, ] <- 0.5 * C0 result <- sampleUniNormMixture( y, S_0, mu_0, sigma2_0, eta_0, c0, g0, G0, C0, b0, B0, M, burnin, thin, Kmax, G, priorOnK, priorOnE0) K <- result$K Kplus <- result$Kplus plot(K, type = "l", ylim = c(0, max(K)), xlab = "iteration", main = "", ylab = expression("K" ~ "/" ~ K["+"]), col = 1) lines(Kplus, col = 2) legend("topright", legend = c("K", expression(K["+"])), col = 1:2, lty = 1, box.lwd = 0) }
if (requireNamespace("mclust", quietly = TRUE)) { data("acidity", package = "mclust") y <- acidity N <- length(y) r <- 1 M <- 200 thin <- 1 burnin <- 100 Kmax <- 50 Kinit <- 10 G <- "MixStatic" priorOnE0 <- priorOnE0_spec("e0const", 0.01) priorOnK <- priorOnK_spec("Pois_1", 50) R <- diff(range(y)) c0 <- 2 + (r-1)/2 C0 <- diag(c(0.02*(R^2)), nrow = r) g0 <- 0.2 + (r-1) / 2 G0 <- diag(10/(R^2), nrow = r) B0 <- diag((R^2), nrow = r) b0 <- as.matrix((max(y) + min(y))/2, ncol = 1) cl_y <- kmeans(y, centers = Kinit, nstart = 100) S_0 <- cl_y$cluster mu_0 <- t(cl_y$centers) eta_0 <- rep(1/Kinit, Kinit) sigma2_0 <- array(0, dim = c(1, 1, Kinit)) sigma2_0[1, 1, ] <- 0.5 * C0 result <- sampleUniNormMixture( y, S_0, mu_0, sigma2_0, eta_0, c0, g0, G0, C0, b0, B0, M, burnin, thin, Kmax, G, priorOnK, priorOnE0) K <- result$K Kplus <- result$Kplus plot(K, type = "l", ylim = c(0, max(K)), xlab = "iteration", main = "", ylab = expression("K" ~ "/" ~ K["+"]), col = 1) lines(Kplus, col = 2) legend("topright", legend = c("K", expression(K["+"])), col = 1:2, lty = 1, box.lwd = 0) }
Simulated multivariate binary data with a 3-group structure where the variables are correlated within the groups.
A data frame with 500 observations and 31 variables:
binary variable coded 1 and 2
binary variable coded 1 and 2
binary variable coded 1 and 2
binary variable coded 1 and 2
binary variable coded 1 and 2
binary variable coded 1 and 2
binary variable coded 1 and 2
binary variable coded 1 and 2
binary variable coded 1 and 2
binary variable coded 1 and 2
binary variable coded 1 and 2
binary variable coded 1 and 2
binary variable coded 1 and 2
binary variable coded 1 and 2
binary variable coded 1 and 2
binary variable coded 1 and 2
binary variable coded 1 and 2
binary variable coded 1 and 2
binary variable coded 1 and 2
binary variable coded 1 and 2
binary variable coded 1 and 2
binary variable coded 1 and 2
binary variable coded 1 and 2
binary variable coded 1 and 2
binary variable coded 1 and 2
binary variable coded 1 and 2
binary variable coded 1 and 2
binary variable coded 1 and 2
binary variable coded 1 and 2
binary variable coded 1 and 2
integer variable with values 1, 2, and 3 indicating group membership