Soft k-means model syntax error


I’m trying to work out how to use Stan for time series clustering. I thought I would start with the soft k-means example in the User guide and github.

However it seems that array is not a valid syntax to declare arrays on line 5. Has the syntax changed since this example was written? What would be the correct syntax?

data {
  int<lower=0> N;        // number of data points
  int<lower=1> D;        // number of dimensions
  int<lower=1> K;        // number of clusters
  array[N] vector[D] y;  // observations
transformed data {
  real<upper=0> neg_log_K;
  neg_log_K = -log(K);
parameters {
  array[K] vector[D] mu; // cluster means
transformed parameters {
  array[N, K] real<upper=0> soft_z; // log unnormalized clusters
  for (n in 1:N) {
    for (k in 1:K) {
      soft_z[n, k] = neg_log_K - 0.5 * dot_self(mu[k] - y[n]);
model {
  // prior
  for (k in 1:K) {
    mu[k] ~ std_normal();

  // likelihood
  for (n in 1:N) {
    target += log_sum_exp(soft_z[n]);

Thanks in advance,


I’m guessing that you’re using rstan? If so, the included Stan version is slightly outdated and so the array syntax is not compatible.

To fix this, you can install the preview of the next version:

remove.packages(c("StanHeaders", "rstan"))
install.packages("StanHeaders", repos = c("", getOption("repos")))
install.packages("rstan", repos = c("", getOption("repos")))

thanks for the reply - so I’m using rstan 2.21.2 and StanHeaders 2.21.0-7

I must admit I shudder at the prospect of installing Stan again. It was a bit of a drama last time and I’m in the middle of a few projects right now which I don’t want to break, so I’ll have to think about it…

If you don’t want to upgrade, then you can also just view the documentation that aligns with your Stan version. You just need to change the version in the URL:

Which you would change to 2_21

1 Like