Hierachical Model Specification- Array Issue


I’m currently running into an issue with model specification as part of an ODE system I’m trying to solve- I’m doing the following:

transformed parameters {
   real params[nStudies, 9];
   real y0[nStudies, 3];
   for(i in 1:nStudies){
     params[i, 1] = epsilon; 
     params[i, 2] = muMF_0;
     params[i, 3] = muW;
     params[i, 4] = foi[i];
     params[i, 5] = muMF_1_max[i];
     params[i, 6] = rho[i];
     params[i, 7] = beta_1_max[i];
     params[i, 8] = lambda[i];
     params[i, 9] = treatment_time;
     y0[i, 1] = fertile_worms_equilibrium(foi[i], muW);
     y0[i, 2] = 0;
     y0[i, 3] = mf_equilibrium(epsilon, y0[1, i], muMF_0);

  real y_hat[nStudies, K, 3];
  for(i in 1:nStudies){
    y_hat[i] = integrate_ode_rk45(LoaModel, to_array_1d(y0[i]), t0, time, to_array_1d(params[i]), x_r, x_i);
- some other code underneath-

And I’m getting this error message:

stan second argument to integrate_ode_rk45 must have type real[] for intial system state; found type=real[,]. fifth argument to integrate_ode_rk45 must have type real[] for parameters; found type=real[,].

Does anyone have any idea what’s going on? I was expecting to_array_1d to give me real[] but evidently not! Any and all help would be massively appreciated!

It helps if you include your whole program so we can test it.

I’m not sure how you’re getting that result, as the only output types of to_array_1d are indeed real[] (or int[], but your y is real).

To solve the problem, you should be able to use just y0[i] instead of to_array_1d and same for params[i]. But it should work as written, which is why I’d like to see the whole program to determine if it’s a parser bug…

Also, you can simplify syntax with:

for (i in 1:nStudies) {
  params[i] = { epsilon, muMF_0, ..., treatment_time };
  y0[i] = { fertile_worms_equilibrium( ), 0, mf_equilibrium(...) };

You might want to just define params and y0 as local variables in the model block—I can’t imagine you want to save params in that form.