## unbalanced data

Help
Paul
2011-01-01
2012-09-01

• Paul
2011-01-01

I just start learning JAGS, and one of the WinBUGS model I am working on has
to deal with unbalanced data.

I am wondering how can we specify variables in the JAGS model when working
with unbalanced data, i.e. WinBUGS style nested indexing trick.

For example:

var

DW,

theta,

z; <-----????

model {

for(i in 1:D) {

theta ~ ddirch(a);

for(j in 1:DW_) {

z ~ dcat(theta);

}

}

}

say

D = 5,

T = 3

DW = (2, 3, 1, 3, 4),

z = (

1, 2,

2,2,3,

3,

1,2,3,

1,2,3,3

)

Thank you very much!

_

• Martyn Plummer
2011-01-04

Treat z as a vector and define a cumulative index variable. You can do this
inside a data block:

```data {
c[1] <- DW[1]
for (i in 1:D) {
c[i+1] <- c[i] + DW[i]
}
}
```

Or you can define c in your data. In your example it takes the values
(1,3,6,7,10,14).

Then your model looks like this

```model {
for (i in 1:D) {
theta[i, 1:T] ~ ddirch(a[])
for (j in c[i]:(c[i+1] - 1)) {
z[j] ~ dcat(i, 1:T)
}
}
}
```

You do not have to declare the variable z and in fact it is more convenient
not to do so in this case, as the length of z is calculated on the fly

• Martyn Plummer
2011-01-04

Hmmm. My code is not displaying correctly, despite being in a "code" block.
Let's try again.

Data block:

```data {
c[1] <- DW[1]
for (j in 1:D) {
c[j+1] <- c[j] + DW[j]
}
}
```

Model block:

```model {
for (j in 1:D) {
theta[j, 1:T] ~ ddirch(a)
for (k in c[j]:(c[j+1] - 1)) {
z[k] ~ dcat(theta[j])
}
}
}
```

• Martyn Plummer
2011-01-04

OK that worked, but I had to change the index "i" to "j" and change "j" to
"k".

• Paul
2011-01-04

Martyn, thanks for your hint! I really appreciate it.