Unable to run Stan models: RStudio Crashes or Throws C++ Exception Error

Hi All,

I’ve been working on improving my Stan model and overall skills lately, and for some strange reason, (1) my R session either keeps crashing and is aborted, or (2) I continually receive the error

Error in mod$fit_ptr() : c++ exception (unknown reason)
failed to create the sampler; sampling not done

Thinking this was due to my own Stan program, I attempted to run Andrew Gelman’s golf putting model to see if the problem would vanish (it didn’t):


This hasn’t happened before and I’m a bit stumped.

I am working from RStudio Version 1.3.959 and rstan Version 2.21.2.

I work from a mac OS High Sierra version 10.13.6.

Based on reading posts on this forum regarding such errors, it is recommended to install the developer version of rstan via GitHub, However, I get an error when trying to do this as well.

Anyone know what is going on?


I use RStudio v1.3.1056, rstan v2.21.2, and StanHeaders v2.21.0.5 on OS X 10.15.6. It works for me :) Perhaps download latest RStudio first of all?


Updating the R packages worked for me, though I am slightly hesitant to upgrade to OSX Catalina due to problems encountered with running rstan posed by members of the Stan community.

Nevertheless, I will consider your post to be a solution to my problem.

So, apparently, despite everything working fine late last night, I am now back to square one regarding the C++ exception error above

Error in mod$fit_ptr() : c++ exception (unknown reason)
failed to create the sampler; sampling not done

I have tried several things to fix the issue

  • updating rstan;
  • updating Rcpp;
  • checking that everything on the “RStan Getting Started” stan-dev GitHub page is adhered to.

To reiterate, I work from

  • macOS High Sierra 10.13.6,
  • R 3.6.3
  • RStudio Version 1.3.959
  • rstan 2.21.2

I have reservations on updating to a new mac version.

The error happens when I run the golf putting data (which worked as expected before)

options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)

J <- 19
x <- 1:J
n <- c(1443, 694, 455, 353, 272, 256, 240, 217, 200, 237, 202, 192, 174, 167, 201, 195, 191, 147, 152)
y <- c(1346, 577, 337, 208, 149, 136, 111, 69, 67, 75, 52, 46, 54, 28, 27, 31, 33, 20, 24)
r <- (1.68/2)/12
R <- (4.25/2)/12
overshot <- 3
distance_tolerance <- 1

golf_data <- list(J=J, x=x, n=n, y=y, r=r, R=R, overshot=overshot, distance_tolerance=distance_tolerance)

fit_trig <- stan("golf.stan", data=golf_data)

and the Stan program (golf.stan)

data {
 int J;
 int n[J];
 vector[J] x;
 int y[J];
 real r;
 real R;
 real overshot;
 real distance_tolerance;

transformed data {
  vector[J] threshold_angle = asin((R-r) ./ x);
  vector[J] raw_proportions = to_vector(y) ./ to_vector(n);

parameters {
  real<lower=0> sigma_angle;
  real<lower=0> sigma_distance;
  real<lower=0> sigma_y;

model {
  vector[J] p_angle = 2*Phi(threshold_angle / sigma_angle) - 1;
  vector[J] p_distance = Phi((distance_tolerance - overshot) ./ ((x + overshot)*sigma_distance)) -
           Phi((- overshot) ./ ((x + overshot)*sigma_distance));
  vector[J] p = p_angle .* p_distance;
  raw_proportions ~ normal(p, sqrt(p .* (1-p) ./ to_vector(n) + sigma_y^2));
  [sigma_angle, sigma_distance, sigma_y] ~ normal(0, 1);

generated quantities {
  real sigma_degrees = sigma_angle * 180 / pi();

Any thoughts here??

@jphill01 Sorry you’re getting this error again.

@bgoodri Any ideas? Maybe installing from source? Are there known issues running Stan on High Sierra now?

Also, in case we can’t sort out why RStan keeps giving you this error, I think the new CmdStanR interface should work fine on your system.

Thanks for looking into the issue with rstan.

I tried CmdStanR on the golf putting data and model and it works beautifully! Hopefully things stay that way for now on for other models.

I will use this implementation in the interim.

1 Like

I am having this same issue after upgrading RStudio to v.1.4.1717. Initially any attempt to fit a model via CmdStanR or Rstan would throw a segmentation fault, while in R it would say that everything completed, but no samples generated.

I read that the seg fault could be caused by mismatched Rcpp, so I updated it. That got rid of the seg fault, but chains still terminate immediately via both Rstan and CmdStanR. Neither creates a sampler. The error messages on Rstan are more illustrative:

$ fit ← stan(“attrib.flat.nopool.center.stan”, chains=1, iter=1000, data=dat)
‘config’ variable ‘CPP’ is deprecated
clang -E
Error in mod$fit_ptr() : c++ exception (unknown reason)
In addition: Warning messages:
1: In read.dcf(file.path(p, “DESCRIPTION”), c(“Package”, “Version”)) :
cannot open compressed file ‘/Library/Frameworks/R.framework/Versions/3.6/Resources/library/RcppEigen/DESCRIPTION’, probable reason ‘No such file or directory’
2: In system(paste(CPP, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
error in running command
failed to create the sampler; sampling not done

Here is the output from CmdstanR:

$ model$sample(data=dat, seed=3141)
Running MCMC with 4 sequential chains…

Warning: Chain 1 finished unexpectedly!

Warning: Chain 2 finished unexpectedly!

Warning: Chain 3 finished unexpectedly!

Warning: Chain 4 finished unexpectedly!

Warning: Use read_cmdstan_csv() to read the results of the failed chains.
Error: Fitting failed. Unable to print.
In addition: Warning messages:
1: All chains finished unexpectedly!

2: No chains finished successfully. Unable to retrieve the fit.

Also, this…

$ check_cmdstan_toolchain()
The CmdStan toolchain is setup properly!

I have rebooted my machine and restarted the R session (and RStudio) several times to no avail.

Any help is greatly appreciated. I am under a tight deadline. Is it possible to run Cmdstan from the command line? I assume it is (given the name), but haven’t yet found the relevant doc.


P.S. I ran rebuild_cmdstan() and it completed successfully. But nothing has changed.

R version 3.6.2 (2019-12-12)"
RStudio Version 1.4.1717
cmdstan version 2.27.0
Mac OS 10.15.7 (19H2) “Catalina”

Update: The problem does not appear to be in Rstudio. I have run the same R code directly on the R Console from the command-line with the same results.

Hi @lpslot sorry you’re running into this problem. I recommend creating a fresh topic on the forum for your issue instead of responding here because often fewer people read responses to old topics. I think if you do that you’ll get more responses. When you do that can you also share what you get from CmdStanR’s output() method after the error you get? You can do that like this:

fit <- model$sample(data=dat, seed=3141)
fit$output(id = 1) # show more detailed output for chain 1, maybe something relevant 

Thanks, @jonah.

I had three issues: (1) Rstudio issues, (2) Stan crashing, and after the crash was resolved (3) Stan not sampling.

Focusing on Stan, the crash was entirely my fault. It’s been a few years since I lived in a C/C+±like environment, and it took me a bit to catch on that my user-defined function was accessing array elements incorrectly and bounding off into unknown memory. Once I realized and fixed that, the crash went away.

I don’t know why Stan wasn’t sampling, but I suspect it was some error in my model. It was the middle of the night, so my memory is foggy. Eventually, I did get it to sample.

But the Rstudio issue was some confluence of updating Rstudio to the newest version, and flailing around with packages trying to fix the above two problems, before I understood them.

In the end, I did a fresh install of everything R and Stan related (which allowed me to also update R to the current version). Since then everything has been fine.

I’d be happy to create a new topic, if I could remember some element that may have actually been a problem with CmdStanR or Rstan. But I can’t.

Thanks for the advice. I’ll definitely use it next time. :)

Ok great, glad it’s working for you now!

No worries!