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)
}
Run the code above in your browser using DataLab