Does rstan do error reporting, if at all?

Hi, I’m an instructor planning a course in Bayesian statistics, and I’m surveying R packages to use for the course. I really like RStan (especially about its vectorization, code blocks, and connection to high-level packages like brms) but it seems to give very poor error reporting when I make a mistake in defining a model (compared to rjags). For example, if I forget to add a semicolon, then I get this error:

Error in stanc(file = file, model_code = model_code, model_name = model_name, : 
failed to parse Stan model '7e02a3e53e12ac2cd8be41854f4f5ce5' due to the above error.

This is not satisfactory for my course since I hope students can get more detailed feedback. I also struggle to find errors in my own code, even I’m quite familiar with Stan. Is there an easy way to get more detailed error, like

Error in line X

or

Missing semicolon at the end of line X

Here are some example error messages from NIMBLE, and I’m truly impressed:

Did you mistakenly use `~` instead of `<-`?
Multiple definitions for the same node. Did you forget indexing with 'i' on the left-hand side of 'prob <- ilogit(b0 + b1 * x[i])'?

Thank you!

You seem to have missed the part above the message. If I take a model foo.stan

data {
  int a[5, 7]
}

that is just a data declaration missing a semicolon, then this is the error message I see from RStan:

> library('rstan')
Loading required package: StanHeaders
Loading required package: ggplot2
rstan (Version 2.21.5, 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)

> stan_model('foo.stan')
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
 error in 'model1335421e9c466_foo' at line 3, column 0
  -------------------------------------------------
     1: data {
     2:   int a[5, 7]
     3: }
       ^
     4: transformed data {
  -------------------------------------------------

PARSER EXPECTED: ";"
Error in stanc(file = file, model_code = model_code, model_name = model_name,  : 
  failed to parse Stan model 'foo' due to the above error.
> 

Overall, though, unless you need to access the log density and gradient directly, you might find it easier to use cmdstanr, because it’s both easier to install and more up to date.

The only thing missing from it is a properly structured extract, where the doc still suggests using RStan (pinging @Jonah again on this as I think it’s important enough to mention this missing feature every time I recommend cmdstanr).

cmdstanr is also up to date with the most recent Stan (2.30), whereas shifting CRAN policy has currently orphaned RStan on CRAN at 2.21 (we’ve been trying to sort this out for over 2 years now with no luck so far). You can install an RStan 2.26 through GitHub.

Thank you! Based on your answer, I found that the part above the message is filtered/blocked in RMarkdown for unknown reasons. The more detailed message is indeed available from R console.

I’m not an expert in R internals, but it might be piped to the standard error stream (stderr) rather than standard output (stdout).

I’ve switched from mainly using R to mainly using Python these days, but I can highly recommend RStudio’s new quarto package as a replacement for RMarkdown. It not only works seamlessly with Python, the default formatting is much much cleaner and more typographically sound in its choice of fonts and spacing.

1 Like