Hi everybody,
I have one question: How to monitor an intermediate variables?
I have some model in Jags and want to translate it into Stan. However, I faced the difficulty to moninor an intermediate variables:
The model in Jags has some parts like:
model {
# Initial condition: At the lowest age
S[1] <- S1; I[1] <- I1; R[1] <- R1
rateRecover <- 1-exp(-sigma)
# From the 2nd age-group
for (i in 2:84)
{
# lambda[i] = age-dependent-FOI.
lambda[i] <- beta1 + 2*beta2*age[i]
# Newly infecteds, recovers, susceptible again.
rate[i] <- 1-exp(-lambda[i])
Inew[i] ~ dbin(rate[i], S[i-1]) # New infected
Rnew[i] ~ dbin(rateRecover, I[i-1]) # New recovers
# Transition process
S[i] <- S[i-1] - Inew[i] ; I[i] <- I[i-1] + Inew[i] - Rnew[i]; R[i] <- R[i-1] + Rnew[i]
# Probability to be infected
p[i] <- (I[i] + R[i])/ (I[i] + R[i] + S[i])
# Put in a logistic regression
Pos[i] ~ dbin(p[i], Tot[i])
}
# Low information prior for parameters
beta1 ~ dnorm(0,.0001); beta2 ~ dnorm(0,.0001); sigma ~ dnorm(8,0.0001)
}
The important part that I could not do the same thing in Stan is:
Inew[i] ~ dbin(rate[i], S[i-1]) # New infected
Rnew[i] ~ dbin(rateRecover, I[i-1]) # New recovers
I was thinking to do something like this for the model in Stan:
model {
real rateRecover;
real lambda[n];
real rate[n];
int S[n]; int Inew[n];
int I[n]; int Rnew[n];
int R[n]; real p[n];
real prob[n];
rateRecover <- 1-exp(-sigma);
S[1] <- S1; Inew[1] <- 0;
I[1] <- I1; Rnew[1] <- 0;
R[1] <- R1; p[1] <- (I1+R1)/(I1+R1+S1);
lambda[1] <- p1+p2*0.5;
rate[1] <- 1-exp(-lambda[1]);
for (i in 2:n) {
lambda[i] <- p1+p2*age2[i];
rate[i] <- 1 - exp(-lambda[i]);
Inew[i] ~ binomial(S[i-1] , rate[i]) ; # New infected
Rnew[i] ~ binomial(I[i-1] , rateRecover) ; # New recovers# Transition process
S[i] <- S[i-1] - Inew[i];
I[i] <- I[i-1] + Inew[i] - Rnew[i];
R[i] <- R[i-1] + Rnew[i] ; # Probability to be infected
p[i] <- (I[i] + R[i])/ (I[i] + R[i] + S[i]); # Put in a logistic regression
}
Pos2 ~ binomial(Tot2, p);
# Low information prior for parameters
p1 ~ normal(0,1000);
p2 ~ normal(0,1000);
sigma ~ normal(8,10) ; # Prior for recovery rates
# Duration in infected class
}
However, when I ran this model in Stan, I ran into the following error:
[459] " Error evaluating the log probability at the initial value."
[460] "Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:"
[461] "Exception thrown at line 39: stan::math::binomial_log: Probability parameter is -0.0480671, but must be between (0, 1)"
[462] "If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,"
[463] "but if this warning occurs often then your model may be either severely ill-conditioned or misspecified."
[464] "Rejecting initial value:"
[465] " Error evaluating the log probability at the initial value."
[466] "Rejecting initial value:"
[467] " Log probability evaluates to log(0), i.e. negative infinity."
[468] " Stan can't start sampling from this initial value."
[469] "Rejecting initial value:"
[470] " Log probability evaluates to log(0), i.e. negative infinity."
[471] " Stan can't start sampling from this initial value."
[472] "Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:"
I tried to specify "good" starting values but the sampling could not be initialized.
I also tried move verything else into the transformed parameters block like:
transformed parameters {
real<lower = 0, upper = 1> rateRecover;
real<lower = 0> lambda[n];
real<lower = 0, upper = 1> rate[n];
real<lower = 0, upper = 1> p[n];
rateRecover <- 1-exp(-sigma);
lambda[1] <- p1+p2*0.5;
rate[1] <- 1-exp(-lambda[1]);
{int S[n]; int Inew[n];
int I[n]; int Rnew[n];
int R[n];
S[1] <- S1; Inew[1] <- 0;
I[1] <- I1; Rnew[1] <- 0;
R[1] <- R1; p[1] <- (I1+R1)/(I1+R1+S1);
for (i in 2:n) {
lambda[i] <- p1+p2*age2[i];
rate[i] <- 1 - exp(-lambda[i]);
Inew[i] <- binomial_rng(S[i-1], rate[i]) ; # New infected
Rnew[i] <- binomial_rng(I[i-1] , rateRecover); # New recovers# Transition process
S[i] <- S[i-1] - Inew[i];
I[i] <- I[i-1] + Inew[i] - Rnew[i];
R[i] <- R[i-1] + Rnew[i] ; # Probability to be infected
p[i] <- (I[i] + R[i])/ (I[i] + R[i] + S[i]); # Put in a logistic regression
}
}
}
But now I got this error:
ERROR: random number generators only allowed in tranformed data block, generated quantities block or user-defined functions with names ending in _rng; found function=binomial_rng in block=transformed parameter
ERROR at line 35
33: lambda[i] <- p1+p2*age2[i];
34: rate[i] <- 1 - exp(-lambda[i]);
35: Inew[i] <- binomial_rng(S[i-1], rate[i]) ; # New infected
^Does any one know how I can improve what I was doing?
In the attachment is the model I have run.
Thank you a lot.
Thank you.