I am trying to use JAGS to infer the birth rate in a (Stochastic) pure birth process.
In the language of chemistry, this model is equivalent to the reaction: X->2X with rate alpha*X (also can be seen as a model of a chain reaction)
This is the R code I'm using to generate the process (at fixed times) and the jags code to make inference of the parameter alpha.
library(rjags)
y <- 1; # Starting number of "individuals"
N <- 25 # number of time samplings
alpha <- 0.2 # per-capita birth rate
# Generate the time series
for(i in 2:N) {
y<- c(y,y[i-1]+rpois(1,alpha*y[i-1]))
};
# The jags code
model_string <- "model{
for(i in 2:N) {
New[i] ~ dpois(alpha*y[i-1])
y[i] <- y[i-1] + New[i]
}
alpha ~ dunif(0, 2)
}"
# Create and run the jags model
model <- jags.model(textConnection(model_string), data = list(y = y,N = N), n.chains = 3, n.adapt= 10000)
update(model, 5000); # Burnin for 10000 samples
mcmc_samples <- coda.samples(model, variable.names=c("alpha"), n.iter=5000)
When I run the code, I get the following error:
Error in jags.model(textConnection(model_string), data = list(y = y, N = N), :
RUNTIME ERROR:
Compilation error on line 4.
y[2] is a logical node and cannot be observed
I have tried different things like putting alpha*y[i-1] in a new variable (say, lambda[i]) or changing the calls to New[i] by New[i-1] but nothing worked. Any idea why is this failing? Another smarter way to do this?
Thank you in advance.