# There is a sum term in my model，i don't know how to code？

sum term is \sum_{k=0}^{m}(_k^{y})a^k*(1-a)^{y-k}*\lambda^{x-k}*e^{-\lambda}/(x-k)!,which m=min(y,x),\lambda is parameter and i have dataset (y_i,x_i,a_i),here’s part of my model below：

model{
……
for (i in 1:N){
m[i] = min(y[i],x[i]);
f[i] = 0;
for (k in 0:m[i]) {
f[i] = f[i]+choose(y[i],k)* a[i]^k*(1-a[i])^(y[i]-k)*\lambda^(x_t[i]-k)*exp(-\lambda)/tgamma(x[i]-k+1);
}
……
}
}


but
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Loop variable already declared. variable name=“k”
error in ‘model400c5bd47446_bayesquantileestimationsigma_’ at line 49, column 9

I don’t understand why you get this specific error message about the loop variable k (unless you are already defining somewhere else in your stan model). Perhaps the parser is confused by the fact you are using \lambda, which I don’t think it’s a valid identifier name.

Maybe it has the loop variable i already, and it doesn’t need define either. i don’t know why it will show error when using another loop variable k(when it showed error i tried to define it, but didn’t work), maybe there is other way which i don’t know to code. \lambda is the parameter i want to estimate, and it’s not the point.

The loop variable doesn’t need to be defined like other variables, you only have to name it in the for loop. So if you have somewhere an int k or similar, try removing it.

Failing that, could you post the code you are trying to compile? It’ll be easier to see what may be going on.

this is my code:

data {
int<lower=0> N;
real Y[N];
real X1[N];
int Y_t[N];
int Y_t_1[N];
real tau;
}
parameters {
real a;
real b;
real c;
real sigma;
real d;
}
model {
int h[N];
real u[N];
real lambda[N];
real a_t[N];
int m_t[N];
real f[N];
real g[N];
int k;
int j;

sigma~chi_square(3);
a~normal(0,10);
b~normal(0,10);
c~normal(0,10);
for (i in 1:N){
m_t[i] = min(Y_t[i],Y_t_1[i]);
a_t[i]=exp(X1[i])/(1+exp(X1[i]));
f[i] = 0;
for (k in 0:m_t[i]) {
f[i] = f[i]+choose(Y_t_1[i],k)* a_t[i]^k*(1-a_t[i])^(Y_t_1[i]-k)*d^(Y_t[i]-k)*exp(-d)/tgamma(Y_t[i]-k+1)*(k-Y_t_1[i]*a_t[i]);
}
g[i] = 0;
for (j in 0:m_t[i]) {
g[i] = g[i]+choose(Y_t_1[i],k)* a_t[i]^k*(1-a_t[i])^(Y_t_1[i]-k)*d^k*exp(-d)/tgamma(k+1)
}
h[i]=0;
u[i]=Y[i]-a-b*X1[i]-c*f[i]/g[i];
lambda[i]=log(tau*(1-tau))+log(sigma)+(fabs(u[i]/sigma)+(2*tau-1)*u[i]/sigma)/2+10000;
h[i]~poisson(lambda[i]);
}
}


if i remove the int k,it shows:
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Variable “k” does not exist.
error in ‘model400c74c8771_bayesquantileestimationsigma_’ at line 52, column 35

This is because after the loop over k, you have a loop over j in which you use k. But k is out of scope here since it’s only defined inside the k loop. If you change the k’s inside the loop over j into j’s, then that should fix this problem.

1 Like

The second loop says (j in 1:m_t[i]) but it has no j in it and instead uses k so I think that’s a typo. Here’s a cleaned up version of your model that at least compiles but I still don’t know if it does what you want. It’s weird because h is always zero but it’s used in a sampling statement. Maybe that’s ok, I don’t really understand the model.

data {
int<lower=0> N;
real Y[N];
real X1[N];
int Y_t[N];
int Y_t_1[N];
real tau;
}
parameters {
real a;
real b;
real c;
real<lower=0> sigma;
real d;
}
model {
sigma ~ chi_square(3);
a ~ normal(0,10);
b ~ normal(0,10);
c ~ normal(0,10);
for (i in 1:N) {
int m_t = min(Y_t[i],Y_t_1[i]);
real a_t = inv_logit(X1[i]);
vector[m_t+1] f;
vector[m_t+1] g;
real lambda;
real u;
for (k in 0:m_t) {
f[k+1] = choose(Y_t_1[i], k) * a_t^k*(1-a_t)^(Y_t_1[i]-k) * d^(Y_t[i]-k) * exp(-d) / tgamma(Y_t[i]-k+1) * (k - Y_t_1[i]*a_t);
}
for (k in 0:m_t) {
g[k+1] = choose(Y_t_1[i],k) * a_t^k * (1-a_t)^(Y_t_1[i]-k) * d^k * exp(-d) / tgamma(k+1);
}
u = Y[i] - a - b*X1[i] - c*sum(f)/sum(g);
lambda = log(tau*(1-tau)) + log(sigma) + (fabs(u/sigma) + (2*tau-1)*u/sigma)/2 + 10000;
0 ~ poisson(lambda); // h = 0 ?
}
}

1 Like

thank you very much

thank you a lot