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