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

Dear Stan community,

I have a problem fitting the bernoulli_logit model. I am trying to combine the linear predictors and inputting into a longitudinal binary model using bernoulli_logit. I have two matrices X (predictor) and Y (outcome), where the row represented subjects and column represented the visits. In the Y matrix, some cells are “NA” because for some subjects, they have missing outcomes at some visits, for others their outcome are 0/1.

I searched and found that the Y should be integer type using bernoulli_logit(). I am not sure how to indicate the matrix to be integer, or does the missing data affect?

Please see code below:

// Binary long model for PA+
  
  data {
    // Define variables in data
    // Number of observations (an integer)
    int<lower=0> M;
    // Number of c parameters
    int<lower=0> p;
    // Number of timepoints
    int k[M];

    // Covariates
    int N;
    matrix[M, N] X;
    
    // Count outcome
    matrix[M, N] Y;
  }
  
  parameters {
    // Define parameters to estimate
    real c0;
    real c[p];
    real sig[p];
    real cp;
    real s[M];
    real u[M];
    real ga;
  }
  
  transformed parameters  {
    //
    matrix[M, N] p2;
    for(i in 1:M){ 
         for(j in 1:k[i]){
             p2[i,j] <-  c0 + c[1] * (X[i,j]-cp) + c[2] * (X[i,j]-cp) * step(X[i,j]-cp) + ga*s[i] + u[i];
         } 
     }
  }
  
  model {
    // Prior part of Bayesian inference
    c0 ~ normal(0,10000);
    c[1] ~ normal(0,10000);
    c[2] ~ normal(0,10000);
    sig[1] ~ uniform(0,100);
    sig[2] ~ uniform(0,100);
    for(i in 1:M){ 
         u[i] ~ normal(0,sig[1]);
         s[i] ~ normal(0,sig[2]);
     }
    ga ~ normal(0,10000);
    cp ~ uniform(0.07,21.45);
    
   // Likelihood part of Bayesian inference
	Y ~ bernoulli_logit(p2);
  }

I got the error below:

Error in stanc(file = file, model_code = model_code, model_name = model_name,  : 
  0

Semantic error in 'string', line 43, column 1 to column 25:

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

After I change the matrix to vector using to_vector(), the error becomes,

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Info: assignment operator <- deprecated in the Stan language; use = instead.
No matches for: 

  vector ~ bernoulli_logit(vector)

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 'model1ea0b972b7c_50a1805149e3235b326959a4870349ac' at line 56, column 50
  -------------------------------------------------
    54:     
    55:    // Likelihood part of Bayesian inference
    56: 	to_vector(Y) ~ bernoulli_logit(to_vector(p2));
                                                         ^
    57:   }
  -------------------------------------------------

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

Thank you for your help!

Is that the full error message? The lpdf function does not accept matrices (besides the multivariate ones). You can fix this with

to_vector(Y) ~ bernoulli_logit(to_vector(p2));

I reinstall the package and rerun the file, now the error becomes,

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Info: assignment operator <- deprecated in the Stan language; use = instead.
No matches for: 

  vector ~ bernoulli_logit(vector)

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 'model1ea0b972b7c_50a1805149e3235b326959a4870349ac' at line 56, column 50
  -------------------------------------------------
    54:     
    55:    // Likelihood part of Bayesian inference
    56: 	to_vector(Y) ~ bernoulli_logit(to_vector(p2));
                                                         ^
    57:   }
  -------------------------------------------------

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

Do you know how can I convert the vector to integer?

You’ll need to declare Y as a two-dimensional array of integers:

data {
  ...
  array[M, N] Y;
}

Then because bernoulli_logit() is only defined for a single-dimensional array of integers as an outcome, you’ll then use a loop in your model {} block:

model {
  ...
  for (m in 1:M) {
    Y[m] ~ bernoulli_logit(p2[m]);
  }
}

If we declare Y` is a two-dimensional array of integers, why in the model we only use the single-dimensional array from 1:M? No need for N?

If we declare Y` is a two-dimensional array of integers, why in the model we only use the single-dimensional array from 1:M? No need for N?

Because it’s a two-dimensional array, when you use Y[m] you’re requesting all N elements from the m-th array. This is equivalent to how you would index rows in a matrix

Thank you for your response. There is something wrong with my rstan package and I reload it these days before I could try the code you mentioned above.

After removing and installing the package, I still have an error below, do you know what happened? Thanks!

> resStan <- stan_model(model_code = stan_code, data = data_stan,
+                 chains = 1, iter = 1000, warmup = 1000)
Error in stan_model(model_code = stan_code, data = data_stan, chains = 1,  : 
  unused arguments (data = data_stan, chains = 1, iter = 1000, warmup = 1000)

Use the stan() function, not stan_model()