# 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;
^
8:      real H = pop_init;
9:      real bh = theta;
``````

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;
real H = pop_init;
real bh = theta;
real mh = theta;
real ml = theta;
real bl = theta;
// 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> theta; //{bH, mH, bL, mL}
vector<lower=0> sigma;
}
model{
matrix[N, 2] pop;
pop[1,1] = H;
pop[1,2] = L;
vector SEQ;
for(i in 1:20) SEQ[i] = i;
pop[2:N,1:2] = ode_rk45(
MyODE, pop, 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);
target += lognormal_lpdf(L[t] | log(pop[t, 2]), sigma);
}
sigma ~ gamma(2, 30);
theta ~ gamma(4, 2);
theta ~ gamma(2, 4);
theta ~ gamma(3, 2);
theta ~ gamma(1, 10);
}
'
``````

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

1 Like

Hi, Got it. Thanks :)