Parser doesn't recognise array[N] x;

Hi all,

This morning I encountered an odd problem. When trying to run the GP example straight out of the manual (10.3 Fitting a Gaussian process | Stan User’s Guide), I run into trouble with the compiler.


data {
  int<lower=1> N;
  array[N] real x;
  vector[N] y;
transformed data {
  vector[N] mu = rep_vector(0, N);
parameters {
  real<lower=0> rho;
  real<lower=0> alpha;
  real<lower=0> sigma;
model {
  matrix[N, N] L_K;
  matrix[N, N] K = cov_exp_quad(x, alpha, rho);
  real sq_sigma = square(sigma);

  // diagonal elements
  for (n in 1:N) {
    K[n, n] = K[n, n] + sq_sigma;

  L_K = cholesky_decompose(K);

  rho ~ inv_gamma(5, 5);
  alpha ~ std_normal();
  sigma ~ std_normal();

  y ~ multi_normal_cholesky(mu, L_K) T[0, 1];

The error:

> stan("models/gp", chains = 1, iter = 100)
 error in 'modelfd8a6dfc59a6_gp_truncated_normal' at line 4, column 2
     2:   int<lower=1> N;
     3:   vector[N] y;
     4:   array[N] real x;
     5: }

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>

In the script editor pane, the word array shows as black text (unlike i.a. vector) suggesting that it’s not recognised:

I just followed the steps in the RStan manual to reinstall RStan and it works (and also clearly the compiler is called):

> sessionInfo("rstan")
R version 4.1.2 (2021-11-01)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.7

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:

other attached packages:
[1] rstan_2.21.3

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.8           lubridate_1.8.0      tidyr_1.1.4          prettyunits_1.1.1   
 [5] ps_1.6.0             assertthat_0.2.1     digest_0.6.29        utf8_1.2.2          
 [9] R6_2.5.1             cellranger_1.1.0     backports_1.4.1      reprex_2.0.1        
[13] stats4_4.1.2         evaluate_0.14        httr_1.4.2           ggplot2_3.3.5       
[17] pillar_1.7.0         utils_4.1.2          rlang_1.0.0          readxl_1.3.1        
[21] rstudioapi_0.13      callr_3.7.0          rmarkdown_2.11       stringr_1.4.0       
[25] readr_2.1.2          loo_2.4.1            munsell_0.5.0        broom_0.7.12        
[29] compiler_4.1.2       modelr_0.1.8         xfun_0.29            pkgconfig_2.0.3     
[33] stats_4.1.2          pkgbuild_1.3.1       htmltools_0.5.2      tidyselect_1.1.1    
[37] tibble_3.1.6         gridExtra_2.3        codetools_0.2-18     matrixStats_0.61.0  
[41] grDevices_4.1.2      fansi_1.0.2          crayon_1.4.2         dplyr_1.0.7         
[45] tzdb_0.2.0           dbplyr_2.1.1         withr_2.4.3          grid_4.1.2          
[49] jsonlite_1.7.3       gtable_0.3.0         lifecycle_1.0.1      DBI_1.1.2           
[53] magrittr_2.0.2       datasets_4.1.2       StanHeaders_2.21.0-7 scales_1.1.1        
[57] RcppParallel_5.1.5   stringi_1.7.6        cli_3.1.1            fs_1.5.2            
[61] tidyverse_1.3.1      xml2_1.3.3           ellipsis_0.3.2       graphics_4.1.2      
[65] generics_0.1.2       vctrs_0.3.8          base_4.1.2           tools_4.1.2         
[69] forcats_0.5.1        glue_1.6.1           purrr_0.3.4          hms_1.1.1           
[73] processx_3.5.2       parallel_4.1.2       fastmap_1.1.0        yaml_2.2.2          
[77] inline_0.3.19        colorspace_2.0-2     rvest_1.0.2          knitr_1.37          
[81] haven_2.4.3          patchwork_1.1.1      methods_4.1.2   

Does anyone have a good idea as to why this happens? I have a nasty feeling that I’m missing something obvious.


1 Like

This because the Stan version included in RStan is a little out of date, and does not yet support the new syntax. You can either install the preview of the next rstan version using:

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

Alternatively, you can just use the old array syntax if you don’t want to upgrade:

real x[N];

The third option is to look at the older version of the Users guide (for 2.21): 10.3 Fitting a Gaussian Process | Stan User’s Guide


Another option is to use CmdStanR to run Stan from R instead of RStan. That would give you access to the latest syntax.

1 Like

Thanks a bunch, guys! As I said, something obvious and all good fixes. Will just stick with the old syntax for now as I’m in the early trial-and-error phase anyway. Cheers!