Hi Nimble team,
The complied R code amazingly runs fast :), but the building and compiling time is daunting :(.
An example is below.
1. Any tips to shorten the time?
2. I did control of monitor, but "gamma" is still monitored. Is it a bug?
library(nimble)
code <- quote(
{
for(i in 1:I) {
d[i] ~ dnorm(0, sd = 100)
}
for(i in 1:block){
for(c in 2:pairedItem){
weight[i,c-1] ~ dunif(0,1)
tau[i,c-1] ~ dnorm(0, sd = 100)
}
}
s ~ dunif(0,100)
for(n in 1:N) {
for(i in 1:I) {
nu[n,i,1] <- 1
for(c in 2:numP){
nu[n,i,c] <- exp(theta[n] - d[i])
}
de[n,i] <- sum(nu[n,i,1:numP])
for(c in 1:numP){
p[n,i,c] <- nu[n,i,c]/de[n,i]
}
pcm[n,i] ~ dcat(p[n,i,1:numP])
}
theta[n] ~ dnorm(0,sd = s)
for(i in 1:block){
nu2[n,i,1] <- 1
for(c in 2:pairedItem){
nu2[n,i,c] <- exp(weight[i,c-1]*theta[n]+(1-weight[i,c-1])*gamma[n] + tau[i,c-1])
}
de2[n,i] <- sum(nu2[n,i,1:pairedItem])
for(c in 1:pairedItem){
pr[n,i,c] <- nu2[n,i,c]/de2[n,i]
}
nrm[n,i] ~ dcat(pr[n,i,1:pairedItem])
}
gamma[n] ~ dnorm(0,sd = 1)
}
}
)
load("data.R")
constants <- list(N = 1000, I = 10,numP=2,block=5,pairedItem=2)
data <- list(pcm = data$pcm,nrm=data$nrm)
inits <- list(d=rep(0,10))
ptm <- proc.time()
Rmodel <- nimbleModel(code=code, name='Rmodel',constants=constants, inits=inits, check = FALSE)
Rmodel$resetData() # remove data
Rmodel$setData(data) # put data in
RmodelSpec <- configureMCMC(Rmodel, print=TRUE)
RmodelSpec$addMonitors(c('d','s','tau','weight'))
Rmcmc <- buildMCMC(RmodelSpec) # uncomplied R code
Cmodel <- compileNimble(Rmodel)
Cmcmc <- compileNimble(Rmcmc, project = Rmodel)
Cmcmc$run(2000)
samples <- as.matrix(Cmcmc$mvSamples)
require("coda")
burnin = 1000
samples = samples[-(1:burnin),]
coda_samples <- mcmc(samples)
summary(coda_samples)
print(proc.time() - ptm)