Reputation: 59555
I am using run.jags
function of the Runjags package. The problem is that run.jags
forces adaptation phase even for models that don't need it. I want to let JAGS itself to decide on the necessity and the default length of adaptation phase (some models do need the adaptation phase, some do not). However the run.jags
wrapper forces the default of 1000 iterations for adaptive phase even for models that apparently don't need it.
Reproducible example to illustrate this issue:
library(R2jags)
N <- 1000
y <- rnorm(N)
x <- rnorm(N)
data <- list("N", "y", "x")
inits <- function(){list(beta0=rnorm(1), beta1=rnorm(1), tau=1)}
parameters <- c("beta0", "beta1", "tau")
#inits <- function(){list(beta0=rnorm(1), beta1=rnorm(1), sigma2=1)}
#parameters <- c("beta0", "beta1")
sink("m.bug")
cat("
model{
for (i in 1:N){
y[i] ~ dnorm(mu[i], tau)
mu[i] <- beta0 + beta1*x[i]
}
beta0 ~ dnorm(0, 0.00001)
beta1 ~ dnorm(0, 0.00001)
sigma2 <- 1/tau
tau ~ dgamma(0.001, 0.001)
#sigma2 ~ dunif(0, 100)
#tau <- 1/sigma2
}
")
sink()
m <- R2jags::jags(data, inits, parameters, "m.bug",
n.chains=3, n.iter=2000, n.burnin=1000, n.thin=1)
require("runjags")
data2 <- list(N = N, y = y, x = x)
outRJ <- run.jags("m.bug", parameters, data2, 3, inits,
1000, 1000, thin = 1, summarise=FALSE, plot=FALSE)
Now, if you look at output R2jags::jags, this model does not actually need adaptation and call to adapt
does nothing, possibly due to letting JAGS decide about it (more information here). Whereas run.jags
forces the (unnecessary) adaptation for this model, thus taking much more time to run. It does 1000 iterations for adaptation, then 1000 for burn-in and then 1000 for samples:
> outRJ <- run.jags("m.bug", parameters, data2, 3, inits,
+ 1000, 1000, thin = 1, summarise=FALSE, plot=FALSE)
Compiling rjags model and adapting for 1000 iterations...
Calling the simulation using the rjags method...
Burning in the model for 1000 iterations...
|**************************************************| 100%
Running the model for 1000 iterations...
|**************************************************| 100%
Simulation complete
Finished running the simulation
run.jags
?Upvotes: 1
Views: 1869
Reputation: 2583
The explicit adaptation phase is there for consistency with the command line version of JAGS - rjags and (external) JAGS handle the adaptive phase differently if left to their own devices, so the only way to make sure that these do the same thing is to force rjags/JAGS to have a specific adaptive phase.
The adapt argument allows manual control of the adaptive phase, so you should be able to override this by setting adapt=0 … but I have just noticed a bug that prevents this for the rjags method, sorry. However, it will work as expected if you specify the following arguments: sample=1000, burnin=1000, adapt=0, method='simple'. In the next release I will (fix this bug and) allow 'adapt=NA' which will mean 'allow rjags/JAGS to adapt as it sees fit'; but unfortunately this will mean that the results obtained will dependent on the method…
Hope that helps.
Upvotes: 2