Error occurred during calling the sampler

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)

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

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

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

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>

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.

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

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

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

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