Scarlet Rice
Scarlet Rice

Reputation: 21

How to pass a list of vectors with various length and list of matrices with various dimensions in Stan model?

I need to pass the data as a list of vectors with various length and a list of matrices with the same number of rows but with a different number of columns. Is there a way to pass the data in Rstan?

Upvotes: 2

Views: 1985

Answers (1)

Ben Goodrich
Ben Goodrich

Reputation: 4990

The answer is essentially no, the Stan language does not allow ragged data structures such as vectors with different lengths, matrices with different numbers of columns, etc.

Depending on your application, it may be easiest to use padding or flattening. By padding I mean adding extra values to your vectors or extra columns to your matrices so that they are all the same size. It is best to use Inf or -Inf as the padded values so that it is easier to spot mistakes if they accidentally get utilized in the target log-kernel. By flattening, I mean making a single long vector by concatenating your vectors of various lengths and the same goes for vectors. Then reform them as vectors and matrices of appropriate sizes in local blocks of your Stan program. In both cases, you would also need to pass the sizes of everything as integer arrays.

Another possibility is to use some script to declare each vector or matrix that you need in the data block, even though they are different sizes. That is simple enough to do but it can be hard to generating the corresponding code to utilize each of them.

Upvotes: 5

Related Questions