Hi all,
I’m very new to Stan, so apologies for what I suspect is a simple problem - I haven’t managed to find a solution that works for my code on any of the online help that I have checked.
I’m trying to write a function for use in my Stan program that will take two arguments: (1) a matrix X of N rows, p columns, and (2) an array m (of length N) of integer values showing the number of times to repeat each row of X (I know that in Stan vectors hold reals not integers, so I can’t pass m as a vector?). I would like the function to output a matrix Xout that has number of rows equal to sum(m) and p columns - so the ith row of X (denoted X[i,]) will be repeated m[i] times, creating a “long” version of the matrix. I cannot do this outside stan, as X will contain values that will update during model fit.
I have tried a range of things, each of which gives errors (e.g. (1) passing a vector m of reals in, and extracting each element, assigning to a temporary int value (not shown), or (2) passing in m as an array of integers (non-working code below)). I have been using rep_matrix to repeat the rows of the matrix.
The best I have so far is (I think):
matrix matrixlongver(matrix X, int m[]){
row_vector[cols(X)] Xtemp = X[1,];
matrix[m[1],cols(X)] Xout = rep_matrix(Xtemp,m[1]);
for(i in 2:rows(X)){
Xtemp = X[i,];
matrix[m[i],cols(X)] Xtemp2 = rep_matrix(Xtemp,m[i]);
Xout = append_row(Xout,Xtemp2)
}
return Xout;
}
Which gives me error message:
"PARSER EXPECTED: <argument declaration or close paren ) to end argument declarations>
Error in stanc(file = file, model_code = model_code, model_name = model_name, : "
Would it be possible to have any help / hints / tips / tricks / pointers to already solved problems for the best way of going about this?
Many thanks