Ragged Dataset - segment function causing bernoulli_logit syntax error

Hi all,

I have been facing the issue of having NA’s in my detection data for my occupancy model. I thought using the segment function might be a way to work around the NA’s. However, I am getting an error that I can’t figure out. Before making my model more complex I started out with the bare minimum. This model is a very simple occupancy model - one species, one year with one occupancy and detection covariate.

data {
  int<lower=0> N; // # of observations/ # temporal replication
  int<lower=0> K; // # of groups/ # of trapping arrays
  int pos[N]; // position of the first detection of each group
  vector[N] y; // observations/detection at each site for all temporal replication
  int s[K]; // group size s={45,42,42,41,54,...}
  int x[K]; // Observed occupancy at each site

  vector[K] t_burn;
  vector[N] air_temp; 
}


parameters {
  real a_psi;
  real b1_psi;
  
  real a_p; 
  real b1_p;
  
}

transformed parameters {
  vector[N] logit_psi;
  vector[N] logit_p;
  
  logit_psi = a_psi + b1_psi * t_burn;
  logit_p = a_p + b1_p * air_temp;
}
model {

  // Priors
  a_psi ~ normal(0, 1); //  (mu, sigma)
  b1_psi ~ normal(0,1); 

  a_p ~ normal(0, 1);
  b1_p ~ normal(0, 1);

// Likelihood

  for (i in 1:K) {
      if (x[i] == 1) {    // Occurred and observed
      1 ~ bernoulli_logit(logit_psi[i]);
      segment(y[i], pos, s[i]) ~ bernoulli_logit(logit_p[i]); 
    
    } else {
                            // Occurred and not observed
      target += log_sum_exp(bernoulli_logit_lpmf(1 | logit_psi[i])
                            + bernoulli_logit_lpmf(0 | logit_p[i]),
                            // Not occurred
                            bernoulli_logit_lpmf(0 | logit_psi[i]));
    }
  }
}

I am getting a syntax error for the line in my model containing the segment function. I am confused on why I am getting a syntax error for ~bernoulli_logit(logit_p[i]) but not for ~bernoulli_logit(psi[i]). Here is the error.

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
No matches for: 

  vector ~ bernoulli_logit(real)

Available argument signatures for bernoulli_logit:

  int ~ bernoulli_logit(real)
  int ~ bernoulli_logit(real[ ])
  int ~ bernoulli_logit(vector)
  int ~ bernoulli_logit(row_vector)
  int[ ] ~ bernoulli_logit(real)
  int[ ] ~ bernoulli_logit(real[ ])
  int[ ] ~ bernoulli_logit(vector)
  int[ ] ~ bernoulli_logit(row_vector)

Real return type required for probability function.
 error in 'model141f47a60175_occu_ragged' at line 45, column 61
  -------------------------------------------------
    43:       if (x[i] == 1) {    // Occurred and observed
    44:       1 ~ bernoulli_logit(logit_psi[i]);
    45:       segment(y, pos[i], s[i]) ~ bernoulli_logit(logit_p[i]); 
                                                                                            ^
    46: 
  -------------------------------------------------

Error in stanc(file = file, model_code = model_code, model_name = model_name,  : 
  failed to parse Stan model 'occu_ragged' due to the above error.

Thank you in advance!

Hi Kristina,

The issue is actually being caused by your declaration of y in the data block:

vector[N] y;

The bernoulli_* distributions require an integer outcome, whereas the vector/matrix types are assumed to contain reals. Try changing your specification to:

int y[N];
1 Like

Thank you! It worked.

2 Likes