简体   繁体   中英

How to load arrays with 3 dimension using R2WinBUGS?

Since WinBUGS and R have different ways of organizing data for arrays, how should I organize the data when using R2WinBUGS so that the order is correct? Thanks!

You shouldn't have to worry about this with R2WinBUGS if you specify the data as a named list of objects (see ?bugs - data argument). R2WinBUGS will reorganize the data so that its structure in WinBUGS will be the same as it was in R.

For example, if you specify an array in R:

y <- array(1:24,dim=c(2,3,4))

which looks like

> y
, , 1

     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

, , 2

     [,1] [,2] [,3]
[1,]    7    9   11
[2,]    8   10   12

, , 3

     [,1] [,2] [,3]
[1,]   13   15   17
[2,]   14   16   18

, , 4

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

and then specify it in data argument of bugs function (eg, bugs(data=list(y=y)... ) then the data for WinBUGS (data.txt) is:

list(y= structure(.Data= c(1.00000E+00, 7.00000E+00, 1.30000E+01, 1.90000E+01,  
3.00000E+00, 9.00000E+00, 1.50000E+01, 2.10000E+01, 5.00000E+00, 1.10000E+01,
1.70000E+01, 2.30000E+01, 2.00000E+00, 8.00000E+00, 1.40000E+01, 2.00000E+01, 
4.00000E+00, 1.00000E+01, 1.60000E+01, 2.20000E+01, 6.00000E+00, 1.20000E+01, 
1.80000E+01, 2.40000E+01), .Dim=c(2, 3, 4)))

which looks like this in WinBUGS:

y[1,1,1]      1.0
y[1,1,2]      7.0
y[1,1,3]      13.0
y[1,1,4]      19.0
y[1,2,1]      3.0
y[1,2,2]      9.0
y[1,2,3]      15.0
y[1,2,4]      21.0
y[1,3,1]      5.0
y[1,3,2]      11.0
y[1,3,3]      17.0
y[1,3,4]      23.0
y[2,1,1]      2.0
y[2,1,2]      8.0
y[2,1,3]      14.0
y[2,1,4]      20.0
y[2,2,1]      4.0
y[2,2,2]      10.0
y[2,2,3]      16.0
y[2,2,4]      22.0
y[2,3,1]      6.0
y[2,3,2]      12.0
y[2,3,3]      18.0
y[2,3,4]      24.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM