ODE vector function

Hi all,
I was wondering if you had any experience with the following error message after running the stan code:

Error in stanc(filename, allow_undefined = TRUE) : 0

Syntax error in ‘string’, line 4, column 11 to column 15, parsing error:

“[” expression “]” expected after “vector” in local (or model block) variable declaration. (No transformations/constraints allowed.)

The stan code is as follows:

functions {
  vector model3(real t, vector y, real uAB) {
    
    vector dydt[1];
    
    dydt[1] = -uAB*y[1];
    
   return dydt;
  }
}
data {
  int <lower=1> nobs;
  real t0;
  vector[1] y0;
  array[nobs] real ts;
  int <lower=1> indivs;
  array[nobs] real antib;
  //real <lower=1, upper=indivs> subj[nobs];
}
parameters {
  //real <lower=0> AB0;
  //real <lower=0> pAB;
  real <lower=0> uAB;
  //real <lower=0> uASC;
  //real <lower=0> sigmaAB0;
  real <lower=0> sigma;
}
transformed parameters {
  
  array[nobs] vector[1] yhat = ode_bdf_tol(model3, y0, t0, ts, 1e-8, 1e-8, 1000, uAB);
}
model {
  
  //theta ~ lognormal(-2, sqrt(4));
  uAB ~ lognormal(-1, sqrt(2));
  //theta[3] ~ lognormal(-0.1, 0.44);
  //AB0 ~ lognormal(-2, 4);
  //sigmaAB0 ~ gamma(0.001, 0.001);
  sigma ~ normal(0, 1);
  
  //for (i in 1:nobs) {
    //antib[i] ~ lognormal(yhat[i,1], sigma); 
  //}
  antib ~ lognormal(log(yhat[ : , 1]), sigma);
}
generated quantities {
  array[nobs] real z_pred;
  for (j in 1:nobs) {
    z_pred[j] = lognormal_rng(log(yhat[j,1]), sigma); 
  }
}

It’s always helpful to examine the code according to Stan’s error msg. In this case, it says the line vector dydt[1] errs as it’s missing vector size. I think you want vector[1] dydt.

P.S. I changed your typesetting by adding “``` stan” around the code.

1 Like

Thanks! That worked