Bug in rstan

Hi all. I found a bug in rstan. When my stan code has a typo in it, stan() screws up. Instead of returning an error, it just returns a blank prompt and then puts R in a bad state where it ignores errors. See code below. I’m using R 3.6.1 run from Rstudio (but I get the same problem in regular R console), rstan 2.19.2, macOS Catalina 10.15

R script:

library("rstan")

data <- list(y=1)
fit_1 <- stan("test.stan", data=data)   # This works fine
 
fit_2 <- stan("test2.stan", data=data)  # This (a) returns no error and (b) screws up R
## For example:
dssdksdl  # This does not return an error

Stan program test.stan:

  real y;
}
parameters {
  real mu;
}
model {
  y ~ normal(mu, 1);
}

Stan program test2.stan:

data {
  xxreal y;
}
parameters {
  real mu;
}
model {
  y ~ normal(mu, 1);
}

This is what happens when I run it:

R version 3.6.1 (2019-07-05) -- "Action of the Toes"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin15.6.0 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> setwd("~/AndrewFiles/research/stan/collinearity")
> library("rstan")
Loading required package: StanHeaders
Loading required package: ggplot2
Need help getting started? Try the cookbook for R: http://www.cookbook-r.com/Graphs/
rstan (Version 2.19.2, GitRev: 2e1f913d3ca3)
For execution on a local, multicore CPU with excess RAM we recommend calling
options(mc.cores = parallel::detectCores()).
To avoid recompilation of unchanged Stan programs, we recommend calling
rstan_options(auto_write = TRUE)
> 
> data <- list(y=1)
> fit_1 <- stan("test.stan", data=data)   # This works fine

SAMPLING FOR MODEL 'test' NOW (CHAIN 1).
Chain 1: 
Chain 1: Gradient evaluation took 1.1e-05 seconds
Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.11 seconds.
Chain 1: Adjust your expectations accordingly!
Chain 1: 
Chain 1: 
Chain 1: Iteration:    1 / 2000 [  0%]  (Warmup)
Chain 1: Iteration:  200 / 2000 [ 10%]  (Warmup)
Chain 1: Iteration:  400 / 2000 [ 20%]  (Warmup)
Chain 1: Iteration:  600 / 2000 [ 30%]  (Warmup)
Chain 1: Iteration:  800 / 2000 [ 40%]  (Warmup)
Chain 1: Iteration: 1000 / 2000 [ 50%]  (Warmup)
Chain 1: Iteration: 1001 / 2000 [ 50%]  (Sampling)
Chain 1: Iteration: 1200 / 2000 [ 60%]  (Sampling)
Chain 1: Iteration: 1400 / 2000 [ 70%]  (Sampling)
Chain 1: Iteration: 1600 / 2000 [ 80%]  (Sampling)
Chain 1: Iteration: 1800 / 2000 [ 90%]  (Sampling)
Chain 1: Iteration: 2000 / 2000 [100%]  (Sampling)
Chain 1: 
Chain 1:  Elapsed Time: 0.009948 seconds (Warm-up)
Chain 1:                0.011802 seconds (Sampling)
Chain 1:                0.02175 seconds (Total)
Chain 1: 

SAMPLING FOR MODEL 'test' NOW (CHAIN 2).
Chain 2: 
Chain 2: Gradient evaluation took 3e-06 seconds
Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0.03 seconds.
Chain 2: Adjust your expectations accordingly!
Chain 2: 
Chain 2: 
Chain 2: Iteration:    1 / 2000 [  0%]  (Warmup)
Chain 2: Iteration:  200 / 2000 [ 10%]  (Warmup)
Chain 2: Iteration:  400 / 2000 [ 20%]  (Warmup)
Chain 2: Iteration:  600 / 2000 [ 30%]  (Warmup)
Chain 2: Iteration:  800 / 2000 [ 40%]  (Warmup)
Chain 2: Iteration: 1000 / 2000 [ 50%]  (Warmup)
Chain 2: Iteration: 1001 / 2000 [ 50%]  (Sampling)
Chain 2: Iteration: 1200 / 2000 [ 60%]  (Sampling)
Chain 2: Iteration: 1400 / 2000 [ 70%]  (Sampling)
Chain 2: Iteration: 1600 / 2000 [ 80%]  (Sampling)
Chain 2: Iteration: 1800 / 2000 [ 90%]  (Sampling)
Chain 2: Iteration: 2000 / 2000 [100%]  (Sampling)
Chain 2: 
Chain 2:  Elapsed Time: 0.010531 seconds (Warm-up)
Chain 2:                0.009683 seconds (Sampling)
Chain 2:                0.020214 seconds (Total)
Chain 2: 

SAMPLING FOR MODEL 'test' NOW (CHAIN 3).
Chain 3: 
Chain 3: Gradient evaluation took 3e-06 seconds
Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0.03 seconds.
Chain 3: Adjust your expectations accordingly!
Chain 3: 
Chain 3: 
Chain 3: Iteration:    1 / 2000 [  0%]  (Warmup)
Chain 3: Iteration:  200 / 2000 [ 10%]  (Warmup)
Chain 3: Iteration:  400 / 2000 [ 20%]  (Warmup)
Chain 3: Iteration:  600 / 2000 [ 30%]  (Warmup)
Chain 3: Iteration:  800 / 2000 [ 40%]  (Warmup)
Chain 3: Iteration: 1000 / 2000 [ 50%]  (Warmup)
Chain 3: Iteration: 1001 / 2000 [ 50%]  (Sampling)
Chain 3: Iteration: 1200 / 2000 [ 60%]  (Sampling)
Chain 3: Iteration: 1400 / 2000 [ 70%]  (Sampling)
Chain 3: Iteration: 1600 / 2000 [ 80%]  (Sampling)
Chain 3: Iteration: 1800 / 2000 [ 90%]  (Sampling)
Chain 3: Iteration: 2000 / 2000 [100%]  (Sampling)
Chain 3: 
Chain 3:  Elapsed Time: 0.011502 seconds (Warm-up)
Chain 3:                0.010308 seconds (Sampling)
Chain 3:                0.02181 seconds (Total)
Chain 3: 

SAMPLING FOR MODEL 'test' NOW (CHAIN 4).
Chain 4: 
Chain 4: Gradient evaluation took 3e-06 seconds
Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0.03 seconds.
Chain 4: Adjust your expectations accordingly!
Chain 4: 
Chain 4: 
Chain 4: Iteration:    1 / 2000 [  0%]  (Warmup)
Chain 4: Iteration:  200 / 2000 [ 10%]  (Warmup)
Chain 4: Iteration:  400 / 2000 [ 20%]  (Warmup)
Chain 4: Iteration:  600 / 2000 [ 30%]  (Warmup)
Chain 4: Iteration:  800 / 2000 [ 40%]  (Warmup)
Chain 4: Iteration: 1000 / 2000 [ 50%]  (Warmup)
Chain 4: Iteration: 1001 / 2000 [ 50%]  (Sampling)
Chain 4: Iteration: 1200 / 2000 [ 60%]  (Sampling)
Chain 4: Iteration: 1400 / 2000 [ 70%]  (Sampling)
Chain 4: Iteration: 1600 / 2000 [ 80%]  (Sampling)
Chain 4: Iteration: 1800 / 2000 [ 90%]  (Sampling)
Chain 4: Iteration: 2000 / 2000 [100%]  (Sampling)
Chain 4: 
Chain 4:  Elapsed Time: 0.010931 seconds (Warm-up)
Chain 4:                0.010682 seconds (Sampling)
Chain 4:                0.021613 seconds (Total)
Chain 4: 
> 
> fit_2 <- stan("test2.stan", data=data)  # This (a) returns no error and (b) screws up R
> 
> ## For example:
> 
> dssdksdl  # This does not return an error
> 
> 
> 
> 

I get

fit_2 <- stan("test2.stan", data=data)  # This (a) returns no error and (b) screws up R

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
error in ‘model2a02abb45fe_test2’ at line 2, column 2

 1: data {
 2:   xxreal y;
     ^
 3: }

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 ‘test2’ due to the above error.

What version of rstan do you have?

Hi, I’m using R 3.6.1 run from Rstudio (but I get the same problem in regular R console), rstan 2.19.2, macOS Catalina 10.15.

@jonah?

I get what Ben gets. I’m on Mac but I haven’t upgraded to Catalina, so maybe that’s the source of the problem Andrew is encountering?

Here’s what I get:

> fit_2 <- stan("test2.stan", data=data)  # This (a) returns no error and (b) screws up R
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
 error in 'model7a351b859efd_test2' at line 2, column 2
  -------------------------------------------------
     1: data {
     2:   xxreal y;
         ^
     3: }
  -------------------------------------------------

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 'test2' due to the above error.

Probably. The parser will throw an exception when the Stan program is invalid, and the exception presumably gets messed up on Catalina.

It’s weird because, not only does it not return an error, it also screws up R. For now I can handle things by using CmdStanR which gives the extra benefit of lightning-fast compilation.