0

I'm trying to pass a three-dimensional data structure to Stan (in RStan) where the entries must be integers, because a function down-stream requires that. However I'm having trouble declaring it.

I tried the straight-forward approach:

int x[n,n,k];

But that gave me the error

mismatch in number dimensions declared and found in context; ... dims declared=(n,n,k); dims found=(n*n*k)

meaning, clearly, the input array is getting flattened, for some reason (that I don't understand). I'm giving it a simple 3d array, no NAs, the dimensions look right before I pass it. And in fact, the same things is happening for 2d arrays, as well, meaning I can't even declare a set of 2d matrices, as a workaround.

Then I tried

row_vector[K] x[N,N];

but that gives back real, not int. And when I do something like

int row_vector[K] x[N,N];

that's just not proper syntax.

I also tried passing logical values, hoping they'd be re-cast as ints, but no. I passed arrays, I passed them cast with as.matrix, I checked their dimension both before and after being put into the data list.

This is with R version 3.4.1 on OSX 10.11.6, using the most recent version of stan, that was just compiled from source, today.

What am I missing? OR, how might I cast a single real to an integer, so that the integer-requiring function doesn't break?

(And, WHERE is the documentation? The best I can find is long-dead comment threads.)

one_observation
  • 454
  • 5
  • 16
  • How does what you are doing differ from: `N <- 10L; K <- 5L; x <- array(1:500, dim = c(N, N, K)); post <- stan("ints.stan", chains = 1, test_grad = TRUE)` where "ints.stan" is `data { int N; int K; int x[N, N, K]; }` `parameters { real theta; }` `model { target += normal_lpdf(theta | 0, 1); }` ? – Ben Goodrich Nov 19 '17 at 16:10
  • That ran fine, which is good to know. Mine still won't. I'm also (I'm just noticing) getting "no valid constructor available for the argument list" but all of the forums say that's benign, and shouldn't be causing a problem. – one_observation Nov 19 '17 at 19:34
  • It's because I was passing an array of logicals, and flattening appears to be a by-product of re-casting. When I tried to cast to integers myself, it was giving the same issue, but I couldn't tell that it was because I was causing it then. Don't know how I missed that. Should I delete this? – one_observation Nov 19 '17 at 19:45
  • The doc should probably say somewhere that array structures take dimensions in the order they are indexed. You didn't say which function you were trying to call, but the doc for input format is in the doc for the stan() call in RStan. It refers to a note at the end which specifies that input must be matrix or array and indicating how promotion works. The RStan vignette also points to this document and has examples. – Bob Carpenter Nov 21 '17 at 07:38

0 Answers0