# Error occurred during calling the sampler

**URL:** <https://discourse.mc-stan.org/t/error-occurred-during-calling-the-sampler/2511>\
**Category:** RStan\
**Created:** [November 15, 2017, 12:37pm UTC](https://discourse.mc-stan.org/t/error-occurred-during-calling-the-sampler/2511 "2017-11-15T12:37:37Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![georgegithinji](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/georgegithinji/32/6307_2.png) [@georgegithinji](https://discourse.mc-stan.org/u/georgegithinji)\
**Post date:** [November 15, 2017, 12:37pm UTC](https://discourse.mc-stan.org/t/error-occurred-during-calling-the-sampler/2511/1 "2017-11-15T12:37:38Z")

</div>

I am running into an issue while compiling a simple linear model via brms, but it appears to be a rstan issue. I googled around though I do not seem to find information on how to effective resolve it. I have tried installing both Rcpp and rstan from source but I still run in to this issue. Is there a workaround? is this a bug or something wrong with my setup? Here is the error I get:

```
Compiling the C++ model
Start sampling
starting worker pid=5249 on localhost:11481 at 15:33:46.552
starting worker pid=5272 on localhost:11481 at 15:33:46.834
starting worker pid=5295 on localhost:11481 at 15:33:47.145
starting worker pid=5319 on localhost:11481 at 15:33:47.428

SAMPLING FOR MODEL 'student(identity) brms-model' NOW (CHAIN 1).

Gradient evaluation took 0.00168 seconds
1000 transitions using 10 leapfrog steps per transition would take 16.8 seconds.
Adjust your expectations accordingly!

[1] "Error in sampler$call_sampler(args_list[[i]]) : "
[2] " c++ exception (unknown reason)"                
error occurred during calling the sampler; sampling not done

```

Operating System: `MacOS`  
Interface Version: Rstan  
Output of writeLines(readLines(file.path(Sys.getenv(“HOME”), “.R/Makevars”))):

```
CFLAGS= -g -O3 -Wall -pedantic -mtune=native -I/usr/local/opt/openssl/include
LDFLAGS= -L/usr/local/opt/openssl/lib -L/usr/local/clang4/lib

CC=/usr/local/clang4/bin/clang
CXX=/usr/local/clang4/bin/clang++
CXX1X=/usr/local/clang4/bin/clang++
CXX98=/usr/local/clang4/bin/clang++
CXX11=/usr/local/clang4/bin/clang++
CXX14=/usr/local/clang4/bin/clang++
CXX17=/usr/local/clang4/bin/clang++
F77=/usr/local/bin/gfortran

CXX=clang++ -arch x86_64 -ftemplate-depth-256

CXXFLAGS += -Wno-ignored-attributes -Wno-deprecated-declarations

CXXFLAGS=-O3 -mtune=native -march=native -Wno-unused-variable -Wno-unused-function

#CXXFLAGS+=-flto -ffat-lto-objects -Wno-unused-local-typedefs

CC=clang
CXX=clang++ -arch x86_64 -ftemplate-depth-256

CXXFLAGS += -Wno-ignored-attributes -Wno-deprecated-declarations

Output of devtools::session_info("rstan”):
Session info ----------------------------------------------------------------------------------------------------------------------
 setting value                       
 version R version 3.4.2 (2017-09-28)
 system x86_64, darwin15.6.0        
 ui RStudio (1.2.158)           
 language (EN)                        
 collate en_GB.UTF-8                 
 tz Africa/Nairobi              
 date 2017-11-15                  

Rcpp * 0.12.13 2017-09-28 CRAN (R 3.4.2)
 RcppEigen 0.3.3.3.0 2017-05-01 CRAN (R 3.4.2)
 reshape2 * 1.4.2 2016-10-22 CRAN (R 3.4.2)
 rlang 0.1.4 2017-11-05 cran (@0.1.4) 
 rstan * 2.16.2 2017-07-03 CRAN (R 3.4.2)
 scales 0.5.0 2017-08-24 CRAN (R 3.4.2)
 StanHeaders * 2.16.0-1 2017-07-03 CRAN (R 3.4.2)
 stats * 3.4.2 2017-10-20 local         
 stats4 3.4.2 2017-10-20 local         
 stringi 1.1.5 2017-04-07 CRAN (R 3.4.2)
 stringr * 1.2.0 2017-02-18 CRAN (R 3.4.2)
 tibble * 1.3.4 2017-08-22 CRAN (R 3.4.2)
 tools 3.4.2 2017-10-20 local         
 utils * 3.4.2 2017-10-20 local         
 viridisLite 0.2.0 2017-03-24 CRAN (R 3.4.2)
```

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [November 15, 2017, 2:24pm UTC](https://discourse.mc-stan.org/t/error-occurred-during-calling-the-sampler/2511/2 "2017-11-15T14:24:11Z")

</div>

Don’t use /usr/local/clang4/bin/clang++. Just do clang++ for now.

---

<div class="post-metadata">

**Author:** ![georgegithinji](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/georgegithinji/32/6307_2.png) [@georgegithinji](https://discourse.mc-stan.org/u/georgegithinji)\
**Post date:** [November 15, 2017, 3:10pm UTC](https://discourse.mc-stan.org/t/error-occurred-during-calling-the-sampler/2511/3 "2017-11-15T15:10:47Z")

</div>

The error still persists. Should I consider reinstalling rstan then? T  
Edit:  
I have reinstalled rstan. The Makevars contents is as follows.  
CC=clang  
CFLAGS= -g -O3 -Wall -pedantic -mtune=native -I/usr/local/opt/openssl/include  
LDFLAGS= -L/usr/local/opt/openssl/lib -L/usr/local/clang4/lib  
CXX=clang++ -arch x86\_64 -ftemplate-depth-256  
CXXFLAGS += -Wno-ignored-attributes -Wno-deprecated-declarations  
CXXFLAGS=-O3 -mtune=native -march=native -Wno-unused-variable -Wno-unused-function  
CXX=clang++ -arch x86\_64 -ftemplate-depth-256  
CXXFLAGS += -Wno-ignored-attributes -Wno-deprecated-declarations  
F77=/usr/local/bin/gfortran

I am getting the same error:  
Gradient evaluation took 0.00261 seconds  
1000 transitions using 10 leapfrog steps per transition would take 26.1 seconds.  
Adjust your expectations accordingly!  
[1] "Error in sampler$call\_sampler(args\_list[[i]]) : "  
[2] " c++ exception (unknown reason)"  
error occurred during calling the sampler; sampling not done

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [November 15, 2017, 3:45pm UTC](https://discourse.mc-stan.org/t/error-occurred-during-calling-the-sampler/2511/4 "2017-11-15T15:45:53Z")

</div>

You have to recompile the Stan model. Make sure to delete any foo.rds files that are the compiled versions of foo.stan.

---

<div class="post-metadata">

**Author:** ![georgegithinji](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/georgegithinji/32/6307_2.png) [@georgegithinji](https://discourse.mc-stan.org/u/georgegithinji)\
**Post date:** [November 15, 2017, 4:05pm UTC](https://discourse.mc-stan.org/t/error-occurred-during-calling-the-sampler/2511/5 "2017-11-15T16:05:00Z")

</div>

I am afraid I am running into the same error even after recompiling the model even after I started the R session and cleaned up the workspace. Renamed the variables and recompiled the model.

```
fit <- brm(bf(distance ~ status, sigma ~ status),
+ family = student,
+ data = dta,
+ iter = 2000,
+ chains = 4)
Compiling the C++ model
Start sampling
starting worker pid=24091 on localhost:11129 at 19:01:40.392
starting worker pid=24114 on localhost:11129 at 19:01:40.663
starting worker pid=24137 on localhost:11129 at 19:01:40.949
starting worker pid=24160 on localhost:11129 at 19:01:41.236

SAMPLING FOR MODEL 'student(identity) brms-model' NOW (CHAIN 1).

Gradient evaluation took 0.001779 seconds
1000 transitions using 10 leapfrog steps per transition would take 17.79 seconds.
Adjust your expectations accordingly!

[1] "Error in sampler$call_sampler(args_list[[i]]) : "
[2] " c++ exception (unknown reason)"                
error occurred during calling the sampler; sampling not done

<truncated>
```

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [November 15, 2017, 4:08pm UTC](https://discourse.mc-stan.org/t/error-occurred-during-calling-the-sampler/2511/6 "2017-11-15T16:08:25Z")

</div>

Is /usr/local/clang4/bin/ somehow on your PATH? If so, you may need to set CXX to the full path of the clang that comes with Xcode.

---

<div class="post-metadata">

**Author:** ![georgegithinji](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/georgegithinji/32/6307_2.png) [@georgegithinji](https://discourse.mc-stan.org/u/georgegithinji)\
**Post date:** [November 15, 2017, 4:15pm UTC](https://discourse.mc-stan.org/t/error-occurred-during-calling-the-sampler/2511/7 "2017-11-15T16:15:24Z")

</div>

Resolved! Thanks a bunch! I removed /usr/local/clang4 from the path.

---

<div class="post-metadata">

**Author:** ![Nimajneb83](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/nimajneb83/32/7791_2.png) [@Nimajneb83](https://discourse.mc-stan.org/u/Nimajneb83)\
**Post date:** [April 21, 2020, 5:53pm UTC](https://discourse.mc-stan.org/t/error-occurred-during-calling-the-sampler/2511/8 "2020-04-21T17:53:26Z")

</div>

I’m having the same problem. What does it mean exactly to remove /usr/local/clang4 from the path…? Thanks!

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [April 21, 2020, 5:58pm UTC](https://discourse.mc-stan.org/t/error-occurred-during-calling-the-sampler/2511/9 "2020-04-21T17:58:44Z")

</div>

It usually entails changing something in the file at ~/.R/Makevars . What do you have in that file currently?

---

<div class="post-metadata">

**Author:** ![Nimajneb83](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/nimajneb83/32/7791_2.png) [@Nimajneb83](https://discourse.mc-stan.org/u/Nimajneb83)\
**Post date:** [April 21, 2020, 10:29pm UTC](https://discourse.mc-stan.org/t/error-occurred-during-calling-the-sampler/2511/10 "2020-04-21T22:29:41Z")

</div>

I seem to have a few of those files, with different content… Here is the content of two of them…

**Makevar 1:**

## With Rcpp 0.11.0 and later, we no longer need to set PKG\_LIBS as there is

## no user-facing library. The include path to headers is already set by R.

#PKG\_LIBS =

## With R 3.1.0 or later, you can uncomment the following line to tell R to

## enable compilation with C++11 (or even C++14) where available

#CXX\_STD = CXX11

**Makevar 2:**

STANHEADERS\_SRC = (shell "(R\_HOME)/bin$(R\_ARCH\_BIN)/Rscript" -e “message()” -e “cat(system.file(‘include’, ‘src’, package = ‘StanHeaders’, mustWork = TRUE))” -e “message()” | grep “StanHeaders”)

PKG\_CPPFLAGS = -I"…/inst/include" -I"$(STANHEADERS\_SRC)" -DBOOST\_DISABLE\_ASSERTS -DEIGEN\_NO\_DEBUG -DBOOST\_MATH\_OVERFLOW\_ERROR\_POLICY=errno\_on\_error

CXX\_STD = CXX14

all: (SHLIB) @if test -e "/usr/bin/install\_name\_tool" && test -e "/usr/local/clang4/lib/libc++.1.dylib" && test -e "/usr/lib/libc++.1.dylib"; then /usr/bin/install\_name\_tool -change /usr/local/clang4/lib/libc++.1.dylib /usr/lib/libc++.1.dylib (SHLIB); fi

clean:  
rm -rf \*.so \*.o

.phony: all clean
