Search code examples
rstan

How to pass a logical R object to a data block in a Stan file


If a list of data includes a logical variable x, namely, x=TRUE or x=FALSE. Then how to declare such a variable in a Stan file?

For example, if an R object x is an integer, then

data{

int <lower=0>x;

}

I want to know its logical version. I guess

data{

               bool x;

    }

and it does not work as following;

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
 error in 'foo' at line 16, column 0
  -------------------------------------------------
    14:   
    15: 
    16: bool x;
       ^
    17: 
  -------------------------------------------------

PARSER EXPECTED: <one of the following:
  a variable declaration, beginning with type,
      (int, real, vector, row_vector, matrix, unit_vector,
       simplex, ordered, positive_ordered,
       corr_matrix, cov_matrix,
       cholesky_corr, cholesky_cov
  or '}' to close variable declarations>
Error in stanc(filename, allow_undefined = TRUE) : 
  failed to parse Stan model 'foo' due to the above error.

Solution

  • I believe logicals are resolved to their integer values of 0L for FALSE and 1L for TRUE, so using int is appropriate.