RStudio aborting after successfully fitting model and then plotting samples

Hello, I am brand new to using ‘rstan’ and specifying models in a “.stan” file and compiling and fitting it with stan(file = model_file, data = data_list). I have been using ‘rstanarm’ and ‘rethinking’ quite a bit though, and have had no issues.

In brief: Plotting samples from a fit model’s posterior causes RStudio to “abort” the R session.

Details:
I am able to fit a simple linear regression model on the iris data set and use the plot() method to show the posterior distributions for the intercept, slope, and standard deviation. I then use extract() on the model object to get samples from the posterior. Again, this seems to work fine. The problem arises when I try to plot the distributions using ‘ggplot2’ (even if I only take 3 values from each parameter’s posterior). This causes RStudio to “abort the session.”

Interestingly, the problem does not arise if I save the fit model object (using saveRDS()), restart R, load in the model object and then try to plot the posteriors. Also, plotting some other data doesn’t cause RStudio to abort, only the posterior samples.

Here is the Stan model:

data {
    int<lower=0> N;          // number of data points
    vector[N] petal_length;  // x data
    vector[N] petal_width;   // y data
}

parameters {
    real alpha;           // intercept
    real beta;            // slope
    real<lower=0> sigma;  // std. dev.
}

model {
    petal_width ~ normal(alpha + beta * petal_length, sigma);
}

And here is the relevant R code:

# Organize data for the model.
m1_data <- list(
    N = nrow(d),
    petal_length = d$petal_length,
    petal_width = d$petal_width
)

# Fit the model.
m1_file <- file.path(model_dir, "ch01_linear-regression_m1.stan")
m1_fit <- stan(file = m1_file, data = m1_data)

# Plot the posteriors.
plot(m1_fit)  # this works and shows the 80% and 95% intervals

# Take samples from the posterior, turn into long data frame, and sub-sample 3 values from 
# each parameter's posterior.
m1_samples <- rstan::extract(m1_fit) %>%
    enframe() %>%
    unnest(value) %>%
    group_by(name) %>%
    sample_n(3) %>%
    ungroup() %>%
    filter(name %in% c("alpha", "beta", "sigma"))

# Try to plot these 3 values for each posterior.
### THIS CAUSES RSTUDIO TO ABORT.
m1_samples %>%
    ggplot(aes(x = name, y = value)) +
    geom_violin()

I have tried:

  • reinstalling ‘rstan’ with and without compiling source
  • reinstalling Rcpp and updating all other packages
  • updating RStudio
  • restarting my computer (MacOS Catalina 10.15.2)

Any help is greatly appreciated!

Welcome to the community!

I can’t reproduce your problem, but I did some small changes,

library(tidyverse)
library(rstan)

d <- iris

# Organize data for the model
m1_data <- list(
    N = nrow(d),
    petal_length = d$Petal.Length, # Petal.Length not petal_length
    petal_width = d$Petal.Width # same as above
)

and then I ran everything else according to your code above. I have OS X 10.15.6, rstan 2.21.2, StanHeaders 2.21.0.5, and RStudio 1.3.1056.

However, using only three values for each violin plot is probably not the best thing to do :)

1 Like

Thanks for the warm welcome and help, Richard. Unfortunately, the names in the data aren’t the problem because I passed iris through janitor::clean_names() before using it (it’s just a reflex at this point). Also, I tried using 3 data points just to be sure that the plotting wasn’t causing a crash due to memory or rendering problems.

I realized that I could see the actual abort message if I rendered the R markdown from the command line. Interestingly, the R session aborts during the model fitting now (when running from RStudio, I can fit the model and the abort happens when plotting posterior samples):

R(40388,0x1037f7dc0) malloc: Attempted to register zone more than once: 0x102485110
R(40388,0x1037f7dc0) malloc: Non-aligned pointer 0x10a627b80 being freed (2)
R(40388,0x1037f7dc0) malloc: *** set a breakpoint in malloc_error_break to debug
[1]    40388 abort      R

I couldn’t find any answers by Googling the error messages.

I’ve checked the names of parameters and data in the model, but can’t find any issues (but, again, I’m very new to this, so there may be mistakes). Also, I tried loading in ‘rstan’ after ‘tidyverse’, but that didn’t change anything.

1 Like

hmm… a malloc error. How does your .R/Makevars look like? I have the following,

CXX14FLAGS=-O3 -mtune=native -ftemplate-depth-256
FC = /usr/local/bin/gfortran
FLIBS = -L/usr/local/lib/gcc/10 -L/usr/local/lib -lgfortran -lquadmath -lm

I didn’t have one, but I guess I had made a back-up at one time or another because I had a file ~/.R/Makevars.bck:

CC=clang
CXX=clang++

CXX14FLAGS=-O3 -march=native -mtune=native
CXX14FLAGS += -arch x86_64 -ftemplate-depth-256

I changed it to ~/.R/Makevars and tried running the program again, but it didn’t help. I also tried copying in yours, but got the same message. Do I need to rebuild something after changing these files? What put the Makevars file there in the first place? Maybe I can reinstall that?

No, no need to rebuild things in this case since these are just compiler flags the compiler uses when compiling your model. Which compiler do you use? You should use the default one coming from Xcode.

I think I use the default - I try not to change these things because I don’t understand them. If I run gcc --version I get the following:

> gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Now I honestly don’t know what else there might be causing this. You could try to update according to what I have (i.e., rstan, StanHeaders, OS X, RStudio, etc.) but it seems a bit far-fetched. Let’s see what @bgoodri has to say about this.

1 Like

I’m glad I came here to ask instead of banging my head against the wall - if you can’t figure it out, then there is no way I was going to. Thanks, @torkar for your help on a Sunday morning!

1 Like

I’ve spent a few hours this afternoon troubleshooting by uninstalling and re-installing the macOS R toolchain (I think that’s what it is called) and trying different Makevars files, but don’t think I made any real progress.

I did find an unusual pattern though. So far, I’ve been running everything in an R Markdown file, either in RStudio or using rmarkdown::render(), and this has reliably caused the R session to crash with the error shown previously. The only difference is that in RStudio, the session aborts when I try to plot the posterior samples, but when using rmarkdown::render(), it crashes during model fitting. I then created a barebones R script (.R file) that runs the same code as in the R markdown. If I run that using source("stan-test.R") and then run the R markdown using rmarkdown::render(path/to/rmd-file.Rmd, knit_root_dir=getwd()), everything works fine.

I still don’t know what the issue is, but I’m hoping that this will help someone more knowledgeable figure it out.

(There is LaTeX in the R Markdown, but removing it had no impact on behavior.)

Then I guess you have a partial solution? To me this sounds like a rmarkdown problem?

Yeah, I think you’re right. That’s annoying, but it will be fine for now. Thanks @torkar for your help. Hopefully if @bgoodri has some free time, he can take a look at this issue, too.

1 Like

My RStudio does not crash on Linux with this example. Can anyone with a Mac reproduce it?

I have been having this problem repeatedly.

For this particular case it reproduces using R from the command line and knitr::knit(“EXAMPLE.Rmd”). It doesn’t reproduce when I just run the code chunks in Rstudio. but I do have the problem in Rstudio with similar files.

I am on a mac

R version 4.0.2 (2020-06-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] forcats_0.5.0        stringr_1.4.0        dplyr_1.0.0         
 [4] purrr_0.3.4          readr_1.3.1          tidyr_1.1.0         
 [7] tibble_3.0.3         tidyverse_1.3.0      rstan_2.21.2        
[10] ggplot2_3.3.2        StanHeaders_2.21.0-5

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5         lubridate_1.7.9    prettyunits_1.1.1 
 [4] ps_1.3.3           assertthat_0.2.1   digest_0.6.25     
 [7] packrat_0.5.0      V8_3.2.0           R6_2.4.1          
[10] cellranger_1.1.0   backports_1.1.8    reprex_0.3.0      
[13] stats4_4.0.2       evaluate_0.14      httr_1.4.2        
[16] pillar_1.4.6       rlang_0.4.7        curl_4.3          
[19] readxl_1.3.1       rstudioapi_0.11    callr_3.4.3       
[22] blob_1.2.1         rmarkdown_2.3      labeling_0.3      
[25] loo_2.3.1          munsell_0.5.0      broom_0.7.0       
[28] compiler_4.0.2     modelr_0.1.8       xfun_0.15         
[31] pkgconfig_2.0.3    pkgbuild_1.1.0     htmltools_0.5.0   
[34] tidyselect_1.1.0   gridExtra_2.3      codetools_0.2-16  
[37] matrixStats_0.56.0 fansi_0.4.1        crayon_1.3.4      
[40] dbplyr_1.4.4       withr_2.2.0        grid_4.0.2        
[43] jsonlite_1.7.0     gtable_0.3.0       lifecycle_0.2.0   
[46] DBI_1.1.0          magrittr_1.5       scales_1.1.1      
[49] RcppParallel_5.0.2 cli_2.0.2          stringi_1.4.6     
[52] farver_2.0.3       fs_1.4.2           xml2_1.3.2        
[55] ellipsis_0.3.1     generics_0.0.2     vctrs_0.3.2       
[58] tools_4.0.2        glue_1.4.1         hms_0.5.3         
[61] processx_3.4.3     parallel_4.0.2     yaml_2.2.1        
[64] inline_0.3.15      colorspace_1.4-1   rvest_0.3.5       
[67] knitr_1.29         haven_2.3.1      

When in RStudio, does extracting the posterior samples with ‘tidybayes’ and trying to plot something cause the R session to abort?

I am getting a similar issue on macOS Catalina as well with R 4.0.2, when I run it straight from my Terminal. It is kind of unpredictable / non-reproducible I’m afraid. Most times (but not always), I get the same malloc warning messages after calling a stan model (either via brms or directly with rstan). The model fits well, but then as soon as I try to check the summary or simply plot something out of the object, the R sessions aborts.

This behaviour emerged when I was updating another package I’m working on that depends on rstan, and I told R to update rstan from source as well, but then I gave up and cancelled the installation half way through. I then tried to completely uninstall R and all packages, and reinstalled everything from scratch. I’m still getting the same error.

I do not have a ~/.R/Makevars anymore, and my ~/.Renviron does not contain any path (just an API key for a non-related package).

I’m using macOS Catalina 10.15.6, my gcc --version:

> gcc --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.3 (clang-1103.0.32.59)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

This is what prints to screen when I run the example code

library(rstan)
example(stan_model,
        run.dontrun = TRUE,
        verbose = TRUE)
Found file = ‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library/rstan/help/stan_model’ 
'envir' chosen:<environment: R_GlobalEnv>
encoding = "UTF-8" chosen
--> parsed 4 expressions; now eval(.)ing them:
has srcrefs:
List of 4
 $ : 'srcref' int [1:8] 8 1 8 84 1 84 8 8
  ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x7fa5e5b021b8> 
 $ : 'srcref' int [1:8] 9 1 9 56 1 56 9 9
  ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x7fa5e5b021b8> 
 $ : 'srcref' int [1:8] 10 1 10 45 1 45 10 10
  ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x7fa5e5b021b8> 
 $ : 'srcref' int [1:8] 11 1 11 46 1 46 11 11
  ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x7fa5e5b021b8> 

>>>> eval(expression_nr. 1 )
		 =================

stn_md> stancode <- 'data {real y_mean;} parameters {real y;} model {y ~ normal(y_mean,1);}'
curr.fun: symbol <-
 .. after ‘expression(stancode <- 'data {real y_mean;} parameters {real y;} model {y ~ normal(y_mean,1);}')’

>>>> eval(expression_nr. 2 )
		 =================
stn_md> mod <- stan_model(model_code = stancode, verbose = TRUE)

TRANSLATING MODEL '16a540c6086086816528e4524def24d9' FROM Stan CODE TO C++ CODE NOW.
successful in parsing the Stan model '16a540c6086086816528e4524def24d9'.
COMPILING THE C++ CODE FOR MODEL '16a540c6086086816528e4524def24d9' NOW.
OS: x86_64, darwin17.0; rstan: 2.21.2; Rcpp: 1.0.5; inline: 0.3.16 
 >> setting environment variables: 
PKG_LIBS =  '/Library/Frameworks/R.framework/Versions/4.0/Resources/library/rstan/lib//libStanServices.a' -L'/Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/lib/' -lStanHeaders -L'/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppParallel/lib/' -ltbb -ltbbmalloc -ltbbmalloc_proxy
PKG_CPPFLAGS =   -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/Rcpp/include/"  -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/"  -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/unsupported"  -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/BH/include" -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/src/"  -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/"  -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppParallel/include/"  -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DBOOST_NO_AUTO_PTR  -include '/Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp'  -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1 
 >> Program source :

   1 : 
   2 : // includes from the plugin
   3 : // [[Rcpp::plugins(cpp14)]]
   4 : 
   5 : 
   6 : // user includes
   7 : #include <Rcpp.h>
   8 : #include <rstan/io/rlist_ref_var_context.hpp>
   9 : #include <rstan/io/r_ostream.hpp>
  10 : #include <rstan/stan_args.hpp>
  11 : #include <boost/integer/integer_log2.hpp>
  12 : // Code generated by Stan version 2.21.0
  13 : 
  14 : #include <stan/model/model_header.hpp>
  15 : 
  16 : namespace model176e96ae23f07_16a540c6086086816528e4524def24d9_namespace {
  17 : 
  18 : using std::istream;
  19 : using std::string;
  20 : using std::stringstream;
  21 : using std::vector;
  22 : using stan::io::dump;
  23 : using stan::math::lgamma;
  24 : using stan::model::prob_grad;
  25 : using namespace stan::math;
  26 : 
  27 : static int current_statement_begin__;
  28 : 
  29 : stan::io::program_reader prog_reader__() {
  30 :     stan::io::program_reader reader;
  31 :     reader.add_event(0, 0, "start", "model176e96ae23f07_16a540c6086086816528e4524def24d9");
  32 :     reader.add_event(3, 1, "end", "model176e96ae23f07_16a540c6086086816528e4524def24d9");
  33 :     return reader;
  34 : }
  35 : 
  36 : class model176e96ae23f07_16a540c6086086816528e4524def24d9
  37 :   : public stan::model::model_base_crtp<model176e96ae23f07_16a540c6086086816528e4524def24d9> {
  38 : private:
  39 :         double y_mean;
  40 : public:
  41 :     model176e96ae23f07_16a540c6086086816528e4524def24d9(rstan::io::rlist_ref_var_context& context__,
  42 :         std::ostream* pstream__ = 0)
  43 :         : model_base_crtp(0) {
  44 :         ctor_body(context__, 0, pstream__);
  45 :     }
  46 : 
  47 :     model176e96ae23f07_16a540c6086086816528e4524def24d9(stan::io::var_context& context__,
  48 :         unsigned int random_seed__,
  49 :         std::ostream* pstream__ = 0)
  50 :         : model_base_crtp(0) {
  51 :         ctor_body(context__, random_seed__, pstream__);
  52 :     }
  53 : 
  54 :     void ctor_body(stan::io::var_context& context__,
  55 :                    unsigned int random_seed__,
  56 :                    std::ostream* pstream__) {
  57 :         typedef double local_scalar_t__;
  58 : 
  59 :         boost::ecuyer1988 base_rng__ =
  60 :           stan::services::util::create_rng(random_seed__, 0);
  61 :         (void) base_rng__;  // suppress unused var warning
  62 : 
  63 :         current_statement_begin__ = -1;
  64 : 
  65 :         static const char* function__ = "model176e96ae23f07_16a540c6086086816528e4524def24d9_namespace::model176e96ae23f07_16a540c6086086816528e4524def24d9";
  66 :         (void) function__;  // dummy to suppress unused var warning
  67 :         size_t pos__;
  68 :         (void) pos__;  // dummy to suppress unused var warning
  69 :         std::vector<int> vals_i__;
  70 :         std::vector<double> vals_r__;
  71 :         local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
  72 :         (void) DUMMY_VAR__;  // suppress unused var warning
  73 : 
  74 :         try {
  75 :             // initialize data block variables from context__
  76 :             current_statement_begin__ = 1;
  77 :             context__.validate_dims("data initialization", "y_mean", "double", context__.to_vec());
  78 :             y_mean = double(0);
  79 :             vals_r__ = context__.vals_r("y_mean");
  80 :             pos__ = 0;
  81 :             y_mean = vals_r__[pos__++];
  82 : 
  83 : 
  84 :             // initialize transformed data variables
  85 :             // execute transformed data statements
  86 : 
  87 :             // validate transformed data
  88 : 
  89 :             // validate, set parameter ranges
  90 :             num_params_r__ = 0U;
  91 :             param_ranges_i__.clear();
  92 :             current_statement_begin__ = 1;
  93 :             num_params_r__ += 1;
  94 :         } catch (const std::exception& e) {
  95 :             stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
  96 :             // Next line prevents compiler griping about no return
  97 :             throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
  98 :         }
  99 :     }
 100 : 
 101 :     ~model176e96ae23f07_16a540c6086086816528e4524def24d9() { }
 102 : 
 103 : 
 104 :     void transform_inits(const stan::io::var_context& context__,
 105 :                          std::vector<int>& params_i__,
 106 :                          std::vector<double>& params_r__,
 107 :                          std::ostream* pstream__) const {
 108 :         typedef double local_scalar_t__;
 109 :         stan::io::writer<double> writer__(params_r__, params_i__);
 110 :         size_t pos__;
 111 :         (void) pos__; // dummy call to supress warning
 112 :         std::vector<double> vals_r__;
 113 :         std::vector<int> vals_i__;
 114 : 
 115 :         current_statement_begin__ = 1;
 116 :         if (!(context__.contains_r("y")))
 117 :             stan::lang::rethrow_located(std::runtime_error(std::string("Variable y missing")), current_statement_begin__, prog_reader__());
 118 :         vals_r__ = context__.vals_r("y");
 119 :         pos__ = 0U;
 120 :         context__.validate_dims("parameter initialization", "y", "double", context__.to_vec());
 121 :         double y(0);
 122 :         y = vals_r__[pos__++];
 123 :         try {
 124 :             writer__.scalar_unconstrain(y);
 125 :         } catch (const std::exception& e) {
 126 :             stan::lang::rethrow_located(std::runtime_error(std::string("Error transforming variable y: ") + e.what()), current_statement_begin__, prog_reader__());
 127 :         }
 128 : 
 129 :         params_r__ = writer__.data_r();
 130 :         params_i__ = writer__.data_i();
 131 :     }
 132 : 
 133 :     void transform_inits(const stan::io::var_context& context,
 134 :                          Eigen::Matrix<double, Eigen::Dynamic, 1>& params_r,
 135 :                          std::ostream* pstream__) const {
 136 :       std::vector<double> params_r_vec;
 137 :       std::vector<int> params_i_vec;
 138 :       transform_inits(context, params_i_vec, params_r_vec, pstream__);
 139 :       params_r.resize(params_r_vec.size());
 140 :       for (int i = 0; i < params_r.size(); ++i)
 141 :         params_r(i) = params_r_vec[i];
 142 :     }
 143 : 
 144 : 
 145 :     template <bool propto__, bool jacobian__, typename T__>
 146 :     T__ log_prob(std::vector<T__>& params_r__,
 147 :                  std::vector<int>& params_i__,
 148 :                  std::ostream* pstream__ = 0) const {
 149 : 
 150 :         typedef T__ local_scalar_t__;
 151 : 
 152 :         local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
 153 :         (void) DUMMY_VAR__;  // dummy to suppress unused var warning
 154 : 
 155 :         T__ lp__(0.0);
 156 :         stan::math::accumulator<T__> lp_accum__;
 157 :         try {
 158 :             stan::io::reader<local_scalar_t__> in__(params_r__, params_i__);
 159 : 
 160 :             // model parameters
 161 :             current_statement_begin__ = 1;
 162 :             local_scalar_t__ y;
 163 :             (void) y;  // dummy to suppress unused var warning
 164 :             if (jacobian__)
 165 :                 y = in__.scalar_constrain(lp__);
 166 :             else
 167 :                 y = in__.scalar_constrain();
 168 : 
 169 :             // model body
 170 : 
 171 :             current_statement_begin__ = 1;
 172 :             lp_accum__.add(normal_log<propto__>(y, y_mean, 1));
 173 : 
 174 :         } catch (const std::exception& e) {
 175 :             stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
 176 :             // Next line prevents compiler griping about no return
 177 :             throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
 178 :         }
 179 : 
 180 :         lp_accum__.add(lp__);
 181 :         return lp_accum__.sum();
 182 : 
 183 :     } // log_prob()
 184 : 
 185 :     template <bool propto, bool jacobian, typename T_>
 186 :     T_ log_prob(Eigen::Matrix<T_,Eigen::Dynamic,1>& params_r,
 187 :                std::ostream* pstream = 0) const {
 188 :       std::vector<T_> vec_params_r;
 189 :       vec_params_r.reserve(params_r.size());
 190 :       for (int i = 0; i < params_r.size(); ++i)
 191 :         vec_params_r.push_back(params_r(i));
 192 :       std::vector<int> vec_params_i;
 193 :       return log_prob<propto,jacobian,T_>(vec_params_r, vec_params_i, pstream);
 194 :     }
 195 : 
 196 : 
 197 :     void get_param_names(std::vector<std::string>& names__) const {
 198 :         names__.resize(0);
 199 :         names__.push_back("y");
 200 :     }
 201 : 
 202 : 
 203 :     void get_dims(std::vector<std::vector<size_t> >& dimss__) const {
 204 :         dimss__.resize(0);
 205 :         std::vector<size_t> dims__;
 206 :         dims__.resize(0);
 207 :         dimss__.push_back(dims__);
 208 :     }
 209 : 
 210 :     template <typename RNG>
 211 :     void write_array(RNG& base_rng__,
 212 :                      std::vector<double>& params_r__,
 213 :                      std::vector<int>& params_i__,
 214 :                      std::vector<double>& vars__,
 215 :                      bool include_tparams__ = true,
 216 :                      bool include_gqs__ = true,
 217 :                      std::ostream* pstream__ = 0) const {
 218 :         typedef double local_scalar_t__;
 219 : 
 220 :         vars__.resize(0);
 221 :         stan::io::reader<local_scalar_t__> in__(params_r__, params_i__);
 222 :         static const char* function__ = "model176e96ae23f07_16a540c6086086816528e4524def24d9_namespace::write_array";
 223 :         (void) function__;  // dummy to suppress unused var warning
 224 : 
 225 :         // read-transform, write parameters
 226 :         double y = in__.scalar_constrain();
 227 :         vars__.push_back(y);
 228 : 
 229 :         double lp__ = 0.0;
 230 :         (void) lp__;  // dummy to suppress unused var warning
 231 :         stan::math::accumulator<double> lp_accum__;
 232 : 
 233 :         local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
 234 :         (void) DUMMY_VAR__;  // suppress unused var warning
 235 : 
 236 :         if (!include_tparams__ && !include_gqs__) return;
 237 : 
 238 :         try {
 239 :             if (!include_gqs__ && !include_tparams__) return;
 240 :             if (!include_gqs__) return;
 241 :         } catch (const std::exception& e) {
 242 :             stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
 243 :             // Next line prevents compiler griping about no return
 244 :             throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
 245 :         }
 246 :     }
 247 : 
 248 :     template <typename RNG>
 249 :     void write_array(RNG& base_rng,
 250 :                      Eigen::Matrix<double,Eigen::Dynamic,1>& params_r,
 251 :                      Eigen::Matrix<double,Eigen::Dynamic,1>& vars,
 252 :                      bool include_tparams = true,
 253 :                      bool include_gqs = true,
 254 :                      std::ostream* pstream = 0) const {
 255 :       std::vector<double> params_r_vec(params_r.size());
 256 :       for (int i = 0; i < params_r.size(); ++i)
 257 :         params_r_vec[i] = params_r(i);
 258 :       std::vector<double> vars_vec;
 259 :       std::vector<int> params_i_vec;
 260 :       write_array(base_rng, params_r_vec, params_i_vec, vars_vec, include_tparams, include_gqs, pstream);
 261 :       vars.resize(vars_vec.size());
 262 :       for (int i = 0; i < vars.size(); ++i)
 263 :         vars(i) = vars_vec[i];
 264 :     }
 265 : 
 266 :     std::string model_name() const {
 267 :         return "model176e96ae23f07_16a540c6086086816528e4524def24d9";
 268 :     }
 269 : 
 270 : 
 271 :     void constrained_param_names(std::vector<std::string>& param_names__,
 272 :                                  bool include_tparams__ = true,
 273 :                                  bool include_gqs__ = true) const {
 274 :         std::stringstream param_name_stream__;
 275 :         param_name_stream__.str(std::string());
 276 :         param_name_stream__ << "y";
 277 :         param_names__.push_back(param_name_stream__.str());
 278 : 
 279 :         if (!include_gqs__ && !include_tparams__) return;
 280 : 
 281 :         if (include_tparams__) {
 282 :         }
 283 : 
 284 :         if (!include_gqs__) return;
 285 :     }
 286 : 
 287 : 
 288 :     void unconstrained_param_names(std::vector<std::string>& param_names__,
 289 :                                    bool include_tparams__ = true,
 290 :                                    bool include_gqs__ = true) const {
 291 :         std::stringstream param_name_stream__;
 292 :         param_name_stream__.str(std::string());
 293 :         param_name_stream__ << "y";
 294 :         param_names__.push_back(param_name_stream__.str());
 295 : 
 296 :         if (!include_gqs__ && !include_tparams__) return;
 297 : 
 298 :         if (include_tparams__) {
 299 :         }
 300 : 
 301 :         if (!include_gqs__) return;
 302 :     }
 303 : 
 304 : }; // model
 305 : 
 306 : }  // namespace
 307 : 
 308 : typedef model176e96ae23f07_16a540c6086086816528e4524def24d9_namespace::model176e96ae23f07_16a540c6086086816528e4524def24d9 stan_model;
 309 : 
 310 : #ifndef USING_R
 311 : 
 312 : stan::model::model_base& new_model(
 313 :         stan::io::var_context& data_context,
 314 :         unsigned int seed,
 315 :         std::ostream* msg_stream) {
 316 :   stan_model* m = new stan_model(data_context, seed, msg_stream);
 317 :   return *m;
 318 : }
 319 : 
 320 : #endif
 321 : 
 322 : 
 323 : 
 324 : #include <rstan_next/stan_fit.hpp>
 325 : 
 326 : struct stan_model_holder {
 327 :     stan_model_holder(rstan::io::rlist_ref_var_context rcontext,
 328 :                       unsigned int random_seed)
 329 :     : rcontext_(rcontext), random_seed_(random_seed)
 330 :      {
 331 :      }
 332 : 
 333 :    //stan::math::ChainableStack ad_stack;
 334 :    rstan::io::rlist_ref_var_context rcontext_;
 335 :    unsigned int random_seed_;
 336 : };
 337 : 
 338 : Rcpp::XPtr<stan::model::model_base> model_ptr(stan_model_holder* smh) {
 339 :   Rcpp::XPtr<stan::model::model_base> model_instance(new stan_model(smh->rcontext_, smh->random_seed_), true);
 340 :   return model_instance;
 341 : }
 342 : 
 343 : Rcpp::XPtr<rstan::stan_fit_base> fit_ptr(stan_model_holder* smh) {
 344 :   return Rcpp::XPtr<rstan::stan_fit_base>(new rstan::stan_fit(model_ptr(smh), smh->random_seed_), true);
 345 : }
 346 : 
 347 : std::string model_name(stan_model_holder* smh) {
 348 :   return model_ptr(smh).get()->model_name();
 349 : }
 350 : 
 351 : RCPP_MODULE(stan_fit4model176e96ae23f07_16a540c6086086816528e4524def24d9_mod){
 352 :   Rcpp::class_<stan_model_holder>("stan_fit4model176e96ae23f07_16a540c6086086816528e4524def24d9")
 353 :   .constructor<rstan::io::rlist_ref_var_context, unsigned int>()
 354 :   .method("model_ptr", &model_ptr)
 355 :   .method("fit_ptr", &fit_ptr)
 356 :   .method("model_name", &model_name)
 357 :   ;
 358 : }
 359 : 
 360 : 
 361 : // declarations
 362 : extern "C" {
 363 : SEXP file176e918054efa( ) ;
 364 : }
 365 : 
 366 : // definition
 367 : 
 368 : SEXP file176e918054efa(  ){
 369 :  return Rcpp::wrap("16a540c6086086816528e4524def24d9");
 370 : }
 371 : 
 372 : 
make cmd is
  make -f '/Library/Frameworks/R.framework/Resources/etc/Makeconf' -f '/Library/Frameworks/R.framework/Resources/share/make/shlib.mk' CXX='$(CXX14) $(CXX14STD)' CXXFLAGS='$(CXX14FLAGS)' CXXPICFLAGS='$(CXX14PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX14LDFLAGS)' SHLIB_LD='$(SHLIB_CXX14LD)' SHLIB='file176e918054efa.so' OBJECTS='file176e918054efa.o'

make would use
clang++ -mmacosx-version-min=10.13 -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/Rcpp/include/"  -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/"  -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/unsupported"  -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/BH/include" -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/src/"  -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/"  -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppParallel/include/"  -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DBOOST_NO_AUTO_PTR  -include '/Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp'  -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1   -I/usr/local/include   -fPIC  -Wall -g -O2  -c file176e918054efa.cpp -o file176e918054efa.o
if test  "zfile176e918054efa.o" != "z"; then \
	  echo clang++ -mmacosx-version-min=10.13 -std=gnu++14 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L"/Library/Frameworks/R.framework/Resources/lib" -L/usr/local/lib -o file176e918054efa.so file176e918054efa.o  '/Library/Frameworks/R.framework/Versions/4.0/Resources/library/rstan/lib//libStanServices.a' -L'/Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/lib/' -lStanHeaders -L'/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppParallel/lib/' -ltbb -ltbbmalloc -ltbbmalloc_proxy  -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation; \
	  clang++ -mmacosx-version-min=10.13 -std=gnu++14 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L"/Library/Frameworks/R.framework/Resources/lib" -L/usr/local/lib -o file176e918054efa.so file176e918054efa.o  '/Library/Frameworks/R.framework/Versions/4.0/Resources/library/rstan/lib//libStanServices.a' -L'/Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/lib/' -lStanHeaders -L'/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppParallel/lib/' -ltbb -ltbbmalloc -ltbbmalloc_proxy  -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation; \
	fi

Now notice the malloc warning at the top ( removed the sampling percentages here to make this fit into one post)

R(95977,0x105b2adc0) malloc: Attempted to register zone more than once: 0x1054d4110
curr.fun: symbol <-
 .. after ‘expression(mod <- stan_model(model_code = stancode, verbose = TRUE))’

>>>> eval(expression_nr. 3 )
		 =================

stn_md> fit <- sampling(mod, data = list(y_mean = 0))

SAMPLING FOR MODEL '16a540c6086086816528e4524def24d9' NOW (CHAIN 1).
Chain 1: 
Chain 1: Gradient evaluation took 1.4e-05 seconds
Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.14 seconds.
Chain 1: Adjust your expectations accordingly!
Chain 1: 
Chain 1: 
Chain 1: Iteration:    1 / 2000 [  0%]  (Warmup)
Chain 1: Iteration: 2000 / 2000 [100%]  (Sampling)
Chain 1: 
Chain 1:  Elapsed Time: 0.008523 seconds (Warm-up)
Chain 1:                0.00836 seconds (Sampling)
Chain 1:                0.016883 seconds (Total)
Chain 1: 

SAMPLING FOR MODEL '16a540c6086086816528e4524def24d9' NOW (CHAIN 2).
Chain 2: 
Chain 2: Gradient evaluation took 3e-06 seconds
Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0.03 seconds.
Chain 2: Adjust your expectations accordingly!
Chain 2: 
Chain 2: 
Chain 2: Iteration:    1 / 2000 [  0%]  (Warmup)
Chain 2: Iteration: 2000 / 2000 [100%]  (Sampling)
Chain 2: 
Chain 2:  Elapsed Time: 0.008036 seconds (Warm-up)
Chain 2:                0.007608 seconds (Sampling)
Chain 2:                0.015644 seconds (Total)
Chain 2: 

SAMPLING FOR MODEL '16a540c6086086816528e4524def24d9' NOW (CHAIN 3).
Chain 3: 
Chain 3: Gradient evaluation took 3e-06 seconds
Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0.03 seconds.
Chain 3: Adjust your expectations accordingly!
Chain 3: 
Chain 3: 
Chain 3: Iteration:    1 / 2000 [  0%]  (Warmup)
Chain 3: Iteration: 2000 / 2000 [100%]  (Sampling)
Chain 3: 
Chain 3:  Elapsed Time: 0.008259 seconds (Warm-up)
Chain 3:                0.007372 seconds (Sampling)
Chain 3:                0.015631 seconds (Total)
Chain 3: 

SAMPLING FOR MODEL '16a540c6086086816528e4524def24d9' NOW (CHAIN 4).
Chain 4: 
Chain 4: Gradient evaluation took 3e-06 seconds
Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0.03 seconds.
Chain 4: Adjust your expectations accordingly!
Chain 4: 
Chain 4: 
Chain 4: Iteration:    1 / 2000 [  0%]  (Warmup)
Chain 4: Iteration: 2000 / 2000 [100%]  (Sampling)
Chain 4: 
Chain 4:  Elapsed Time: 0.008249 seconds (Warm-up)
Chain 4:                0.008082 seconds (Sampling)
Chain 4:                0.016331 seconds (Total)
Chain 4: 
curr.fun: symbol <-
 .. after ‘expression(fit <- sampling(mod, data = list(y_mean = 0)))’

>>>> eval(expression_nr. 4 )
		 =================

And ends with this warning message about the path to my base library

stn_md> fit2 <- sampling(mod, data = list(y_mean = 5))

SAMPLING FOR MODEL '16a540c6086086816528e4524def24d9' NOW (CHAIN 1).
Chain 1: 
Chain 1: Gradient evaluation took 6e-06 seconds
Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.06 seconds.
Chain 1: Adjust your expectations accordingly!
Chain 1: 
Chain 1: 
Chain 1: Iteration:    1 / 2000 [  0%]  (Warmup)
Chain 1: Iteration: 2000 / 2000 [100%]  (Sampling)
Chain 1: 
Chain 1:  Elapsed Time: 0.008693 seconds (Warm-up)
Chain 1:                0.007724 seconds (Sampling)
Chain 1:                0.016417 seconds (Total)
Chain 1: 

SAMPLING FOR MODEL '16a540c6086086816528e4524def24d9' NOW (CHAIN 2).
Chain 2: 
Chain 2: Gradient evaluation took 3e-06 seconds
Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0.03 seconds.
Chain 2: Adjust your expectations accordingly!
Chain 2: 
Chain 2: 
Chain 2: Iteration:    1 / 2000 [  0%]  (Warmup)
Chain 2: Iteration: 2000 / 2000 [100%]  (Sampling)
Chain 2: 
Chain 2:  Elapsed Time: 0.008307 seconds (Warm-up)
Chain 2:                0.007694 seconds (Sampling)
Chain 2:                0.016001 seconds (Total)
Chain 2: 

SAMPLING FOR MODEL '16a540c6086086816528e4524def24d9' NOW (CHAIN 3).
Chain 3: 
Chain 3: Gradient evaluation took 1.1e-05 seconds
Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0.11 seconds.
Chain 3: Adjust your expectations accordingly!
Chain 3: 
Chain 3: 
Chain 3: Iteration:    1 / 2000 [  0%]  (Warmup)
Chain 3: Iteration: 2000 / 2000 [100%]  (Sampling)
Chain 3: 
Chain 3:  Elapsed Time: 0.009047 seconds (Warm-up)
Chain 3:                0.008054 seconds (Sampling)
Chain 3:                0.017101 seconds (Total)
Chain 3: 

SAMPLING FOR MODEL '16a540c6086086816528e4524def24d9' NOW (CHAIN 4).
Chain 4: 
Chain 4: Gradient evaluation took 4e-06 seconds
Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0.04 seconds.
Chain 4: Adjust your expectations accordingly!
Chain 4: 
Chain 4: 
Chain 4: Iteration:    1 / 2000 [  0%]  (Warmup)
Chain 4: Iteration: 2000 / 2000 [100%]  (Sampling)
Chain 4: 
Chain 4:  Elapsed Time: 0.00839 seconds (Warm-up)
Chain 4:                0.007618 seconds (Sampling)
Chain 4:                0.016008 seconds (Total)
Chain 4: 
curr.fun: symbol <-
 .. after ‘expression(fit2 <- sampling(mod, data = list(y_mean = 5)))’
Warning message:
In find.package(package, lib.loc, verbose = verbose) :
  package ‘base’ found more than once, using the first from
  “/Library/Frameworks/R.framework/Resources/library/base”,
  “/Library/Frameworks/R.framework/Versions/4.0/Resources/library/base”

And the session info

> sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.6

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_AU.UTF-8/en_AU.UTF-8/en_AU.UTF-8/C/en_AU.UTF-8/en_AU.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rstan_2.21.2         ggplot2_3.3.2        StanHeaders_2.21.0-6

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5         pillar_1.4.6       compiler_4.0.2     prettyunits_1.1.1  tools_4.0.2        pkgbuild_1.1.0     jsonlite_1.7.1     lifecycle_0.2.0    tibble_3.0.3       gtable_0.3.0       pkgconfig_2.0.3   
[12] rlang_0.4.7        cli_2.0.2          parallel_4.0.2     curl_4.3           loo_2.3.1          gridExtra_2.3      withr_2.2.0        dplyr_1.0.2        generics_0.0.2     vctrs_0.3.4        stats4_4.0.2      
[23] grid_4.0.2         tidyselect_1.1.0   glue_1.4.2         inline_0.3.16      R6_2.4.1           processx_3.4.4     fansi_0.4.1        callr_3.4.4        purrr_0.3.4        magrittr_1.5       codetools_0.2-16  
[34] scales_1.1.1       ps_1.3.4           ellipsis_0.3.1     matrixStats_0.56.0 assertthat_0.2.1   colorspace_1.4-1   V8_3.2.0           RcppParallel_5.0.2 munsell_0.5.0      crayon_1.3.4      

I have no idea why that happens. Do you have two installations of R?

Hi @bgoodri, no I do not. I actually wiped it all out from my Terminal (sudo rm -rf /Library/Frameworks/R.framework /Applications/R.app) before reinstalling R 4.0.2 again from scratch.

Earlier this week I attempted to teach a remote class to 17. About half including us instructors had this problem on windows. Constant aborts. Some fresh installs some upgrades from prior versions of R/RStan.

2 Likes

@bgoodri I assume that this problem is very difficult to work on because of the difficulties with reproducing the errors, so please let me know if there is anything you would like me to try or system information you think would be useful.

2 Likes