jags.parallel: setting less clusters than chains: “Error in res[[ch]] : subscript out of bounds”

帅比萌擦擦* 提交于 2019-12-12 13:20:20

问题


I have only 2 core CPU so logically I want to set only two parallel threads/clusters for jags.parallel. unfortunatelly, when I try it and the number of chains is 3 or 4, jags fails with an error:

Error in res[[ch]] : subscript out of bounds

Is lower number of threads (than chains) not allowed?

I have not encountered such statement in the documentation. Anyway, it doesn't make sense to run 4 chains in 4 threads/clusters, when your CPU only has 2 cores! Threads will fight for CPU, caches won't be used optimally and the result will be much slower than using 2 threads only.

Full code:

set.seed(123)

### 14.1.2. Data generation
n.site <- 10
x <- gl(n = 2, k = n.site, labels = c("grassland", "arable"))
eps <- rnorm(2*n.site, mean = 0, sd = 0.5)# Normal random effect
lambda.OD <- exp(0.69 +(0.92*(as.numeric(x)-1) + eps) )
lambda.Poisson <- exp(0.69 +(0.92*(as.numeric(x)-1)) ) # For comparison

C.OD <- rpois(n = 2*n.site, lambda = lambda.OD)
C.Poisson <- rpois(n = 2*n.site, lambda = lambda.Poisson)

### 14.1.4. Analysis using WinBUGS
# Define model
sink("Poisson.OD.t.test.txt")
cat("
model {
# Priors
 alpha ~ dnorm(0,0.001)
 beta ~ dnorm(0,0.001)
 sigma ~ dunif(0, 10)   
 tau <- 1 / (sigma * sigma)
 maybe_overdisp <- mean(exp_eps[])

# Likelihood
 for (i in 1:n) {
    C.OD[i] ~ dpois(lambda[i]) 
    log(lambda[i]) <- alpha + beta *x[i] #+ eps[i]
    eps[i] ~ dnorm(0, tau)
    exp_eps[i] <- exp(eps[i])
 }
}
",fill=TRUE)
sink()


# Bundle data
x = as.numeric(x)-1
n = length(x)
win.data <- list(C.OD = C.OD, x = as.numeric(x)-1, n = length(x))

# Inits function
inits <- function(){ list(alpha=rlnorm(1), beta=rlnorm(1), sigma = rlnorm(1))}

# Parameters to estimate
params <- c("lambda","alpha", "beta", "sigma", "maybe_overdisp")

# MCMC settings
nc <- 3     # Number of chains
ni <- 3000     # Number of draws from posterior per chain
nb <- 1000     # Number of draws to discard as burn-in
nt <- 5     # Thinning rate

require(R2jags)

# THIS WORKS FINE
out <- R2jags::jags(win.data, inits, params, "Poisson.OD.t.test.txt",
    nc, ni, nb, nt);

# THIS PRODUCES ERROR
out <- do.call(jags.parallel, list(names(win.data), inits, params, "Poisson.OD.t.test.txt",
    nc, ni, nb, nt, n.cluster = 2));

来源:https://stackoverflow.com/questions/20156316/jags-parallel-setting-less-clusters-than-chains-error-in-resch-subscrip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!