suyusung / R2jags

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

'Number of Initialized Chains' Error in jags() #17

Closed badgerinden closed 2 months ago

badgerinden commented 1 year ago

I encountered an error while running the jags(). The error message is as follows: Error in checkForRemoteErrors(val) : 3 nodes produced errors; first error: Number of initialized chains (length(inits)) != n.chains

I have already checked that the number of chains matches the length of the initial value list, but the error persists. My code is as follows:

#model (Burgar et al., 2018)
model_post <- function() {
  sigma ~ dunif(0,10) # uninformative prior

  lam0 ~ dunif(0,100)
  psi ~ dbeta(1,1)
  for(i in 1:M) {
    z[i] ~ dbern(psi)
    s[i,1] ~ dunif(xlim[1], xlim[2])
    s[i,2] ~ dunif(ylim[1], ylim[2])
    for(j in 1:J) {
      distsq[i,j] <- (s[i,1] - X[j,1])^2 + (s[i,2] - X[j,2])^2
      lam[i,j] <- lam0 * exp(-distsq[i,j] / (2*sigma^2)) * z[i]
    }
  }
  for(j in 1:J) {
    bigLambda[j] <- sum(lam[,j])
    for(k in 1:K) {
      n[j,k] ~ dpois(bigLambda[j])
    }
  }
  N <- sum(z[])
  D <- N/area
}

#data, parameter, inits
dat1 <- list(n=n, X=X2, M=M, J=nrow(n), K=ncol(n), xlim=xlims.scaled, ylim=ylims.scaled, area=areaha.scaled)
pars1 <- c("sigma","lam0","psi","N","D")

z <- rep(1,dat1$M)
init1 <- list("sigma"=1, "lam0"=0.5, "z"=z)
init2 <- list("sigma"=2.5, "lam0"=0.35, "z"=z)
init3 <- list("sigma"=4, "lam0"=0.2, "z"=z)
Inits <- list(init1, init2, init3)

fit_post <- jags.parallel(data = dat1, inits = Inits, parameters.to.save = pars1,
                             n.chains = 3, n.iter = 2000, n.burnin = 1000, model.file = model_post,
                             RNGname = "Wichmann-Hill", jags.seed = 1234)

Here is my sesionInfo()

R version 4.3.1 (2023-06-16 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8    LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C                           LC_TIME=English_United States.utf8    

time zone: Asia/Seoul
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggmcmc_1.5.1.1 ggplot2_3.4.3  tidyr_1.3.0    dplyr_1.1.2    R2jags_0.7-1   rjags_4-14     coda_0.19-4   

loaded via a namespace (and not attached):
 [1] gtable_0.3.4       compiler_4.3.1     tidyselect_1.2.0   Rcpp_1.0.10        parallel_4.3.1     gridExtra_2.3      scales_1.2.1      
 [8] boot_1.3-28.1      GGally_2.1.2       lattice_0.21-8     plyr_1.8.8         R6_2.5.1           generics_0.1.3     tibble_3.2.1      
[15] R2WinBUGS_2.1-21   munsell_0.5.0      RColorBrewer_1.1-3 pillar_1.9.0       rlang_1.1.1        rgdal_1.6-7        utf8_1.2.3        
[22] sp_1.6-1           reshape_0.8.9      terra_1.7-39       cli_3.6.1          withr_2.5.0        magrittr_2.0.3     grid_4.3.1        
[29] rstudioapi_0.15.0  lifecycle_1.0.3    vctrs_0.6.2        glue_1.6.2         raster_3.6-20      codetools_0.2-19   egg_0.4.5         
[36] abind_1.4-5        fansi_1.0.4        colorspace_2.1-0   purrr_1.0.1        tools_4.3.1        pkgconfig_2.0.3   

Reference Burgar, Joanna M., et al. "Estimating density for species conservation: Comparing camera trap spatial count models to genetic spatial capture-recapture models." Global Ecology and Conservation 15 (2018): e00411

suyusung commented 2 months ago

Hi,

We are broadcasting initializations to each cluster either through a distribution function or as a list for a chain, one at a time. In your current code, you are broadcasting initializations as a list with a length of 3, which may lead to a problem. This is because we are operating a single chain on a single thread at a time. Consequently, the current approach may not function as intended.