I wanna input a two dimension vector in stan 2.21

I can’t use “array” syntax, so I have the following input. the y is an 11 \times T matrix.

data {
  ...
 vector[11] y[T] ;
 ...
} 

I got a warning that :

Error in mod$fit_ptr() : 
  Exception: mismatch in dimension declared and found in context; processing stage=data initialization; variable name=y; position=0; dims declared=(11,293); dims found=(293,11) 

Because I need to use vectors in my functions. I want to know how to input my data in stan. very thanks

What interface are you using? CmdStan, rstan…?

Sorry, use for rstan.
And y[1] means an 11 dimensions vector?

vector[11] y[T] is an array of length T, and each array element is a vector with length 11. In rstan, you can give the y input as a matrix or numeric array with dimension (T, 11)

Thank you, but I meet the problem about that:

function{
vector ssm_update_v(vector y, vector a, vector d, matrix Z) {
  vector[num_elements(y)] v;
  v = y - Z * a - d;
  return v;
}
...
real ssm_constant_lpdf(vector[] y,
                        vector d, matrix Z, matrix H,
                        vector c, matrix T, matrix R, matrix Q,
                        vector a1, matrix P1) {
  real ll;
  int n;
  int m;
  int p;

  n = size(y); // number of obs
  m = cols(Z);
  p = rows(Z);
  {
    vector[n] ll_obs;
    vector[m] a;
    matrix[m, m] P;
    vector[p] v;
    matrix[p, p] Finv;
    matrix[m, p] K;
    matrix[m, m] RQR;
    // indicator for if the filter has converged
    // This only works for time-invariant state space models
    int converged;
    matrix[m, m] P_old;
    real tol;
    real matdiff;
    converged = 0;
    tol = 1e-7;

    RQR = quad_form_sym(Q, R ');
    a = a1;
    P = P1;
    for (t in 1:n) {
      v = ssm_update_v(y[t], a, d, Z);
...

When I use ssm_costant_lpdf(y|…)
there is a warning saying:

Exception: Exception: Exception: subtract: Rows of m1 (293) and rows of m2 (11) must match in size

but y[t] has dimensions of 11, why 293? It really makes me confused…

The exception says number of rows must match, which hints that it is occurring when calling subtract for matrices. In ssm_update_v you call subtract only for vectors, so I’d guess the Exception is coming from some other place. But someone who knows better can tell better, maybe vectors are actually N-by-1 matrices under the hood.

1 Like

I have solve it very thanks~

1 Like