Large Monitor List for runjags

117 Views Asked by At

With runjags, I am trying to monitor a very large number of values. The format for the monitor list is a string of values, In this case I am asking to moitor just 3, Y[14], Y[15], Y[3].

run.jags(model="model.MC.txt",data=list(Y=Y.NA.Rep,sizes=sizesB,cumul=cumul),
                monitor=c("thetaj", "Y[14]", "Y[15]","Y[3]"))

Suppose I wanted to monitor hundreds of values. I can create this string, but it just returns to the prompt "+". and fails to run.

Is there some upper limit on the size of strings that can be created and passed in as arguments?

Is there a better way (non string) to pass this list into run.jags?

The only way I have been able to get it to run is to paste the string literal into the function call, a variable containing the string does not work.

The longer run list looks something like this:

run.jags(model="model.MC.txt",data=list(Y=Y.NA.Rep,sizes=sizesB,cumul=cumul)
,monitor=c('Y[14]',   'Y[15]',   'Y[18]',   'Y[26]',   'Y[41]',   
'Y[55]',   'Y[62]',   'Y[72]',   'Y[80]',   'Y[81]',   'Y[128]',   'Y[138]',   
'Y[180]',   'Y[188]',   'Y[191]',   'Y[209]',   'Y[224]',   'Y[244]',   ' 
'Y[255]',   'Y[263]',   'Y[282]',   'Y[292]',   'Y[303]',   'Y[324]',   
'Y[349]',   'Y[358]',   'Y[359]',   'Y[365]',   'Y[384]',   

... many lines deleted

'Y[1882]',   'Y[1895]',   'Y[1899]',   'Y[1903]',   'Y[1918]',   'Y[1922]',   
'Y[1929]',   'Y[1942]',   'Y[1953]',   'Y[1990]'))
1

There are 1 best solutions below

2
On BEST ANSWER

I'm not sure that this is a problem with runjags - the following code has 1002 monitors and runs just fine:

model <- "model {
for(i in 1 : N){ #data# N
    Y[i] ~ dnorm(true.y[i], precision) #data# Y
    true.y[i] <- (m * X[i]) + c #data# X
}
m ~ dnorm(0, 10^-3)
c ~ dnorm(0, 10^-3)
precision ~ dgamma(10^-3, 10^-3)
}"

X <- 1:1000
Y <- rnorm(length(X), 2*X + 10, 1)
N <- length(X)

monitors <- c('m','c',paste0('Y[',1:1000,']'))


results <- run.jags(model, n.chains=2, monitor=monitors, sample=100, method='rjags')
results <- run.jags(model, n.chains=2, monitor=monitors, sample=100, method='inter')

I have also tried writing the string directly into the function call by using:

cat('monitor = c("'); cat(monitors, sep='", "'); cat('")\n')

...and copy/pasting the resulting text as the monitor argument - that still works for me in R.app but when pasting into RStudio I get:

> results <- run.jags(model, n.chains=2, monitor = c("m", "c", "Y[1]", "Y[2]", "Y[3]", "Y[4]", "Y[5]", "Y[6]", "Y[7]", "Y[8]", "Y[9]", "Y[10]", "Y[11]", "Y[12]", "Y[13]", "Y[14]", "Y[15]", "Y[16]", "Y[17]", "Y[18]", "Y[19]", "Y[20]", "Y[21]", "Y[22]", "Y[23]", "Y[24]", "Y[25]", "Y[26]", "Y[27]", "Y[28]", "Y[29]", "Y[30]", "Y[31]", "Y[32]", "Y[33]", "Y[34]", "Y[35]", "Y[36]", "Y[37]", "Y[38]", "Y[39]", "Y[40]", "Y[41]", "Y[42]", "Y[43]", "Y[44]", "Y[45]", "Y[46]", "Y[47]", "Y[48]", "Y[49]", "Y[50]", "Y[51]", "Y[52]", "Y[53]", "Y[54]", "Y[55]", "Y[56]", "Y[57]", "Y[58]", "Y[59]", "Y[60]", "Y[61]", "Y[62]", "Y[63]", "Y[64]", "Y[65]", "Y[66]", "Y[67]", "Y[68]", "Y[69]", "Y[70]", "Y[71]", "Y[72]", "Y[73]", "Y[74]", "Y[75]", "Y[76]", "Y[77]", "Y[78]", "Y[79]", "Y[80]", "Y[81]", "Y[82]", "Y[83]", "Y[84]", "Y[85]", "Y[86]", "Y[87]", "Y[88]", "Y[89]", "Y[90]", "Y[91]", "Y[92]", "Y[93]", "Y[94]", "Y[95]", "Y[96]", "Y[97]", "Y[98]", "Y[99]", "Y[100]", "Y[101]", "Y[102]", "Y[103]", "Y[104]", "Y[105]... <truncated>
+                                                    
+ 

Which is somewhat similar to your description. So I'm guessing that you are using RStudio and that the problem is to do with the maximum length of a line of code that can be interpreted by RStudio.

If so, the fix is to simply hard wrap the command so it is broken over multiple lines - I tried this with 72 character width (100+ lines) and it works fine in RStudio. If my assumption is incorrect please modify your question to give more details of how you are running R, and your system using:

> sessionInfo()
R version 3.4.0 (2017-04-21)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Sierra 10.12.5

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
[1] runjags_2.0.4-2

loaded via a namespace (and not attached):
[1] compiler_3.4.0  tools_3.4.0     parallel_3.4.0  coda_0.19-1     grid_3.4.0      rjags_4-6       lattice_0.20-35