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