5

When using jags.parallel, I get the following error:

> out <- jags.parallel(win.data, inits, params, "Poisson.OD.t.test.txt",
+ nc, ni, nb, nt);
Error in get(name, envir = envir) : invalid first argument

The same call using jags function runs OK. I have only found one thread on this topic, but there is only one speculative suggestion that does not apply nor work here.

Reproducible code, taken from Introduction to WinBUGS for ecologists, see chapter 14.1 (slightly modified):

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
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 <- jags.parallel(win.data, inits, params, "Poisson.OD.t.test.txt",
    nc, ni, nb, nt);

# THIS ALSO PRODUCES ERROR
out <- do.call(jags.parallel, list(win.data, inits, params, "Poisson.OD.t.test.txt",
    nc, ni, nb, nt));
Tomas
  • 57,621
  • 49
  • 238
  • 373
  • 1
    The error occurs because the first argument to `get` must be a character string naming a function. Use `traceback` to see which function calls `get` and produces the error, then `debug` that function to investigate the objects in the function body. Or you can use `options(error=recover)` (and turn it off via `options(error=NULL)`). – Joshua Ulrich Jul 23 '13 at 11:44
  • Thanks @Joshua! So this actually leads to debugging the jags package, not my code, right? If we conclude it is a bug, then maybe I the best strategy is to report to jags developers? – Tomas Jul 23 '13 at 11:47
  • Not sure. I'm not familiar with these packages and didn't run the code, so it could still be one of your functions (e.g. maybe `inits` should be `"inits"` in the `jags.parallel` call?)... but that should be clear to you once you run `traceback`. – Joshua Ulrich Jul 23 '13 at 11:51
  • Possibly not unrelated: http://stackoverflow.com/q/21367922/684229 – Tomas Jan 26 '14 at 19:19

2 Answers2

4

Jags/R had practically two problems with this line:

out <- jags.parallel(win.data, inits, params, "Poisson.OD.t.test.txt",
    nc, ni, nb, nt);

Both are related to evaluation of function parameters - he is probably not able to resolve parameters which refer to other R variables:

1) The win.data was encoded as variable names as usually for WinBUGS/Jags:

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

but jags.parallel issues the error "Error in get(name, envir = envir) : invalid first argument". He wants the data in this format:

windata <- list("C.OD", "x", "n")

Why? Don't ask me... I discovered this when reading the example of ?jags.

2) The arguments nc, ni, nb, nt in function call are a problem! If I put constants, it is OK, but references to variables are unacceptable for him. Don't ask me why. Remedy found at strange jags.parallel error / avoiding lazy evaluation in function call.

The complete fix looks like:

out <- do.call(jags.parallel, list(names(win.data), inits, params, "Poisson.OD.t.test.txt",
    nc, ni, nb, nt));
Community
  • 1
  • 1
Tomas
  • 57,621
  • 49
  • 238
  • 373
  • Thanks! I can add that the model MUST be a filename. A textConnection(modelString) doesn't work. I used sink(), cat() and file.remove() to handle the file. – Jonas Lindeløv Apr 20 '14 at 22:22
  • @Jonas, exactly - I do it like this all the time. – Tomas Apr 22 '14 at 11:27
  • @TMS, a quick question, does jags.parallel actually parallelize? I mean, does it need other packages like ``snow`` to do the parallelization, or does it parallelize different chains by itself and we don't need to take care of that part? Thanks! – qkhhly Mar 28 '15 at 01:09
  • @qkhhly I don't actually remember, but I *think* it actualy parallelizes. But I gave up completely on using R2jags and this function, since there have been many bugs and strange problems like this one. I am using runjags package now and I am satisfied. With the `run.jags` function you can just specify `method = "rjparallel"` and the chains will run on separate cores. And the best thing is that it *actually works* :-) – Tomas Mar 29 '15 at 17:36
  • @TMS & @qkhhly, I think (at least `R2jags` version 0.5-7) uses the R package `parallel` to initialize clusters etc. It does have some nice wrappers for exporting objects to the cluster. – Michael Roswell Mar 02 '20 at 20:52
  • @TMS, the why this format for `windata` works might be how the package figures out what to export to the cluster... btw. – Michael Roswell Mar 02 '20 at 20:53
0

sometimes this error occurs when you use a vectorized function inside parlapply. I've solved the problem by adding "do.call" and "mapply" functions to cluster.export

zercchi
  • 41
  • 1
  • 2
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 04 '23 at 15:56