model{
    for(i in 1:nmuni){
        for(j in 1:nperiods){
            Obs[j,i]~dpois(mu[j,i])
#Modelling of the mean for every municipality and period
            log(mu[j,i])<-log(Exp[j,i])+mediainter+inter[j]+theta.ST[j,i]
#SMR for every municipality and period
            SMR[i,j]<-100*exp(mediainter+inter[j]+theta.ST[j,i])
#Contribution of the i-th municipality in the j-th period to the deviance
            D.ij[j,i]<-Obs[j,i]*log(mu[j,i])-mu[j,i]-(Obs[j,i]*log(Obs[j,i])-Obs[j,i])
        }
#Contribution of the i-th municipality to the deviance
        D.i[i]<-sum(D.ij[,i])
    }
#Deviance
    D<- -2*sum(D.i[])

#Spatio-temporal effect for the first period
    theta.S[1,1:nmuni]~car.normal(map[],w[],nvec[],prec.spat)
    for(i in 1:nmuni){
      theta.ST[1,i]~dnorm(theta.S[1,i],prec.het)
    }
#Spatio-temporal effect for the subsequent periods
    for(j in 2:nperiods){
        for(i in 1:nmuni){
            theta.ST[j,i]~dnorm(theta.S[j,i],prec.het)
        }
        theta.S[j,1:nmuni]~car.normal(map[],w[],nvec[],prec.spat)
    }

#Prior distribution for the mean risk for every municipality and period
    mediainter~dnorm(0,0.01)
#Prior distribution for the global time trend
    inter[1:nperiods]~car.normal(mapT[],wT[],nvecT[],prec.inter)
#Prior distribution for the precision parameters in the model
    prec.inter~dgamma(0.5,0.005)
    prec.het~dgamma(0.5,0.005)  
    prec.spat~dgamma(0.5,0.005)
}