data{ int N; //number of nests int cens[N]; //id for censored nests "1", and fully decayed nests "" int days_to_event[N]; //days until full decay (or until last observation day if censored) int F [N]; //forest type int E [N]; //nest exposure int P [N]; //nest position int C [N]; //construction type int H [N]; //nest realtive height int W [N]; //average rainfall int S [N]; //average number of stroms int D [N]; //differential temperature at construction int R [N]; //rain at construction int SP[N]; //tree species } parameters{ //////////Define factor specific parameters, and number of levels within each vector[2] f; //forest type vector[2] e; //nest exposure vector[2] p; //nest position vector[2] c; //nest construction type vector[3] h; //nest realtie height vector[3] w; //average rainfall vector[3] s; //average number of storms vector[3] d; //differential temperature at construction vector[2] r; //rain at construction vector[11] sp; //tree species real theta; //rate parameter, has to be positive } transformed parameters{ vector[N] mu; //defines decay time of each nest, has to be positive for ( i in 1:N ) { mu[i] = f[F[i]]+e[E[i]]+p[P[i]]+c[C[i]]+h[H[i]]+w[W[i]]+s[S[i]]+d[D[i]]+r[R[i]]+sp[SP[i]]; //linear model mu[i] = exp(mu[i]); //link function : exp of mu. mu has to be positive } } model{ /////////////Define priors for parameters, on the log scale f ~ normal(0,5); e ~ normal(0,5); p ~ normal(0,5); c ~ normal(0,5); h ~ normal(0,5); w ~ normal(0,5); d ~ normal(0,5); r ~ normal(0,5); s ~ normal(0,5); sp ~ normal(0,5); theta ~ gamma (0.1,0.1); for ( i in 1:N ) if ( cens[i] == 1 ) target += gamma_lccdf(days_to_event[i] | mu[i] * theta , theta);//censored nests, modelled using complementary cumulative gamma distribution for ( i in 1:N ) if ( cens[i] == 0 ) days_to_event[i] ~ gamma( mu[i] * theta, theta); // fully decayed nests, modelled using gamma distribution } generated quantities{ /////////////////Define quantities to be calculated real Decay; //Average decay time ////////////Define main effects //Forest type matrix [N,2] D_F; vector [2] Dec_F; //Exposure matrix [N,2] D_E; vector [2] Dec_E; //Position matrix [N,2] D_P; vector [2] Dec_P; //Constructon type matrix [N,2] D_C; vector [2] Dec_C; //Relative height matrix [N,3] D_H; vector [3] Dec_H; //Average rain matrix [N,3] D_W; vector [3] Dec_W; //Average storms matrix [N,3] D_S; vector [3] Dec_S; //DIfferential temperature matrix [N,3] D_T; vector [3] Dec_T; //Rain at construction matrix [N,2] D_R; vector [2] Dec_R; //Tree species matrix[N,11] D_SP; vector[11] Dec_SP; /////////////////Calculate main effects for (i in 1:N){ for(j in 1:2){ D_F[i,j] = exp(e[E[i]] + p[P[i]] + m[M[i]] + dr[DR[i]] + ht[HT[i]] + r[R[i]] + s[S[i]] + mt[MT[i]] + sp[SP[i]] + f[j]); D_E[i,j] = exp(f[F[i]] + p[P[i]] + m[M[i]] + dr[DR[i]] + ht[HT[i]] + r[R[i]] + s[S[i]] + mt[MT[i]] + sp[SP[i]] + e[j]); D_P [i,j] = exp(f[F[i]] + e[E[i]] + m[M[i]] + dr[DR[i]] + ht[HT[i]] + r[R[i]] + s[S[i]] + mt[MT[i]] + sp[SP[i]] + p[j]); D_M [i,j] = exp(f[F[i]] + e[E[i]] + p[M[i]] + dr[DR[i]] + ht[HT[i]] + r[R[i]] + s[S[i]] + mt[MT[i]] + sp[SP[i]] + m[j]); D_DR[i,j] = exp(f[F[i]] + e[E[i]] + m[M[i]] + p[P[i]] + ht[HT[i]] + r[R[i]] + s[S[i]] + mt[MT[i]] + sp[SP[i]] + dr[j]); } for(j in 1:3){ D_HT[i,j] = exp(e[E[i]] + p[P[i]] + m[M[i]] + dr[DR[i]] + f[F[i]] + r[R[i]] + s[S[i]] + mt[MT[i]] + sp[SP[i]] + ht[j]); D_R [i,j] = exp(e[E[i]] + p[P[i]] + m[M[i]] + dr[DR[i]] + f[F[i]] + ht[HT[i]] + s[S[i]] + mt[MT[i]] + sp[SP[i]] + r[j]); D_S [i,j] = exp(e[E[i]] + p[P[i]] + m[M[i]] + dr[DR[i]] + f[F[i]] + r[R[i]] + ht[HT[i]] + mt[MT[i]] + sp[SP[i]] + s[j]); D_MT[i,j] = exp(e[E[i]] + p[P[i]] + m[M[i]] + dr[DR[i]] + f[F[i]] + r[R[i]] + s[S[i]] + ht[HT[i]] + sp[SP[i]] + mt[j]); } for(j in 1:11){ D_SP[i,j] = exp(e[E[i]] + p[P[i]] + m[M[i]] + dr[DR[i]] + ht[HT[i]] + r[R[i]] + s[S[i]] + mt[MT[i]] + f[F[i]] + sp[j]); } } ////////////Calculate mean decay time for each category for(j in 1:2){ Dec_F[j] = mean(D_F[,j]); Dec_E[j] = mean(D_E[,j]); Dec_P [j] = mean(D_P[,j]); Dec_M [j] = mean(D_C[,j]); Dec_DR[j] = mean(D_R[,j]); } for(j in 1:3){ Dec_HT[j] = mean(D_HT[,j]); Dec_R [j] = mean(D_W[,j]); Dec_S [j] = mean(D_S[,j]); Dec_MT[j] = mean(D_T[,j]); } for (j in 1:11){ Dec_SP[j] = mean(D_SP[,j]); } ////////////Calculate overall mean decay time Decay = mean(mu); }