Uneven data structures in nimble

22 views
Skip to first unread message

Brook Milligan

unread,
Aug 7, 2025, 12:00:08 PMAug 7
to nimble-users
I would like to specify a model that includes statements like the following:

for (i in 1:m) {
lambda[i] ~ dnorm(mu,sigma)
for (j in 1:n[i]) {
N[[i]][j] ~ dpois(lambda[i])
}
}

That is, observations are grouped (outer loop) and the number of observations in each group varies (n[i]).

Naively, I expected a list of vectors to work here, hence the N[[i]][j] notation. That seems not to be the case.

How should this situation be handled?

Thanks a lot for your help.

Cheers,
Brook

Perry de Valpine

unread,
Aug 7, 2025, 1:36:13 PMAug 7
to Brook Milligan, nimble-users
Hi Brook,
Usually people do this as you have it except for
N[i, j] ~ dpois(lambda[i])
so N is a matrix holding a ragged array. The number of columns will be max(n).
At this point I sometimes suggest looking around our web site's (r-nimble.org) examples and user manual in case you haven't already, but we are currently caught up in a web site outage with our host institution.
An alternative is to make N a vector and use nested indexing for lambda. Then you would have
for(i in 1:m) {lambda[i] ~ dnorm(mu, sigma)}
for(i in 1:N) {y[i] ~ dpois(lambda[group[i]])}
where you provide the index vector `group` in the constants list.
HTH
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 view this discussion visit https://groups.google.com/d/msgid/nimble-users/375A09D9-8CE8-42D9-B396-5F660841AFC9%40nmsu.edu.

Brook Milligan

unread,
Aug 7, 2025, 5:26:21 PMAug 7
to Perry de Valpine, nimble-users
Hi Perry,

Thanks for the response.

I was not aware that you could have ragged arrays; I thought every row had to have the same number of elements. I suppose the trick is to have the an m x max(n[i]) array with a bunch of NAs in the shorter rows. Is that the right idea?

By the way, I have looked on your site a lot in the past, but when I was trying to sort this problem out yesterday I was getting no response from the web server. I appreciate you taking the time to respond here.

Cheers,
Brook
> To unsubscribe from this group and stop receiving emails from it, send an email tonimble-user...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages