stan-dev / rstan

RStan, the R interface to Stan
https://mc-stan.org
1.04k stars 269 forks source link

rstan suppressing errors in rstudio #721

Open bertozzivill opened 4 years ago

bertozzivill commented 4 years ago

Summary:

Upon running a misspecified model in stan, I get no output and my RStudio no longer prints any error messages.

Description and Reproducible Steps:

I'm new to Stan (pivoting from JAGS), and trying to learn how get my syntax right. For a toy example, let's look at the code below:

library(data.table)
library(rstan)

rm(list=ls())

chains <- 1
warm<-5000
iterations<-10000

example_data <- data.table(hh_size_mean=c(4.2, 4.1, 3.8, 3.5, 3.2),
                           hh_size_se=c(0.04, 0.09, 0.07, 0.04, 0.01),
                           n_llin_mean=c(0.001, 0.8, 0.5, 0.9, 1.2),
                           n_llin_se=c(0.0001, 0.04, 0.05, 0.02, 0.06),
                           population=c(33652565, 39624762, 40679006, 45085900, 46223996)
                           )
data_list <- c(as.list(example_data), list(N = nrow(example_data)))

survey_model_string <- "
    data{
        int N;
        real<lower=0> hh_size_mean[N];
        real<lower=0> hh_size_se[N];
        real<lower=0> n_llin_mean[N];
        real<lower=0> n_llin_se[N];
        real population[N];
    }
    parameters{
      real<lower=0> hh[N];
      real<lower=0> avg_llin[N];
      real<lower=0> llin_count[N];
    }
    model{
      hh ~ normal(hh_size_mean, hh_size_se);
      avg_llin ~ normal(n_llin_mean, n_llin_se);

      for(i in 1:N){
        llin_count[i] = avg_llin[i]*population[i]/hh[i];
      }
    }
    "

survey_prep_model <- stan(model_code=survey_model_string,
                          warmup=warm,
                          data = data_list,
                          chains=chains,
                          iter=iterations,
                          verbose = T
)

I'm sure there's something wrong with my Stan code. What I would expect, upon running the stan() function, is to see an error message with some description of where things are going wrong.

Instead, my call to stan() prints the following, and no more:

TRANSLATING MODEL 'dabf8abd0c0011e93f78bc1b1c0419d1' FROM Stan CODE TO C++ CODE NOW.

Even though the stan call does not throw an error, no survey_prep_model object is saved to my environment. Strangely, all RStudio error messages are suppressed as well. Executing commands such as print(nonexistent_object) or nonexistent_function(4995) prints no error to the console. I have to quit and restart RStudio in order to see error messages again.

Running e.g. print("example_string") or packageVersion("rstan") works as expected, so it is only errors that are suppressed.

This bug may be related to this issue, but this model is very small so I wouldn't expect any memory issues.

RStan Version:

2.19.2

R Version:

3.6.0

Operating System:

10.15.1 As described here, I've already worked through the fixes associated with having Catalina installed.

Thanks for your help.

Achab94 commented 4 years ago

Same issue here. stanc(mystanfile.stan) says my Stan file is syntactically correct, but when fitting it with stan() I get no results/output and/or error messages. @bertozzivill you got any workaround in the meanwhile?

bertozzivill commented 4 years ago

@Achab94 Nope, I'm just sticking with the JAGS version of my code for now :/

bob-carpenter commented 4 years ago

Sorry for all the hassles with Catalina. When I run the above on my High Sierra Mac OS X machine with rstan 2.19.2, I get this:

TRANSLATING MODEL 'dabf8abd0c0011e93f78bc1b1c0419d1' FROM Stan CODE TO C++ CODE NOW.
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Cannot assign to variable outside of declaration block; left-hand-side variable origin=parameter
Illegal statement beginning with non-void expression parsed as
  llin_count[i]
Not a legal assignment, sampling, or function statement.  Note that
  * Assignment statements only allow variables (with optional indexes) on the left;
  * Sampling statements allow arbitrary value-denoting expressions on the left.
  * Functions used as statements must be declared to have void returns

 error in 'model14a676f79e28c_dabf8abd0c0011e93f78bc1b1c0419d1' at line 20, column 8
  -------------------------------------------------
    18:       
    19:       for(i in 1:N){
    20:         llin_count[i] = avg_llin[i]*population[i]/hh[i];
               ^
    21:       }
  -------------------------------------------------

This correctly diagnoses the problem as trying to assign to llin_count in the model block when it was declared in the parameters block. You can't assign to parameters at all in Stan. Because the quantity llin_count isn't used in the log density, you can declare and define it in the generated quantities block, which is much more efficient and clearly indicates it's a transfrom from the fitted parameters:

generated quantities {
  real<lower = 0> llin_count[N];
  for(i in 1:N)
    llin_count[i] = avg_llin[i] * population[i] / hh[i];
}

You can do it with a one-liner if you redeclare avg_llin, population, and hh as vectors and use the elementwise multiplication and division operators,

generated quantities {
  vector<lower = 0>[N] llin_count = avg_llin .* population ./ hh;
}
bertozzivill commented 4 years ago

Hi @bob-carpenter,

Thank you for helping me diagnose the problem with this code, and for the vectorizing tips. Unfortunately to use Stan I need a fix for the underlying issue of error displays-- this code is just part of a much larger model I'm trying to translate, and I need to be able to actively debug.

Have there been reports of these types of issues on Windows? It's relatively painless for me to gain access to a Windows VM if I'm likely to have better luck there.

bob-carpenter commented 4 years ago

Usually we have more problems on Windows than elsewhere. Can you try running RStan in an R terminal rather than RStudio with the suggested parallelization options turned off? I think this turns off the parallelization:

options(mc.cores = 1)

On Dec 1, 2019, at 8:05 PM, Amelia Bertozzi-Villa notifications@github.com wrote:

Hi @bob-carpenter,

Thank you for helping me diagnose the problem with this code, and for the vectorizing tips. Unfortunately to use Stan I need a fix for the underlying issue of error displays-- this code is just part of a much larger model I'm trying to translate, and I need to be able to actively debug.

Have there been reports of these types of issues on Windows? It's relatively painless for me to gain access to a Windows VM if I'm likely to have better luck there.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

bertozzivill commented 4 years ago

Same issue, including suppression of errors after running stan:

Screen Shot 2019-12-02 at 10 40 20 AM

bob-carpenter commented 4 years ago

You're restoring history here, which can confound matters. Could you (a) start in a clean R session, and (b) set options(mc.cores = 1).

If that doesn't work, you're going to need help from a real rstan dev---I'm just a user.

bertozzivill commented 4 years ago

The options line was already in the example_stan.r script I ran. Starting in a clean R session gets the same result sadly, but thank you for your help!

jsilve24 commented 4 years ago

I am having the same issue. Has there been any fix?

bertozzivill commented 4 years ago

Hi @jsilve24 ,

I just tried the solution identified in issue #735 , and it worked for me: install.packages("rstan", type="source")

Let me know if it works for you as well and I'll close this issue?

Achab94 commented 4 years ago

Hi @jsilve24 , I can confirm i solved it with the same procedure @bertozzivill suggested.