Error with categorical distribution

I am using the categorical likelihood for the first time in Stan and it is giving me a very strange error/warning. I had to edit out a few things for privacy, but safe to say ptilde[n,] does sum to 1 for all n. I found one other question relating to this on here and it didn’t really seem to solve the issue I am dealing with.

data {
  int<lower=0> N ;
  int<lower=0> S ;
  int<lower=0> J ;
  int<lower=0,upper=1> Y[N,S+1] ;
  matrix[N,J] X ;
}
parameters {
  matrix[J,S] beta ;
  vector[S] beta0 ;
  vector[S] alpha0 ;
}
transformed parameters {
  matrix<lower=0,upper=1>[N,S+1] p ;
  matrix<lower=0,upper=1>[N,S+1] ptilde ;
  row_vector<lower=0>[S] p_tmp ;
  row_vector<lower=0>[S] ptilde_tmp ;
// Code here removed for privacy
}
model {
  
  for(s in 1:S){
    for(j in 1:J){
       target += normal_lpdf(beta[j,s] |0, 2.5) ;
    }
    target += normal_lpdf(beta0[s] |0, 5) ;
    target += normal_lpdf(alpha0[s] |-2.05, 3.19) ;
  }
  for(n in 1:N){
    target += categorical_lpmf( Y[n,] | to_vector(ptilde[n,])) ;
  }

}

SAMPLING FOR MODEL ‘code v2’ NOW (CHAIN 1).
Chain 1: Rejecting initial value:
Chain 1: Error evaluating the log probability at the initial value.
Chain 1: Exception: categorical_lpmf: element of outcome array is 0, but must be in the interval [1, 4] > (in ‘model2c04461d53_space_partitioning_SDM_v2’ at line 43)

Categorical expects int<lower=1,upper=S+1> Y[N]. You want

model {
  ...
    target += multinomial_lpmf( Y[n,] | to_vector(ptilde[n,])) ;
}
1 Like