Tanks a lot for help convert this BUGS code to stan code!!!
model{
for(i in 1:N){
# Data model (or likelihood) for the observed NPP data:
NPP[i] ~ dnorm(mu[i], tau)
# Generate “replicated data” to evaluate model fit.
NPP.rep[i] ~ dnorm(mu[i],tau)
# Define model for latent (mean) NPP; Event[,k] represents the amount
# of precipitation received in different size classes, where k indexes
# the even size class (k=1 for < 5 mm; k=2 for 5-15 mm; k=3 for 15
# 30 mm; k=4 for >30 mm); convert antecedent precipitation (antX) from
# inches to mm.
mu[i] <- a[1] + a[2]*antX[YearID[i]]*25.4 + a[3]*Event[i,1] + a[4]*Event[i,2] +
a[5]*Event[i,3] + a[6]*Event[i,4]
# Some of the precipitation event data are missing, so specify a simple
# data model for the Event data for the purpose of estimating the
# missing data:
for(k in 1:4){
Event[i,k] ~ dnorm(mu.ev[k], tau.ev[k])
}
}
# Dirichlet prior for monthly precipitation weights (due to restrictions
# on when the built-in dirichlet distribution can be used, we are required
# to use the relationship between the gamma distribution and the dirichlet
# to assign the dirichlet prior. For each time block into the past, assign
# the unnormalized weight (deltaX) a gamma(1,1) prior:
for(j in 1:Nblocks){
deltaX[j] ~ dgamma(1,1)
}
for(t in 1:Nlag){
# Compute the yearly weights:
yr.w[t] <- sum(weight[,t])
alphad[t] <- 1
for(m in 1:12){
# Redefine the unnormalized monthly weights to account for post-ANPP
# harvest period; i.e., 2nd part involving equals and step functions
# sets weight = 0 if in year 1 and in Oct, Nov, or Dec (i.e., post
# ANPP harvest).
delta[m,t] <- (deltaX[block[t,m]])*(1-equals(t,1)*step(m-9.5))
# Compute normalized monthly weights, which will be between 0 and 1,
# and will some to one.
weight[m,t] <- delta[m,t]/sumD
# Reorder the weights to go from most recent month (Dec of current
# year) to “oldest” month (Jan at past year = Nlag).
weightOrdered[(t-1)*12 + (12-m+1)] <- weight[m,t]
# For each time into the past, compute the weighted precipitation
# variable.
for(i in Nlag:Nyrs){
antX1[i,m,t] <- weight[m,t]*ppt[i-t+1,m]
}
}
}
# Compute sum of deltas (unnormalized weights), to be used to compute
# the normalized antecedent weights:
for(t in 1:Nlag){
sumD1[t] <- sum(delta[,t])
}
sumD <- sum(sumD1[])
# Compute the cumulative monthly weights:
for(t in 1:(12*Nlag)){
cum.weight[t] <- sum(weightOrdered[1:t])
}
# Compute the month within year weights (alpha’s = wP,m in Box 1 in main
# text); that is, these weights sum to 1 within each past year
for(m in 1:12){
for(t in 1:Nlag){
alpha[m,t] <- delta[m,t]/sum(delta[,t])
}
}
# Compute antecedent precipitation by summing the weighted precipitation
# variable over months and past years:
for(i in Nlag:Nyrs){
for(t in 1:Nlag){
ant.sum1[i,t] <- sum(antX1[i,,t])
}
antX[i] <- sum(ant.sum1[i,])
}
# Assign priors to the ANPP regression parameters (covariate effects):
for(k in 1:6){
a[k] ~ dnorm(0,0.0000001)
}
# Prior for residual (observation) standard deviation, and compute
# associated precision:
sig ~ dunif(0,100)
tau <- pow(sig,-2)
# Priors for parameters in the Event missing data model:
for(k in 1:4){
mu.ev[k] ~ dunif(0,500)
sig.ev[k] ~ dunif(0,500)
tau.ev[k] <- pow(sig.ev[k],-2)
}
}