Pass two-dimensional array of matrices into RStan

In my data block, I define a two-dimensional array of matrices called X. For example:

matrix[4, 12] X[1000, 9];

Each of the 1000 respondents go through 9 trials, and information about these trials are stored in a matrix with 4 rows and 12 columns.

How do I create an object in R that represents what Stan is looking for? If you make a two-dimensional array, you cannot assign an element of it to be a matrix:

> (test <- array(dim = c(3, 4)))
     [,1] [,2] [,3] [,4]
[1,]   NA   NA   NA   NA
[2,]   NA   NA   NA   NA
[3,]   NA   NA   NA   NA

> test[1, 1] <- matrix(1:4, ncol = 2)
Error in test[1, 1] <- matrix(1:4, ncol = 2) : 
  number of items to replace is not a multiple of replacement length

How do I make a two-dimensional array filled with matrices in R, in the format that Stan wants?

I think in R it would need to be an array that is 1000x9x4x12.

3 Likes