4

I have a function call (to jags.parallel) that works when given a numerical argument like n.iter = 100 but fails when the argument uses a variable value, n.iter = n.iter. This looks like it might be a bug in jags.parallel

A minimal reproducible example of the error:

    library(R2jags)
    model.file <- system.file(package="R2jags", "model", "schools.txt")
    J <- 8.0
    y <- c(28.4,7.9,-2.8,6.8,-0.6,0.6,18.0,12.2)
    sd <- c(14.9,10.2,16.3,11.0,9.4,11.4,10.4,17.6)    
    jags.data <- list("y","sd","J")
    jags.params <- c("mu","sigma","theta")
    jags.inits <- function(){
      list("mu"=rnorm(1),"sigma"=runif(1),"theta"=rnorm(J))
    }

Then this works:

    jagsfit.p <- jags.parallel(data=jags.data, inits=jags.inits, jags.params, 
                               n.iter=5000, model.file=model.file)

But this does not:

     n.iter=5000
    jagsfit.p <- jags.parallel(data=jags.data, inits=jags.inits, jags.params,
                               n.iter=n.iter, model.file=model.file)

Giving the error:

Error in checkForRemoteErrors(lapply(cl, recvResult)) : 
  3 nodes produced errors; first error: object 'n.iter' not found

I gather this has something to do with not exporting the variable n.iter to the cluster, but it is not clear what parallel engine jags.parallel is using. Is there any way to trick R to evaluate n.iter before passing it to the function?

Tomas
  • 57,621
  • 49
  • 238
  • 373
cboettig
  • 12,377
  • 13
  • 70
  • 113

1 Answers1

5

do.call() is a great go-to friend in situations like this because (from ?do.call):

If 'quote' is 'FALSE', the default, then the arguments are evaluated (in the calling environment, not in 'envir').

I confirmed that the following works, producing results that match your jagsfit.p through all digits displayed by the result object's print method:

jagsfit.p2 <- do.call(jags.parallel, 
                      list(data=jags.data, inits=jags.inits, jags.params,
                           n.iter=n.iter, model.file=model.file))
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • What would one do if the data were actually stored in a list? I.e. if you had all the data as `mydata=list(y=c(28.4,7.9,-2.8,6.8,-0.6,0.6,18.0,12.2), sd=c(14.9,10.2,16.3,11.0,9.4,11.4,10.4,17.6), J=8.0)`? I've run jags(data=mydata, ETC.) before, but with the do.call method here it doesn't work because the names are not found in the environment it's searching in. – Kalin Feb 10 '15 at 01:52
  • @user29020 -- Sorry, I don't use jags enough for your question to be making quick sense to me, and no one else will see it here on a 2.5-year old question. I'd recommend you ask it as a question in its own right, together with a reproducible example, on SO. (You can of course link to this question and answer in your own question.) Best of luck! – Josh O'Brien Feb 10 '15 at 01:57
  • 1
    Thanks for the quick response. I prefer to keep old SO/SE questions in order to reduce duplication. Anyway, it turns out that my question was asked. I just hadn't found it yet. Here it is: http://stackoverflow.com/questions/23790452/using-jags-parallel-from-within-a-function-r-language-error-in-getname-envir?rq=1 – Kalin Feb 10 '15 at 02:06
  • @user29020 Great! Glad to hear that you found that, and thanks for reporting back here. As you say, it's good to keep these related questions well interlinked. – Josh O'Brien Feb 10 '15 at 02:18