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