Thursday, December 29, 2011

vim virtualedit

set virtualedit=onemore so that some mapping still works when the cursor is at the end of file. 

Friday, December 9, 2011

jags data format and openbugs data format

From JAGS manual:

There is no need to transpose matrices and arrays when transferring data between R and JAGS,
since JAGS stores the values of an array in "column major" order, like R and FORTRAN (i.e.
filling the left-hand index first).
If you have an S-style data file for WinBUGS and you wish to convert it for JAGS, then
use the command bugs2jags, which is supplied with the coda package.



Data can be in an S-Plus format (see most of the examples ) or, for data in arrays, in rectangular format.

Missing values are represented as NA.

The whole array must be specified in the file - it is not possible just to specify selected components. Any parts of the array you do not want to specify must be filled with NAs.

All variables in a data file must be defined in a model, even if just left unattached to the rest of the model. 

S-Plus format: This allows scalars and arrays to be named and given values in a single structure headed by key-word list. There must be no space after list.

For example, in the Rats example, we need to specify a scalar 
xbar , dimensions and , a vector and a two-dimensional array with 30 rows and 5 columns. This is achieved using the following format:

         list(
               xbar = 22, N = 30, T = 5,
               x = c(8.0, 15.0, 22.0, 29.0, 36.0),
               Y = structure(
                     .Data = c(
                           151, 199, 246, 283, 320,
                           145, 199, 249, 293, 354,
                           ...................
                           ...................
                           137, 180, 219, 258, 291,
                           153, 200, 244, 286, 324),
                     .Dim = c(30, 5)
                     )
         )

OpenBUGS reads data into an array by filling the right-most index first, whereas the S-Plus program fills the left-most index first. Hence OpenBUGS reads the string of numbers c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) into a 2 * 5 dimensional matrix in the order   
   [i, j]th element of matrix      value
      [1, 1]      1
      [1, 2]      2
      [1, 3]      3
      .....      ..
      [1, 5]      5
      [2, 1]      6
      .....      ..
      [2, 5]      10

whereas S-Plus reads the same string of numbers in the order

   [i, j]th element of matrix      value
      [1, 1]      1
      [2, 1]      2
      [1, 2]      3
      .....      ..
      [1, 3]      5
      [2, 3]      6
      .....      ..
      [2, 5]      10

Hence the ordering of the array dimensions must be reversed before using the S-Plus dput function to create a data file for input into 
OpenBUGS .

For example, consider the 2 * 5 dimensional matrix

         1   2   3   4   5
         6   7   8   9   10

This must be stored in S-Plus as a 5 * 2 dimensional matrix:

> M
[,1] [,2]
[1,] 1 6
[2,] 2 7
[3,] 3 8
[4,] 4 9
[5,] 5 10

The S-Plus command

> dput(list(M=M), file="matrix.dat")

will then produce the following data file

list(M = structure(.Data = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), .Dim=c(5,2))

Edit the .Dim statement in this file from .Dim=c(5,2) to .Dim=c(2,5). The file is now in the correct format to input the required 2 * 5 dimensional matrix into 
OpenBUGS .

Now consider a 3 * 2 * 4 dimensional array

         1   2   3   4
         5   6   7   8

         9   10   11   12
         13   14   15   16

         17   18   19   20
         21   22   23   24

This must be stored in S-Plus as the 4 * 2 * 3 dimensional array:

> A
, , 1
[,1] [,2]
[1,] 1 5
[2,] 2 6
[3,] 3 7
[4,] 4 8

, , 2
[,1] [,2]
[1,] 9 13
[2,] 10 14
[3,] 11 15
[4,] 12 16

, , 3
[,1] [,2]
[1,] 17 21
[2,] 18 22
[3,] 19 23
[4,] 20 24

The command

> dput(list(A=A), file="array.dat")

will then produce the following data file

list(A = structure(.Data = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1 
6, 17, 18, 19, 20, 21, 22, 23, 24), .Dim=c(4,2,3)) 

Edit the .Dim 
statement in this file from .Dim=c(4,2,3) to .Dim=c(3,2,4). The file is now in the correct format to input the required 3 * 2 * 4 dimensional array into OpenBUGS in the order