Passing empty list/array of matrices as data


#1

If I have data of the form
vector[N] x[P];
and in a particular use of my model have P=0 I can just pass an 0xN matrix from R to Stan.

However, if I try to do the equivalent with an array of matrices
vector[N,N] x[P];
passing a 0xNxN array as x then Stan chokes with (here N=10)
"mismatch in number dimensions declared and found in context; processing stage=data initialization; variable name=x; dims declared=(0,10,10); dims found=(0)"
Passing an empty list doesn’t work, and everything is fine with P>0. It seems at some point the dim attributes of x got lost. Is this a bug? Is there some other way I can pass in the array that would work?

(the context is a model with some optional components. I can work
around by making additional special case stan files but this is pretty
cumbersome)

Thanks in advance for any help.


#2

Good seeing you on the forums!

In R, have you tried creating a 3-d array? Something like:

x <- array(0, dim=c(0, 10, 10))

#3

Thanks Dan! 6 minute response time? That’ll do I guess.

Curious. That’s almost exactly what I was doing
x <- array(dim=c(0, 10, 10))
on the basis that the data didn’t matter. This crashes with the above error. It is seems to be equivalent, as you would expect, to
x <- array(NA, dim=c(0, 10, 10))
However, your solution
x <- array(0, dim=c(0, 10, 10))
runs just fine. I’ve no idea what the difference is: as far as I can tell the two objects in R are identical (class, dims, no other attributes).


#4

Sorry, that’s deep in the internals and I don’t really have an clue on what’s going on.


#5

Might be due to the fact that he first two cases create logical arrays, whereas the last one is a numeric array.


#6

Yup I think you’re correct, seems to be the only difference between the resulting objects. Odd to me that array returns a logical rather than a numeric by default but I’ll just add it to the list of R’s idiosyncrasies.