title: JAGS: unit-specific time trends
body: <p>Using JAGS I am trying to estimate a model including a unit-specific time trend.
  However, the problem is that I don't know how to model this and so far I have been unable to find a solution. </p>
  
  <p>As an example, consider we have the following data:</p>
  
  <pre><code>rain&lt;-rnorm(200)     # Explanatory variable
  n1&lt;-rnorm(200)       # Some noise
  gdp&lt;-rain+n1         # Outcome variable
  ccode&lt;-rep(1:10,20)  # Unit codes
  year&lt;-rep(1:20,10)   # Years 
  </code></pre>
  
  <p>Using normal linear regression, we would estimate the model as:</p>
  
  <pre><code>m1&lt;-lm(gdp~rain+factor(ccode)*year)
  </code></pre>
  
  <p>Where <code>factor(ccode)*year</code> is the unit-specific time trend. Now I want to estimate the model using JAGS. So I create parameters for the indexing:</p>
  
  <pre><code>N&lt;-200
  J&lt;-max(ccode) 
  T&lt;-max(year)
  </code></pre>
  
  <p>And estimate the model,</p>
  
  <pre><code>library(R2jags)
  library(rjags)
  
  set.seed(42); runif(1)
  dat&lt;-list(gdp=gdp,
        rain=rain,
        ccode=ccode,
        year=year,
        N=N,J=J,T=T)
  
  parameters&lt;-c("b1","b0")
  model.file &lt;- "~/model.txt"
  system.time(m1&lt;-jags(data=dat,inits=NULL,parameters.to.save=parameters,
          model.file=model.file,
          n.chains=4,n.iter=500,n.burnin=125,n.thin=2))
  </code></pre>
  
  <p>with the following model file, and this is where the error is at the moment:</p>
  
  <pre><code># Simple model 
  
  model {
    # For N observations
    for(i in 1:N) {
      gdp[i] ~ dnorm(yhat[i], tau)
      yhat[i] &lt;- b1*rain[i] + b0[ccode[i]*year[i]]   
    }
  
    for(t in 1:T) {
      for(j in 1:J) {
        b0[t,j] ~ dnorm(0, .01)
      }
    }
    # Priors
    b1 ~ dnorm(0, .01)   
  
    # Hyperpriors
    tau &lt;- pow(sd, -2)
    sd ~ dunif(0,20)  
  }
  </code></pre>
  
  <p>I am fairly sure that the way in which I define <code>b0</code> and the indexing is incorrect given the error I get when using the code: <code>Compilation error on line 7. Dimension mismatch taking subset of b0</code>. 
  However, I don't know how to solve this so I wondered whether someone here has suggestions about this?</p>