cat("model{######Vague priors on first row and column#######for(y in 1:(Y+1)) {LogRecruits[y]~dnorm(10,.01); LogN[1,y]<-LogRecruits[y]; } for(a in 2:(A+1)){LogInitials[a-1]~dnorm(10,.01); LogN[a,1]<-LogInitials[a-1]; } ########Distribution for data, I[a,y]###### for (a in 1:A) { for (y in 1:Y) { MeanLogI[a,y]<-Logq[a]+(LogN[a,y]+10*LogN[a+1,y+1])/12; Prec[a,y]<-PrecLogI/a; I[a,y]~dlnorm(MeanLogI[a,y],Prec[a,y]); } } #####Prior on mortalities, hierarchical######NB: log(0.2)=-1.61 for(a in 1:A){ MeanLogAgeM[a]~dnorm(-1.21,1);} for(y in 1:Y){ MeanLogDeltaM[y]~dnorm(0,1);} PrecLogM~dgamma(1,1); for(a in 1:A) { for(y in 1:Y) { MeanLogM[a,y]<-MeanLogAgeM[a]+MeanLogDeltaM[y]; M[a,y]~dlnorm(MeanLogM[a,y],PrecLogM); } } #####Cohort equation##### for(a in 2:(A+1)) { for(y in 2:(Y+1)) { LogN[a,y]<-log(max(exp(LogN[a-1,y-1])*exp(-M[a-1,y-1])-C[a-1,y-1]*exp(-0.5*M[a-1,y-1]),10)); } } ######Prior on q's###### MeanLogq<--10; PrecLogq<-1; for (a in 1:A){ Logq[a]~dnorm(MeanLogq,PrecLogq); } ######Prior on q's, quadratic###### #gamma~dnorm(-8,1); #alpha~dunif(0.0001,0.02); #beta~dnorm(5,0.25); #for (a in 1:A){ MeanLogq[a]<-gamma-alpha*(a-beta)*(a-beta); # Logq[a]~dnorm(MeanLogq[a],16); } ######Prior on precision of log(I)###### PrecLogI~dgamma(10,1); ######Vector of 1983 numbers for monitoring##### for(a in 1:A) {N2001s[a]<-exp(LogN[a,6])/1000; } }", file="agestructure.jag")data { int<lower=0> A; int<lower=0> Y; matrix[A,Y] I; matrix[A,Y] C; } parameters { real<lower=-50,upper=50> LogN[A,Y]; real<lower=-50, upper=50> Logq[A]; real<lower=0, upper=2> M[A,Y]; real<lower=-50, upper=50> MeanLogAgeM[A]; real<lower=-50, upper=50> MeanLogDeltaM[Y]; real<lower=0, upper=10> PrecLogM; real<lower=0, upper=50> PrecLogI;
real<lower=0, upper=30> LogInitials[9]; real<lower=0, upper=30> LogRecruits[26]; }
model { PrecLogI ~ gamma(10,1);
for(a in 1:A){ MeanLogAgeM[a]~normal(-1.21,1);} for(y in 1:Y){ MeanLogDeltaM[y]~normal(0,1);} PrecLogM ~ gamma(1,1);
for(a in 2:(9)){LogInitials[a-1]~normal(10,.01);} for(y in 1:(26)){LogRecruits[y]~normal(10,.01);}
//prior on q's
{real MeanLogq; real PrecLogq; MeanLogq <- -10; PrecLogq <- 1;
for (a in 1:A) { Logq[a] ~ normal(MeanLogq, PrecLogq); } }
// state equations // ************** //mortality equation {real MeanLogM[A,Y];
for(a in 1:A) { for(y in 1:Y) { MeanLogM[a,y] <- MeanLogAgeM[a] + MeanLogDeltaM[y]; M[a,y] ~ lognormal(MeanLogM[a,y],PrecLogM); } } } // observation equation // ******************** //Distribution for data, I[a,y]
{ real MeanLogI[A,Y]; real Prec[A,Y];
for (a in 1:A) { for (y in 1:Y) { MeanLogI[a,y]<- Logq[a]+ (LogN[a,y]); Prec[a,y]<-PrecLogI/a; I[a,y] ~ lognormal(MeanLogI[a,y],Prec[a,y]); } } } }generated quantities { //cohort equation real N_log[9,26]; for(y in 1:(26)) { N_log[1,y]<-LogRecruits[y]; } for(a in 2:(9)){ N_log[a,1]<-LogInitials[a]; } for(a in 2:(9)) { for(y in 2:(26)) { N_log[a,y]<-log(fmax(exp(LogN[a-1,y-1])*exp(-M[a-1,y-1])-C[a-1,y-1]*exp(-0.5*M[a-1,y-1]),10)); } }} ########Distribution for data, I[a,y]###### for (a in 1:A) { for (y in 1:Y) { MeanLogI[a,y]<-Logq[a]+(LogN[a,y]+11*LogN[a+1,y+1])/12; Prec[a,y]<-PrecLogI/a; I[a,y] ~ lognormal(MeanLogI[a,y],Prec[a,y]); } }INDEX OPERATOR [] OUT OF BOUNDS; index=26; lower bound=1; upper bound=25; index position=2; LogN
# WinBUGS Code
########Distribution for data, I[a,y]###### for (a in 1:A) { for (y in 1:Y) { MeanLogI[a,y]<-Logq[a]+(LogN[a,y]+10*LogN[a+1,y+1])/12; Prec[a,y]<-PrecLogI/a; I[a,y]~dlnorm(MeanLogI[a,y],Prec[a,y]); } }
//STAN CODE (my first attempt which is wrong)
for (a in 1:A) { for (y in 1:Y) { MeanLogI[a,y]<-Logq[a]+(LogN[a,y]+10*LogN[a+1,y+1])/12; Prec[a,y]<-PrecLogI/a; I[a,y] ~ lognormal(MeanLogI[a,y],Prec[a,y]); } } //Distribution for data, I[a,y] { real MeanLogI[A,Y]; real Prec[A,Y]; for (a in 1:A-1) { for (y in 1:Y-1) { MeanLogI[a,y]<- Logq[a]+ (LogN[a,y]+11*LogN[a+1,y+1])/12; } } MeanLogI[8,25] <- Logq[8]+ LogN[8,25]; for (a in 1:A) { for (y in 1:Y) { Prec[a,y]<-PrecLogI/a; I[a,y] ~ lognormal(MeanLogI[a,y],Prec[a,y]); } } }
## Error "Rejecting proposed initial value with zero density."
--
You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
//Distribution for data, I[a,y]
for (a in 1:A) { I[a] ~ lognormal(Logq[a]+(LogN[a],PrecLogI/a); }
library(rstan)
sink("agestructure.stan")cat(" data { int<lower=0> A; int<lower=0> Y; real I[A,Y]; real C[A,Y]; } parameters { vector[Y] LogN[A]; vector[A] Logq; vector<lower=0, upper=10>[Y] M[A]; vector[A] MeanLogAgeM; vector[Y] MeanLogDeltaM; real PrecLogM; real PrecLogI; vector[A] LogInitials; vector[Y+1] LogRecruits; } model { PrecLogI ~ gamma(10,1); PrecLogM ~ gamma(1,1); MeanLogAgeM ~ normal(-1.61,1); MeanLogDeltaM ~ normal(0,1); LogRecruits ~ normal(10,10); Logq ~ normal(-10, 1); for (a in 2:A+1){ LogInitials[a-1] ~ normal(10,10); }
//Distribution for data, I[a,y] {real MeanLogI[A,Y]; MeanLogI[A,Y]<-Logq[8]+LogN[8,25]; I[8,25] ~ lognormal(MeanLogI[8,25],PrecLogI/8);
for (a in 1:A-1) { for (y in 1:Y-1) {
MeanLogI[a,y]<-Logq[a]+(LogN[a,y]+10*LogN[a+1,y+1])/12; I[a,y] ~ lognormal(MeanLogI[a,y],PrecLogI/a); } } }
//mortality equation for(a in 1:A) { M[a] ~ lognormal(MeanLogAgeM[a]+MeanLogDeltaM,PrecLogM); } }
generated quantities { //cohort equation real N_log[A+1,Y+1]; for(y in 1:Y+1) { N_log[1,y]<-LogRecruits[y]; } for(a in 2:A+1){ N_log[a,1]<-LogInitials[a-1]; } for(a in 2:A+1) { for(y in 2:Y+1) { N_log[a,y]<-log(fmax(exp(LogN[a-1,y-1])*exp(-M[a-1,y-1])-C[a-1,y-1]*exp(-0.5*M[a-1,y-1]),10)); } }} ", fill=TRUE) sink()Hi Allan,
What do you mean slow? Are the parameters mixing?
Are you getting reasonable inferences? There really isn't a point trying to make it fast if it's not correct.
Daniel
Hi Guys,Using your advice I got the model to work. I also forgot to change Normal and Log-Normal distributions since they are different specifications between JAGS and STAN.This model is running slow, so there is probably a better way to code it. See highlighted part which causes it to run slowly. If I use this:
//Distribution for data, I[a,y]{
for (a in 1:A) {
MeanLogI[a,y]<-Logq[a]+(LogN[a,y]+10*LogN[a+1,y+1])/12;
I[a] ~ lognormal(Logq[a]+(LogN[a],PrecLogI/a);}}}
it runs much faster, but I want the auto-correlation between years to help estimate the index (except for the last year).
library(rstan)sink("agestructure.stan")cat("
data {int<lower=0> A;int<lower=0> Y;
real I[A,Y];real C[A,Y];}parameters {vector[Y] LogN[A];vector[A] Logq;vector<lower=0, upper=10>[Y] M[A];vector[A] MeanLogAgeM;vector[Y] MeanLogDeltaM;real PrecLogM;real PrecLogI;vector[A] LogInitials;vector[Y+1] LogRecruits;}model {PrecLogI ~ gamma(10,1);PrecLogM ~ gamma(1,1);MeanLogAgeM ~ normal(-1.61,1);MeanLogDeltaM ~ normal(0,1);LogRecruits ~ normal(10,10);Logq ~ normal(-10, 1);for (a in 2:A+1){LogInitials[a-1] ~ normal(10,10);}
//Distribution for data, I[a,y]{real MeanLogI[A,Y];
MeanLogI[A,Y]<-Logq[8]+LogN[8,25];I[8,25] ~ lognormal(MeanLogI[8,25],PrecLogI/8);
for (a in 1:A-1) {for (y in 1:Y-1) {
MeanLogI[a,y]<-Logq[a]+(LogN[a,y]+10*LogN[a+1,y+1])/12;
I[a,y] ~ lognormal(MeanLogI[a,y],PrecLogI/a);}}}//mortality equationfor(a in 1:A) {M[a] ~ lognormal(MeanLogAgeM[a]+MeanLogDeltaM,PrecLogM);}}
generated quantities {//cohort equation
real N_log[A+1,Y+1];for(y in 1:Y+1) {
N_log[1,y]<-LogRecruits[y];}
for(a in 2:A+1){N_log[a,1]<-LogInitials[a-1];}for(a in 2:A+1) {for(y in 2:Y+1) {
N_log[a,y]<-log(fmax(exp(LogN[a-1,y-1])*exp(-M[a-1,y-1])-C[a-1,y-1]*exp(-0.5*M[a-1,y-1]),10));}}}
", fill=TRUE)sink()
Is there a better way to code the highlighted part?Cheers,
Allan
########Distribution for data, I[a,y]###### for (a in 1:A) { for (y in 1:Y) { MeanLogI[a,y]<-Logq[a]+(LogN[a,y]+10*LogN[a+1,y+1])/12; Prec[a,y]<-PrecLogI/a; I[a,y]~dlnorm(MeanLogI[a,y],Prec[a,y]); } }