I implemented one type of dependent Dirichlet process mixture model, namely the logistic stick-breaking process mixture, in Nimble. The model works, but it reports warnings about the initialization of membership variables, e.g.,
. I am confused since this is how I usually initialize membership variables.
I would appreciate your suggestions. Thank you very much.
# Nimble model
ddpm <- nimbleCode({
# likelihood
for(i in 1:N) {
for(j in 1:J) {
y[i,j] ~ dcat(ip[z[i], j, 1:C])
}
z[i] ~ dcat(w[i,1:H])
}
# prior on item parameters
for(h in 1:H) {
for(j in 1:J) {
ip[h, j, 1:C] ~ ddirch(beta[1:C])
}
}
# logistic stick-breaking process
for(h in 1:(H-1)) {
b0[h] ~ dnorm(0, tau=1.0E-03)
b1[h] ~ dnorm(0, tau=1.0E-03)
}
for(i in 1:N) {
for(h in 1:(H-1)) {
logit(v[i,h]) <- b0[h] + b1[h]*x[i]
}
w[i,1:H] <- stick_breaking(v[i,1:(H-1)])
}
})
# Prepare data
library(poLCA)
data(election)
dat <- election[,-13] # remove outcome variable VOTE
dat <- dat[complete.cases(dat), ]
N <- 200
data_N <- dat[sample(nrow(dat),N),]
data_N <- lapply(data_N, function(x) as.numeric(x))
data_N <- as.data.frame(data_N)
# Run the MCMC
Y <- data_N[,1:12]
X <- data_N[,13]
C <- 4 # number of categories for each item
H <- 10 # truncation
N <- nrow(Y) # sample size
J <- ncol(Y) # number of items
nMCMC <- 10000 # number of MCMCs
consts <- list(N=N, J=J, H=H, C=C, beta=rep(1, C))
set.seed(123)
ip <- array(0, dim=c(N, J, C)) # initial values for ip
for(i in 1:N) {
for(j in 1:J) {
ip[i,j,] <- MCMCpack::rdirichlet(1, alpha=rep(1,C))
}
}
inits <- list(ip=ip, b0=rep(0.5,H), b1=rep(0.5,H),
z=sample(1:5, size=consts$N, replace=TRUE))
dat <- list(y=Y, x=X)
model <- nimbleModel(code=ddpm, data=dat, inits=inits, constants=consts)
cmodel <- compileNimble(model)
conf <- configureMCMC(model, monitors=c('y'), print=TRUE)
mcmc <- buildMCMC(conf)
cmcmc <- compileNimble(mcmc, project=model)
samples <- runMCMC(cmcmc, niter=nMCMC, nburnin=1000, thin=1, setSeed=TRUE)