How to pass a logical matrix to RStan

Published on

I ran into this problem with RStan today, and I’m sure I’ve had it at least once in the past, and it took me a ridiculously long time to figure out, so I’m documenting it here.

Suppose you need to pass a multidimensional logical array to RStan (events in the example below). The details of the model don’t matter; let’s say it’s a simple beta-binomial model:

The corresponding R code might look like this:

But this results in the following error:

Error in new_CppObject_xp(fields$.module, fields$.pointer, ...) : 
  Exception: mismatch in number dimensions declared and found in context; processing stage=data initialization; variable name=events; dims declared=(30,20); dims found=(600)  (in 'model3e955026c5b4_model' at line 4)
## failed to create the sampler; sampling not done

The important bits are declared=(30,20); dims found=(600). It looks like the events matrix got flattened… but why? We can double check that events is still a matrix of the correct dimensions:

## [1] "matrix"
## [1] 30 20

Here’s what’s going on. Because events in R is a logical (i.e. boolean) matrix, and events in Stan is an integer two-dimensional array, RStan tries to convert the former to the latter by calling as.integer. And indeed, we can check that as.integer flattens the matrix.

So how do you convert a logical matrix to an integer one? There is certainly more than one way to do it, but a quick and dirty solution, which I found in the archives of r-help, is to add a 0 to it. So: