Ill-typed arguments to '~' statement. No distribution 'poisson' was found with the correct signature

Hey everyone!

I’m trying to perform the NUTS with Stan, but I keep repeatedly getting the following error when I try to use stan_model():

rstan:::rstudio_stanc(“scriptStan_tp2.stan”)
Error in stanc(filename, allow_undefined = TRUE) : 0

Semantic error in ‘string’, line 50, column 6 to column 40:

Ill-typed arguments to ‘~’ statement. No distribution ‘poisson’ was found with the correct signature.

I’ve tried multiple things, checked my data, but everything seems correct. Could anyone please help me?

Thats my code:

// data block
data {
  int<lower=1> n;
  int<lower=1> t;
  vector[n] E;
  matrix[n,t] X1;
  matrix[n,t] X2;
  matrix[n,n] Sig_delta;
  matrix[t,t] Sig_gamma;
  real m0;
  real m1;
  real m2;
  real <lower=0> v0;
  real <lower=0> v1;
  real <lower=0> v2;
  real <lower=0> a_phi;
  real <lower=0> b_phi;
  real <lower=0> a_delta;
  real <lower=0> b_delta;
  real <lower=0> a_gamma;
  real <lower=0> b_gamma;
}

// parameters block
parameters {
  real beta0;
  real beta1;
  real beta2;
  real<lower=0> tau_phi;
  real<lower=0> tau_delta;
  real<lower=0> tau_gamma;
  matrix[n, t] phi;
  vector[n] delta;
  vector[t] gamma;
}

// transformed parameters block
transformed parameters{
  matrix[n, t] theta;
  for(i in 1:n){
    for(j in 1:t){
      theta[i, j] = exp(beta0 + beta1*X1[i,j] + beta2*X2[i,j] + phi[i,j] + delta[i] + gamma[j]);
    }
  }
}

// model block
model {
  matrix[n, t] Y;
  // likelihood
  for(i in 1:n){
    for(j in 1:t){
      Y[i,j] ~ poisson(theta[i,j]*E[i]);
      phi[i,j] ~ normal(0, sqrt(tau_phi));
    }
    
  }
  
  // priors
  delta ~ multi_normal(rep_vector(0,n), tau_delta*Sig_delta);
  gamma ~ multi_normal(rep_vector(0,t), tau_gamma*Sig_gamma);
  tau_phi ~ gamma(a_phi, b_phi);
  tau_delta ~ gamma(a_delta, b_delta);
  tau_gamma ~ gamma(a_gamma, b_gamma);
  beta0 ~ normal(m0, sqrt(v0));
  beta1 ~ normal(m1, sqrt(v1));
  beta2 ~ normal(m2, sqrt(v2));
}

Thank you very much!

You have Y as a matrix (of reals) with nothing in it. The sampling statement Y[i,j] ~ poisson(theta[i,j]*E[i]); needs Y to be an array of integers defined either as data or as transformed data.

1 Like

Thanks for your answer @jsocolar! But I’m afraid I did not understand how to fix it. Do you mean I should declare Y again as transformed data? Could you please detail more? I really appreciate any help you can provide.

What is Y supposed to be in this model? Where does it come from? What values does it have?

NUTS cannot sample discrete parameters; Poisson is a discrete distribution. Depending on what you’re doing you need to write this as either

data {
  int Y[n, t];
  ...
}
...
model {
  for(i in 1:n){
    for(j in 1:t){
      Y[i,j] ~ poisson(theta[i,j]*E[i]);
      phi[i,j] ~ normal(0, sqrt(tau_phi));
    }
  }

or

model {
  for(i in 1:n){
    for(j in 1:t){
      phi[i,j] ~ normal(0, sqrt(tau_phi));
    }
  }
  ...
}
generated quantities {
  int Y[n, t];
  for(i in 1:n){
    for(j in 1:t){
      Y[i,j] = poisson_rng(theta[i,j]*E[i]);
    }
  }
}
2 Likes

Thanks for your answer @nhuurre! It helped me a lot and solved the problem!

Hey @jsocolar! Y is the response matrix nxt having the observed number of infections in location n and time t, so it is made of integer values only!

You need to pass the data Y to the model. Your data block never declares Y. You just declare it as a local variable in the model block, and moreover, you never put any values in it. Finally, when you declare it (presumably in the data block), you need to declare it as an array of integers, and not as a matrix.

1 Like