Vector indexing doesn't work

Hi everyone,
I think this is very simple question but I failed to fix it. I am defining my ODE in stan where I get an error when I assign the first element of pop_init (which is a vector) to the variable L which is a real. But I get the incompatibility error. Any help is appreciated. Thanks

 5:                    vector[] theta,             // parameters
 6:                    array[] real x_r, array[] int x_i) {  // unused
 7:      real L = pop_init[1];
         ^
 8:      real H = pop_init[2];
 9:      real bh = theta[1];

Ill-typed arguments supplied to assignment operator =: lhs has type real and rhs has type vector
This is the entire stan code:

functions {
  vector[] MyODE( real t,                 // time
                vector[] pop_init,          // initial states
                vector[] theta,
                array[] real x_r, array[] int x_i) {  // unused             // parameters
     real L = pop_init[1];
    real H = pop_init[2];
    real bh = theta[1];
    real mh = theta[2];
    real ml = theta[3];
    real bl = theta[4];
    // differential equations
    real dH_dt = (bh - mh * L) * H;
    real dL_dt = (bl * H - ml) * L;
    return { dL_dt , dH_dt };
  }
}
data{
  int N; 
  vector[N] L; 
  vector[N] H; 
}
parameters{
  vector<lower=0>[4] theta; //{bH, mH, bL, mL}
  vector<lower=0>[2] sigma;
}
model{
  matrix[N, 2] pop;
  pop[1,1] = H[1];
  pop[1,2] = L[1];
  vector[20] SEQ;
  for(i in 1:20) SEQ[i] = i; 
  pop[2:N,1:2] = ode_rk45(
    MyODE, pop[1], 0, SEQ, theta,
    rep_array(0.0, 0), rep_array(0, 0),
    1e-5, 1e-3, 5e2);
  for ( t in 2:N ){
    target += lognormal_lpdf(H[t] | log(pop[t, 1]), sigma[1]); 
    target += lognormal_lpdf(L[t] | log(pop[t, 2]), sigma[2]); 
  }
  sigma ~ gamma(2, 30);
  theta[1] ~ gamma(4, 2);
  theta[2] ~ gamma(2, 4);
  theta[3] ~ gamma(3, 2);
  theta[4] ~ gamma(1, 10);
}
'

vector[] is the (deprecated) equivalent of array[] vector. So the line real L = pop_init[1] is selecting the first element of an array of vectors, which is a vector, not a real.

1 Like

Hi, Got it. Thanks :)