1

I wonder I can use parallel computing in JAGS as I want.

Here is my R script.

library(foreach)
list.data2 <- foreach(i=1:n.rep) %do% {
  foreach(j=1:2) %do% {list( cap       = cap_data[[i]][[j]],
                             loc       = loc_data[[i]][[j]],
                             eff       = eff_data[[i]][[j]],
                             trap.numb = trap.numb2,
                             av        = av,
                             forest    = env$forest,
                             crop      = env$crop,
                             bamboo    = env$bamboo,
                             grass     = env$grass,
                             abandoned = env$abandoned,
                             city      = env$city,
                             rate      = env$for_cr_rate,
                             m.numb    = m.numb,
                             ones = matrix( 1, m.numb, 5 )
                             )  #,bound_mat=bound_mat,bound_numb=bound_numb
  }
}

inits2 <- foreach(j=1:2) %do% {list( n=n.inits2[[j]],
                                     b0=0.5, b1=0.1, b2=0.1, b3=0.1, b4=0.1, b5=0.1, b6=0.1,
                                     a0=5,   a1=0.5, a2=0.5, a3=0.5, a4=0.5, a5=0.5, a6=0.5,
                                     sd=1,
                                     err=rep(0,m.numb),
                                     r_capt=0.10
                                     )
}

para2 <- c("a0","a1","a2","a3","a4", "a5","a6",
           "b0","b1","b2","b3","b4", "b5","b6", "n28", "n29", "r_capt")

library(R2jags)
start.time <- Sys.time()
install.packages("doParallel")

library(doParallel)
registerDoParallel(cores=6)
x_real2 <- foreach( i = 1:2, 
                   .packages = "R2jags"
                    ) %dopar% {jags( "realdata_5years.txt",
                                      data     = list.data2[[i]][[?]],
                                      inits    = inits2[[i]],
                                      para     = para2,
                                      n.chain  = 3,
                                      n.iter   = n.1000000,
                                      n.burnin = 400000,
                                      n.thin   = 200
                                      )
           }
sum_real2 <- foreach(i = 1:2) %do% {x_real2[[i]]$BUGSoutput$summary}
---------------------------------------------------------------------

So, I have two data sets and each has 30 ( == n.rep ) times repetition.
Therefore I have 60 data lists in total.

I would like to use six cores for both 2 data sets and each 3 MCMC chains. Moreover, I need to repeat this calculation 30 ( == n.rep ) times.
However, I have no idea to write in this way. I have problems in the last 4 lines.

Should I use %dopar% twice? or
Should I use jags.parallel in addition to the foreach?

user3666197
  • 1
  • 6
  • 50
  • 92
Yu Yoko
  • 11
  • 1
  • 1
    I'm not sure about `R2jags` but other packages like `runjags` allow you to straightforwardly run each chain on a separate core, without using `doParallel`. It wouldn't help with running multiple separate models in parallel but you could do that with slightly modified scripts that you run in separate R sessions. – Marius Nov 21 '17 at 05:55
  • @Marius Thanks! I am trying to use 'runjags' packages now. It does not work well, though. I am not sure what "adapt" is in runjags. It may not be not used in R2jags packages. Do you know it in detail? – Yu Yoko Nov 27 '17 at 07:12

0 Answers0