I am trying to convert Jags model to stan model.
Jags:
model{
for (i in 1:n){
theta[i] ~ dbeta(u*s, s-u*s)
y[i] ~ dbin(theta[i],N[i])
}
u ~ dunif(0,1)
s ~ dlnorm(4,0.25)
}
stan:
data {
int<lower=0> J;
int y[J];
int N[J];
}
parameters {
real<lower=0, upper=1> u;
real<lower=0> s;
vector[J] theta;
}
model {
s ~ lognormal(4,2);
theta ~ beta(s*u, s*(1-u));
y ~ binomial(N, theta);
}
But when I run it, it returns message as follow:
Chain 1: Rejecting initial value: Chain 1: Error evaluating the log probability at the initial value. Chain 1: Exception: beta_lpdf: Random variable[4] is -1.58608, but must be >= 0! (in 'model29e45483bba0_model' at line 18)
What could generate negative value in this model?
The error message results from the absence of appropriate bounds on theta
. It should be
vector<lower = 0, upper = 1>[J] theta;
Many new users of Stan assume that putting a something like a beta prior on theta
implies that it is between zero and one. However, there is no such implication, particularly on proposals for theta
, which as you can see from your error message can be negative or greater than one unless the bounds are declared (in which case they are enforced by transformations of unconstrained variables).