How do I create a simple single gap sequence?


I’m trying to create a simple sequence in Stan that would be done in R with a colon for something like 1:10. When I try the following code:

model {
  int Wgts[200];
  Wgts = 1:200;
} 

I get a dimension mismatch error. Is there a way to create this sequence in Stan? I’ve found the colon to be useful for indexing in Stan but perhaps it can’t be used for this purpose.

I might be wrong, but I don’t think that has been added to the language yet. For now you can do something like

int x[200];
for (i in 1:200) x[i] = i;

Also, if you’re not going to use them as indexes you can just make a vector or array of reals.

@Bob_Carpenter do you know if there are plans to add this or improve the error message? Right now the error message looks like this:

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Variable definition base type mismatch, 
variable declared as base type int[ ] variable definition has base type int
Variable definition dimensions mismatch, 
definition specifies 1, declaration specifies 0 

which I think is pretty confusing.

3 Likes

I often create such sequences in R if they are used for indexing. And then use them as input in the Stan data block.

2 Likes