How to declare a integer-valued matrix?

My data is a matrix X with the dimension 100 \times 3, whose each element follows a Poisson distribution. Therefore, each element of it must be nonnegative. I wonder how I can declare such matrix in the data block? because an error message reminds me of defining X correctly.

You can define a 2D array of non-negative integers like:

int<lower=0> X[100, 3];

Does this solve your error? If not what was the message?

Thanks!

Hello,

As of Stan 2.26, declaring arrays with this syntax does not work (see here). Since matrices and vectors are by default declared as reals, how can we now define vectors and matrices of integers? My first guess was :

matrix[100, 3] int<lower=0> X;

However, I’m getting this error messages from the parser :

"[" expression "," expression "]" expected for matrix sizes.

It would be array[100,3] int<lower=0> X;

2 Likes

This works but I’m wondering if declaring X as an array instead of a matrix could be inconvenient in some way. After reading this thread on the topic, I believe using basic types (matrix, vector, row_vector, etc.) is generally preferable since you can do matrix arithmetic and linear algebra with them. Is it not possible at all?

No, there is no integer matrix/vector type in Stan

1 Like

You can define the integer array and then convert from array to matrix (which will automatically be real) if you want to use it in matrix calculations. You would use some function called to_matrix or something like that.

1 Like