I am getting started with Stan, and I have the following state-process model coded in Stan:
data {
int<lower=0> maxIter;
int<lower=0> numGears;
int C[numGears,maxIter,2];
real totalArea;
real a[numGears];
}
parameters {
real<lower=0> N0;
real<lower=0,upper=1> juvProp;
real<lower=0> f;
real<lower=0,upper=1> sJ;
real<lower=0,upper=1> b;
real<lower=0,upper=1> sA;
real<lower=0,upper=1> c[numGears];
}
transformed parameters{
real J[maxIter];
real A[maxIter];
real B[maxIter];
J[1] = N0*juvProp;
A[1] = N0*(1-b)*(1-juvProp);
B[1] = N0*b*(1-juvProp);
for(i in 1:(maxIter-1)){
J[i+1] = f*B[i]/2;
A[i+1] = (1-b)*sJ*J[i]+sA*A[i];
B[i+1] = b*sJ*J[i];
}
}
model {
int nA[numGears,maxIter];
int nJ[numGears,maxIter];
for(i in 1:numGears){
c[i] ~ uniform(0,1);
for(j in 1:maxIter){
nJ[i,j] ~ poisson(a[i]*J[j]/totalArea);
C[i,j,1] ~ binomial(nJ[i,j],c[i]);
nA[i,j] ~ poisson(a[i]*(A[j]+B[j])/totalArea);
C[i,j,2] ~ binomial(nA[i,j],1-c[i]);
}
}
N0 ~ normal(1000,10000) T[0,];
juvProp ~ uniform(0,1);
f ~ lognormal(0,5);
sJ ~ uniform(0,1);
b ~ uniform(0,1);
sA ~ uniform(0,1);
}
When I try to fit the model in rstan:
library(rstan)
numGears <- 2
maxIter <- 10
a <- c(0.2,0.3)
totalArea <- 1
C <- array(rbinom(1000,size = 1000,0.3),c(2,10,2))
stanData <- list(maxIter=maxIter,numGears=numGears,C=C,totalArea=totalArea,a=a)
stanModel <- stan(
file = "StanModel.stan",
data = stanData,
chains= 1,
warmup = 5000,
iter = 50000,
cores = 8
)
I get the following error:
SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 1).
Chain 1: Rejecting initial value:
Chain 1: Error evaluating the log probability at the initial value.
Chain 1: Exception: poisson_lpmf: Random variable is -2147483648, but must be nonnegative! (in 'string', line 38, column 4 to column 43)
This doesn’t make much sense to me. All the parameters are set to be positive, so nothing should produce a negative number during sampling.
What am I doing wrong?