Ragged arrays

148 views
Skip to first unread message

Arthur Lui

unread,
Jul 24, 2018, 9:30:59 PM7/24/18
to nimble-users
Hi,

What is the recommended way to handle ragged arrays (arrays or matrices of different sizes) in nimble?

For example,

y[i][j] ~ Normal(mu[i],1), for i=1,...,I, and j=1,...,J[i], where J[a] != J[b].
mu[i] ~ Normal(0,1), for i=1,...,I

What is the recommended way to express this in nimbleCode, and in the data argument in nimbleModel?

Thanks,
Arthur

Message has been deleted

Arthur Lui

unread,
Jul 24, 2018, 11:57:22 PM7/24/18
to nimble-users
I found something that works for me here. But I'd still be interested to know if there's a better way.

For reference:

### this belongs in ragged.R ###
library(nimble)

### Nimble Model Code ###
model.code = nimbleCode({
  for (i in 1:I) {
    for (j in 1:J[i]) {
      y[idxOffset[i] + j] ~ dnorm(mu[i], 1)
    }
    mu[i] ~ dnorm(m, var=s2)
  }
})

### Data ###
J = c(30, 15)
mu_true = c(-3, 5)
y_orig = list(rnorm(J[1], mu_true[1]), rnorm(J[2], mu_true[2]))
I = length(y_orig)
y = Reduce(c, y_orig)
hist(y)

### Model data, constants, and inits ###
model.data = list(y=y)
model.consts = list(m=0, s2=100, idxOffset=c(0, cumsum(J[-I])), I=I, J=J)
model.inits = list(mu=rep(0, I))

### Model ###
model = nimbleModel(model.code, data=model.data, constants=model.consts, inits=model.inits)


Perry de Valpine

unread,
Jul 25, 2018, 8:30:35 AM7/25/18
to luia...@gmail.com, nimble-users
Hi Arthur,

A common way to do it would be:
  for (i in 1:I) {
    for (j in 1:J[i]) {
      y[i, j] ~ dnorm(mu[i], 1)
    }
    mu[i] ~ dnorm(m, var=s2)
  }

y will become a matrix variable with sizes I x max(J[]), but it will be effectively ragged because only the declared nodes with be in the model (the directed acyclic graph, if you like to think of it that way).  The data can be provided as a full matrix.  Values of elements that don't exist in the model shouldn't matter.

Unfortunately in the current release, there is an over-zealous error trap that incorrectly traps some ragged arrays and gives an error message when none is needed.  A work-around to disable this problem is nimbleOptions(allowDynamicIndexing = FALSE), as long as you do not need stochastic indices in the model.

This should be fixed in a release soon.

-Perry


--
You received this message because you are subscribed to the Google Groups "nimble-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To post to this group, send email to nimble...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/c4de9389-fc37-482a-93ee-c29ebaf45342%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Arthur Lui

unread,
Jul 25, 2018, 8:49:19 AM7/25/18
to nimble-users
Excellent. Thank you very much!

Arthur Lui

unread,
Jul 25, 2018, 9:06:21 AM7/25/18
to nimble-users
One more question about your response, actually. In your solution, how would you pass y as data into the model? For example, would you use an R list of vectors? Or a (rectangular) matrix padded with zeros?

Chris Paciorek

unread,
Jul 25, 2018, 10:22:00 AM7/25/18
to Arthur Lui, nimble-users
Hi Arthur,

You'd need to use a matrix padded with zeros.

Chris

On Wed, Jul 25, 2018 at 6:06 AM, Arthur Lui <luia...@gmail.com> wrote:
> One more question about your response, actually. In your solution, how would you pass y as data into the model? For example, would you use an R list of vectors? Or a (rectangular) matrix padded with zeros?
>
> --
> You received this message because you are subscribed to the Google Groups "nimble-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
> To post to this group, send email to nimble...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/04a6f9f4-42c9-458e-a813-4712e62a3679%40googlegroups.com.

Arthur Lui

unread,
Jul 25, 2018, 11:55:23 AM7/25/18
to christophe...@gmail.com, nimble...@googlegroups.com
Thanks! 
Reply all
Reply to author
Forward
0 new messages