Ragged array example in user's guide is wrong?

Hello,

I was unable to compile the example code in section 8.2 “Ragged Data Structures” of the Stan User’s Guide (version 2.28). Link here: https://mc-stan.org/docs/2_28/stan-users-guide/ragged-data-structs.html#ragged-data-structs.section

The guide suggests the following (I’ve added a missing curly brace at the end of the model section):

Original Documentation (plus final curly brace)

data {
  int<lower=0> N;   // # observations
  int<lower=0> K;   // # of groups
  vector[N] y;      // observations
  array[K] int s;   // group sizes
  // ...
}
model {
  int pos;
  pos = 1;
  for (k in 1:K) {
    segment(y, pos, s[k]) ~ normal(mu[k], sigma);
    pos = pos + s[k];
  }
}   

However, this does not compile in my version of rstan (2.21.2). I am running R version 3.6.3 (2020-02-29) on gnu/linux. Instead, the compiler flags the array[K] int s; declaration:

  SYNTAX ERROR, MESSAGE(S) FROM PARSER:
   error in 'model125e6c2f14fb_ragged_group_sizes' at line 5, column 2
    -------------------------------------------------
       3:   int<lower=0> K;   // # of groups
       4:   vector[N] y;      // observations
       5:   array[K] int s;   // group sizes
           ^
       6:   // ...
    -------------------------------------------------
  
  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(file = file, model_code = model_code, model_name = model_name,  : 
    failed to parse Stan model 'ragged_group_sizes' due to the above error.

Looking through the forums, I didn’t see any posts directly addressing this issue, however, I saw that declaring s as int s[K] rather than an as an array worked for others, as follows:

Revised to Remove Array Declaration

data {
  int<lower=0> N;   // # observations
  int<lower=0> K;   // # of groups
  vector[N] y;      // observations
  int s[K];  // group sizes
  // ...
}
model {
  int pos;
  pos = 1;
  for (k in 1:K) {
    segment(y, pos, s[k]) ~ normal(mu[k], sigma);
    pos = pos + s[k];
  }
}

However, this model still doesn’t compile. Experienced stan model coders probably recognize that mu and sigma need to be declared, and could be declared as variables in the model block or as parameters, but for stan neophytes like myself, including explicit declarations is helpful, even if the code is no longer a minimal example:

**Revised to Include Parameters and Priors **

data {
  int<lower=0> N;   // # observations
  int<lower=0> K;   // # of groups
  vector[N] y;      // observations
  //array[K] int s;   // group sizes
  int s[K];
  // ...
}
parameters {
  vector[K] mu;
  vector<lower=0>[K] sigma;
}
model {
  int pos;
  pos = 1;
  for (k in 1:K) {
    segment(y, pos, s[k]) ~ normal(mu[k], sigma[k]);
    pos = pos + s[k];
  }
  sigma ~ exponential(1);
  mu ~ normal(0, 10);
}

Is my version of rstan and stan just too old? Is the array[K] int s going to work in future versions of stan? Is there something else I am missing?

The array syntax works with newer versions of Stan, its just that the CRAN version of rstan has not caught up yet. The latest version of Stan is 2.28, which can be used with cmdstanr, cmdstanpy, cmdstan and pystan3 interfaces. For rstan, you can use our Stan R package repository to install a version that supports Stan 2.26.

You can do that by running:

remove.packages(c("StanHeaders", "rstan"))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))

You can also access an older version of the Users guide that works for the 2.21 Stan version you are using this link: Stan User’s Guide (just search for ragged data structures then).

Great, thank you. I wasn’t able to find an explicit reference in the rstan documentation as to exactly which version of stan it installed…it could be that the rstan version numbers are maybe meant to track stan versions. The StanHeaders package is version is version 2.21.0-7. Still, neither of those is a guarantee that stan itself is 2.21.

And it looks like previous documentation is at

    https://mc-stan.org/docs/*<version_point number>*,

even though going to https://mc-stan.org/docs/ doesn’t list previous versions, but instead redirects to the documentation main page.

The rstan version matches the Stan version is uses, so you are using 2.21.

As for the URL, you need to specify that you want the User guide, so :

https://mc-stan.org/docs/* stan version number with underscores */stan-users-guide/

So for example:

https://mc-stan.org/docs/2_21/stan-users-guide/