Marco
2013-05-05
Hi every one
I'm trying to figure out the order used by Jags in the monitor phase and in the file written by the "parameters to" command.
Looking at the two files created for a matrix(say N*M) whit missed data, seems that both the procedure write the matrix by column:
[1,1]
[1,2]
...
[n,1]
[1,2]
...
However the values for the visible data are not ordered in this way. I write below the rats example(bugs_classical volume 1).
Data file:
...
"Y" <-
structure(c(151, 145, 147, 155, 135,
159, 141, 159, 177, 134,
...
NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA
), .Dim = c(30, 5))
Monitor file (Coda index)
Y[1,1] 1 1
Y[2,1] 2 2
Y[3,1] 3 3
...
Y[29,1] 29 29
Y[30,1] 30 30
Y[1,2] 31 31
Y[2,2] 32 32
Monitor file (Coda data)
1001 151
1001 145
1001 147 //Note that these are the entry of the first row in the Data file
1001 155 //and are not by column, because the second value should be 159...
1001 135
...
parameters to file
...
Y
<-
structure(c(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA ...
...
That means that all the first are known parameters.
Someone of you can explain me what kind of order is used??
Thanks
Marco
Martyn Plummer
2013-05-06
JAGS stores matrices (and higher dimensional arrays) in column-major order: when we enumerate the elements of the matrix, the first element moves fastest.
Column-major order is the format used by Fortran, R, Matlab and its variants (Octave, Scilab). When you read the data directly into R, you can see that the first elements of the data file are the first column (151, 145, 147, 155, ...):
> source("rats-data.R") > head(Y) [,1] [,2] [,3] [,4] [,5] [1,] 151 199 246 283 320 [2,] 145 199 249 293 354 [3,] 147 214 263 312 328 [4,] 155 200 237 272 297 [5,] 135 188 230 280 323 [6,] 159 210 252 298 331
The CODA output also uses column-major order, so there is no inconsistency.
If you have used WinBUGS then you may find this confusing because WinBUGS uses row-major ordering. But WinBUGS is the odd one out here! The situation is even more confusing because WinBUGS has a data format it calls "S-PLUS" format, and which can be read into R using the dget()
function, but this does not produce a valid matrix because the elements are in the wrong order.
If you have prepared your data set in WinBUGS-S-PLUS format, you can use the read.bugsdata()
function from the rjags
package to read it into R and resolve the problems with element ordering. Alternatively, you can write a new file with the correct format for JAGS (and indeed R) using the bugs2jags
function from the coda
package.