Partial sum error

Hi, I get some error about the partial sum in my stan program


The stan code is this

functions {
  real partial_sum(
    vector slice_y,
    int start, int end,
    matrix x, real sigma,
    vector[] beta, int[] g
  )
  {
    real lp = 0;
    
    for (i in start:end)
      lp += normal_lpdf(slice_y[i-start+1] | dot_product(x[i], beta[g[i]]), sigma);
    
    return lp;
  }
}

data {
  int<lower=1> N;             // Number of observations
  int<lower=1> D;             // Number of covariates
  vector[N] y;                // Continuous outcomes (for OLS regression)
  matrix[N, D] x;             // Covariate matrix
  int<lower=1> g[N];          // Group index for each observation
  int<lower=1> P;             // Number of different years
}

parameters {
  vector[D] beta21;           // Coefficients for the first year
  real<lower=0> sigma;        // Standard deviation of the error term
  vector<lower=0>[D] omega;   // Non-negative adjustments to coefficients
  vector[D] eta[P-1];         // Random effects for each year (except the first year)
}

transformed parameters {
  vector[D] beta2[P];         // Coefficients for each year
  
  beta2[1] = beta21;          // Coefficients for the first year
  for (i in 2:P) {
    beta2[i] = beta2[i-1] + omega .* eta[i-1]; // Adjusted coefficients for subsequent years
  }
}

model {
  // Priors
  beta21 ~ normal(0, 10);
  sigma ~ normal(0, 1);
  omega ~ normal(0, 1);
  for (i in 1:(P-1)) {
    eta[i] ~ normal(0, 1);
  }
  
  // Likelihood
  target += reduce_sum(partial_sum, y, 1, x, sigma, beta2, g);
}


But when I check the program I found that the error can’t be fixed:

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

Semantic error in 'string', line 49, column 12 to column 61:

Ill-typed arguments supplied to function 'reduce_sum'. Available arguments:
(T[], int, int, ...) => real, T[], int, ...
(T[,], int, int, ...) => real, T[,], int, ...
(T[,,], int, int, ...) => real, T[,,], int, ...
(T[,,,], int, int, ...) => real, T[,,,], int, ...
(T[,,,,], int, int, ...) => real, T[,,,,], int, ...
(T[,,,,,], int, int, ...) => real, T[,,,,,], int, ...
(T[,,,,,,], int, int, ...) => real, T[,,,,,,], int, ...
Where T is any one of int, real, vector, row_vector or matrix.
Instead supplied arguments of incompatible type: (vector, int, int, matrix, real, vector[], int[]) => real, vector, int, matrix, real, vector[], int[]

I know it may be the problem with my slice_y character, but I revised the type to real, and the error still exist. Some suggestions? Very thanks

y here should be an array of definite size, and slice_y should be an array of arbitrary size. It is from this array that the partial_sum function returns the correct type of container to reduce_sum()

"The user-defined partial sum functions have the signature:
real f(array[] T x_slice, int start, int end, T1 s1, T2 s2, ...)
and take the arguments:

x_slice - The subset of x (from reduce_sum / reduce_sum_static) for which this partial sum is responsible (x_slice = x[start:end])
start - An integer specifying the first term in the partial sum
end - An integer specifying the last term in the partial sum (inclusive)
s1, s2, … - Arguments shared in every term (passed on without modification from the reduce_sum / reduce_sum_static call)"

https://mc-stan.org/docs/stan-users-guide/parallelization.html#reduce-sum

thanks for your reply, I have revised the array y in my reduce_sum block, it is ok now