A little question about the Identifier error

I currently running a sample like this

functions {
int num_matches(vector x, int y) {
  int n = 0;
  for (i in 1:rows(x))
    if (x[i] == y)
      n += 1;
  return n;
// Find the indexes of the elements in the vector x that equal real number y
int[] which_equal(vector x, int y) {
  int match_positions[num_matches(x, y)];
  int pos = 1;
  for (i in 1:size(x)) {
    if (x[i] == y) {
      match_positions[pos] = i;
      pos += 1;
  return match_positions;

data {
  int<lower=1> I; // Total number of observations
  int<lower=1> Q; // Total numbers of covariates
  int<lower=1> J; // Numbers of repeated measurements in time dimension
  int<lower=1> S; // Dimension of alpha
  int<lower=1> L; // Length of each beta
  int<lower=1> K; // Total number of classes
  int<lower=1, upper=K> g[I]; // Class of each observation
  int<lower=1> nz[I, Q]; // calculate the total count of non-zero elements 
  array[I, Q] vector[J] y;  // observed data
  array[I, Q] matrix[J, S] Z;  // covariates for alpha
  array[I, Q] matrix[J, L] B;  // covariates for beta
  int<lower=0, upper=1> data_index[I, Q, J]; // Indicator for non-missing data
  // int data_index_nonzero[I, Q, J];  Nonzero indices for each observation and covariate

parameters {
  array[Q] vector[S] alpha;
  array[K, Q] vector[L] beta;
  matrix[Q, Q] Sigma_omega;
  array[I] vector[Q] omega;
  vector<lower=0>[Q] sigma2;

model {
  for (q in 1:Q) {
    alpha[q] ~ multi_normal(rep_vector(0, S), diag_matrix(rep_vector(100, S))); // Prior for alpha
    sigma2[q] ~ inv_gamma(1, 1); // Prior for sigma2
  for (k in 1:K) {
    for (q in 1:Q) {
      beta[k, q] ~ multi_normal(rep_vector(0, L), diag_matrix(rep_vector(100, L))); // Prior for beta
  for (i in 1:I) {
    omega[i] ~ multi_normal(rep_vector(0, Q), Sigma_omega); // Prior for omega
  for (q in 1:Q) {
    vector[S] alpha_q = alpha[q];
    for (i in 1:I) {
      int g_i = g[i];
      vector[L] beta_giq = beta[g_i, q];
      vector[L] beta_Kq = beta[K, q]; // this is the beta_0 in the formula
      int nz_iq = nz[i, q];
      vector[J] data_index_iq_raw = data_index[i,q];
      int[num_matches(data_index_iq_raw,1)] data_index_iq = which_equal(data_index_iq_raw, 1);

      vector[nz_iq] y_iq = y[i, q, data_index_iq];
      matrix[nz_iq, S] Z_iq = Z[i, q, data_index_iq, :];
      matrix[nz_iq, L] B_iq = B[i, q, data_index_iq, :];
      vector[nz_iq] mu;
      if (g_i == K) {
        // case when g[i] == K
        mu = Z_iq * alpha_q + B_iq * beta_Kq + rep_vector(omega[i, q], nz_iq);
      } else {
        // case when g[i] != K
        mu = Z_iq * alpha_q + B_iq * beta_giq + B_iq * beta_Kq
        + rep_vector(omega[i, q], nz_iq);
      y_iq ~ normal(mu, sqrt(sigma2[q]));

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

Syntax error in ‘string’, line 68, column 9 to column 10, parsing error:

Identifier expected after sized type in local (or model block) variable declaration. (No transformations/constraints allowed.)

but the line 67 shows in my Rstudio interface is this line:
vector[L] beta_giq = beta[g_i, q];
but I don’t think it is this line, so is there anyone can help me where the problem goes on? Really appreciate that if someone could help.

Besides, I wanna ask why when I click the check for my stan file, the error shows are not match, for this example, error shows for line 68 n, but actually bugs comes for line72, and it won’t show exactly the place where the error comes, so it cost a lot time for me to find the bug, how can I configure the rstan so it will show the exactly position for error?

The error is on line

      int[num_matches(data_index_iq_raw,1)] data_index_iq = which_equal(data_index_iq_raw, 1);

where int[..] is wrong; it should be array[..] int. There’s also a type error on the previous line. The correct code is

      vector[J] data_index_iq_raw = to_vector(data_index[i,q]);
      array[num_matches(data_index_iq_raw,1)] int data_index_iq = which_equal(data_index_iq_raw, 1);

I don’t know why RStudio reports wrong line number. Maybe it removes empty lines or something?

It works! Really appreciate that! BTW do you know anything about how to optimize the varying length target, In my model , i is subject, q the variables, and J is time, for each y_iq, the recording time is varying , so it takes almost 8 hours for my models to fit, I set chain 1 and iteration only takes 5000 times , and previous 1000 is the burn in.
If you are available, I will provid detailed formula, and thanks again!