Can not line up 3D vectors to feed data into model

I have a model which takes data from N users, each provides a K dimensional vector per day over T days.

The way I want to write out the data is sequentially for each K dimensional vector, one day at a time per user, then iterate over all users. So it is like

N <- 3
T <- 10
K <- 5

x <- array(1:50, dim = c(K, T, N))

this appears to dump out data in the right order
, , 1

 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]

[1,] 1 6 11 16 21 26 31 36 41 46
[2,] 2 7 12 17 22 27 32 37 42 47
[3,] 3 8 13 18 23 28 33 38 43 48
[4,] 4 9 14 19 24 29 34 39 44 49
[5,] 5 10 15 20 25 30 35 40 45 50

, , 2 (same as above)
, , 3 (same as above)

Then I dumped out x using stan_rdump. The x data looks like
x <-
structure(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50),
.Dim = c(5, 10, 3))

Inside the model, I am quite sure the way to declare the row vector is

row_vector[K] x[N, T]; // where K is the dimension of each row_vector. Less sure about order of N and T.

However, when I run the model, I get
variable name=x; position=0; dims declared=(3,10,5); dims found=(5,10,3)

It thinks I declared x with K as the last dimension. I could modify my x <- definition in r to match, but then I don’t get sequential numbers along K axis any more.

How can this be? Is the problem with the model declaration or with r object declaration?

row_vector[K] x[N, T] means an array of length N containing arrays of length T containing row_vectors of length K.

Just think of it like that. The first index is N, the second is T, the third is K. N = 3, T = 10, K = 5, in your example. ( It is not K row_vectors containing arrays of length N containing arrays of length T – if that was causing confusion)

To get the data to copy in, just make a matrix with indices that look like that. That’d be:

array(1:50, dim = c(N, T, K))

There’s probly a handy R package for swapping indices around if it’s easier on your to build the 3D array the original way you had it.

Hope that helps! (and hopefully it’s right…)

1 Like

Think of the row_vector[K] here as a scalar type stuck in a 2D array – that’s why its index comes last.

Thanks, makes sense the way you explained. But does make it troublesome to generate the data file.

The data file only cares about shape, not type. So inputs for all of the following are the same, because they all have the 2 x 3 x 4 shape.

real a[2, 3, 4];
vector[4] b[2, 3];
row_vector[4] c[2, 3];
matrix[3, 4] d[2];