suyusung / R2jags

R2jags: Using R to Run 'JAGS'
8 stars 3 forks source link

Error in jags.parallel when providing initial values in a list #2

Closed Pakillo closed 7 years ago

Pakillo commented 7 years ago

Hello,

Many thanks for the package. It's extremely useful!

I'm having problems when using jags.parallel and providing initials values in a list (i.e. already-generated initial values, rather than a function).

I wonder if I'm doing something wrong or there is a bug. A reproducible example follows:

library(R2jags)

# example model file 
model.file <- system.file(package="R2jags", "model", "schools.txt")

# data
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))
}

# This works fine:
jagsfit.p <- jags.parallel(data=jags.data, inits=jags.inits, jags.params,
                           n.iter=5000, model.file=model.file)

# Now providing list of inits (DOESN'T WORK)

inits.list <- list(jags.inits(), jags.inits(), jags.inits())

# inits.list is a list with 3 elements (like the number of chains below)
str(inits.list)
#> List of 3
#>  $ :List of 3
#>   ..$ mu   : num -0.186
#>   ..$ sigma: num 0.319
#>   ..$ theta: num [1:8] -0.553 0.367 -0.417 0.916 -0.289 ...
#>  $ :List of 3
#>   ..$ mu   : num 0.579
#>   ..$ sigma: num 0.209
#>   ..$ theta: num [1:8] 0.9473 -1.5215 -0.1471 -0.0384 -1.0283 ...
#>  $ :List of 3
#>   ..$ mu   : num -1.41
#>   ..$ sigma: num 0.753
#>   ..$ theta: num [1:8] 0.8193 -0.2298 -0.5633 1.1406 -0.0238 ...

# Same model as above, just changing how initial values are provided:
jagsfit.p <- jags.parallel(data=jags.data, inits=inits.list, jags.params,
                           n.chains=3,
                           n.iter=5000, model.file=model.file)

## ERROR MESSAGE:
#> Error in checkForRemoteErrors(lapply(cl, recvResult)): 3 nodes produced errors; first error: Number of initialized chains (length(inits)) != n.chains

# But length(inits) = 3, like n.chains
length(inits.list)
#> [1] 3

Any help much appreciated! Many thanks in advance

My session info:

#> Session info --------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.3.1 (2016-06-21)
#>  system   x86_64, mingw32             
#>  ui       RStudio (0.99.903)          
#>  language (EN)                        
#>  collate  English_United Kingdom.1252 
#>  tz       Europe/Paris                
#>  date     2016-10-25
#> Packages ------------------------------------------------------------------
#>  package    * version    date       source                         
#>  abind        1.4-5      2016-07-21 CRAN (R 3.3.1)                 
#>  assertthat   0.1        2013-12-06 CRAN (R 3.3.0)                 
#>  boot         1.3-18     2016-02-23 CRAN (R 3.3.1)                 
#>  clipr        0.2.1      2016-06-23 CRAN (R 3.3.1)                 
#>  coda       * 0.18-1     2015-10-16 CRAN (R 3.3.0)                 
#>  DBI          0.5-1      2016-09-10 CRAN (R 3.3.1)                 
#>  devtools     1.12.0     2016-06-24 CRAN (R 3.3.1)                 
#>  digest       0.6.10     2016-08-02 CRAN (R 3.3.1)                 
#>  dplyr      * 0.5.0      2016-06-24 CRAN (R 3.3.1)                 
#>  evaluate     0.10       2016-10-11 CRAN (R 3.3.1)                 
#>  formatR      1.4        2016-05-09 CRAN (R 3.3.0)                 
#>  htmltools    0.3.5      2016-03-21 CRAN (R 3.3.0)                 
#>  knitr        1.14       2016-08-13 CRAN (R 3.3.1)                 
#>  lattice      0.20-34    2016-09-06 CRAN (R 3.3.1)                 
#>  lazyeval     0.2.0      2016-06-12 CRAN (R 3.3.1)                 
#>  magrittr     1.5        2014-11-22 CRAN (R 3.3.0)                 
#>  memoise      1.0.0      2016-01-29 CRAN (R 3.3.0)                 
#>  R2jags     * 0.5-7      2015-08-23 CRAN (R 3.3.0)                 
#>  R2WinBUGS    2.1-21     2015-07-30 CRAN (R 3.3.0)                 
#>  R6           2.2.0      2016-10-05 CRAN (R 3.3.1)                 
#>  Rcpp         0.12.7     2016-09-05 CRAN (R 3.3.1)                 
#>  reprex     * 0.0.0.9001 2016-02-24 Github (jennybc/reprex@826ddf4)
#>  rjags      * 4-6        2016-02-19 CRAN (R 3.3.1)                 
#>  rmarkdown    1.1        2016-10-16 CRAN (R 3.3.1)                 
#>  rsconnect    0.5        2016-10-17 CRAN (R 3.3.1)                 
#>  stringi      1.1.2      2016-10-01 CRAN (R 3.3.1)                 
#>  stringr      1.1.0      2016-08-19 CRAN (R 3.3.1)                 
#>  tibble       1.2        2016-08-26 CRAN (R 3.3.1)                 
#>  withr        1.0.2      2016-06-20 CRAN (R 3.3.1)
Pakillo commented 7 years ago

Ah! Now I see in the code of jags.parallel, line 54 (https://github.com/suyusung/R2jags/blob/master/R/jagsParallel.R#L54) that each processor runs one chain (i.e. n.chain = 1). But inits seem to be passed directly, without first separating it in three parts (one for each chain). So each processor runs one chain but receives the initial values for 3 chains...

Might that be the cause? Thanks again for your help

suyusung commented 7 years ago

Hi, Yes, indeed, the current code for jags.parallel only take one batch of the initial values. So in your case, providing three will cause the error. I shall revise the code to take care of this issue.

YS