Random variable is nan: Poisson-gamma model for grouped data

Variables in Stan are default NaNs unless you set them to anything else, so in the block:

model {
  matrix[I,J] lambda; // <-- lambda will be NaNs unless you set them equal to something
  ...
}

target +=gamma_lpdf(lambda[i,j]|alpha[j],beta[j]); or equivalently lambda[i, j] ~ gamma(alpha[j],beta[j]); don’t do any assignment to lambda.

Do you mean for lambda to be a parameter that gets sampled? If so, define it in the parameters block.