data {
  int<lower=0> N;
  int<lower=0> nit;
  matrix[N,nit] z;

  real<lower=0> sT;
  real<lower=0> sA;
  real<lower=0> sB;
  real<lower=0> sP;

}
parameters {
  vector[N] theta;
  vector[nit] beta;
  vector[nit] ln_phi;
  vector[nit] ln_alpha;
}
transformed parameters{
  vector<lower=0>[nit] alpha = exp(ln_alpha);
  vector<lower=0>[nit] phi = exp(ln_phi);
}
model {
  beta~normal(0,sB) ;
  ln_phi~normal(0,sP);
  ln_alpha~normal(0,sA);
  theta~normal(0,sT);

     for(p in 1:N)
      for(i in 1:nit)
        if(z[p,i]!=-999) target+=normal_lpdf(z[p,i] | beta[i]+alpha[i]*theta[p],sqrt(phi[i]));

}

