RStudio crashes working with brms

I’m dipping my toes into the brms package, currently working with ordinal regression. Unfortunately, I’m experiencing a lot of crashes. Here is a screen dump from the latest one:


This occurred when applying loo(fitt2), where fitt2 is the fitted model as conveniently shown in in the screen dump after crashing, but previously the same occurred just after fitt2 was fitted. Not sure if it’s related, but I consistently get the following warning message after fitting:

Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  'C:/rtools40/usr/mingw_/bin/g++' not found

Info about the data: The data is questionnaire data with ordered responses (5 levels) regarding farmers’ perceptions of innovation needs in response to wildlife damage. 222 farmers (ID) were asked 6 questions (Q), and the data frame DF contains 1332 observations. These first two analyses are merely trying to rank the responses to the questions by their coefficients, with (fitt2) and without (fitt) accounting for farmer ID random effects.

Any ideas what’s going on?

So the crash doesn’t occur during sampling, just when you call loo on the end result?

Does the crash occur when you don’t use multiple cores? (i.e., loo(fitt2, cores=1))

That warning message is safe to ignore

The crash has not occurred during sampling. It has occurred just as the chains are done (couldn’t capture the exact moment). Other times that works just fine. The crash only occasionally crashes when running loo.

How much RAM does your computer have and how much gets used during sampling and the loo() call? Since running out of RAM can cause R to crash

The computer has 16 GB RAM. A lot seems to be used by other things are running in the background, but there is at least 4 GB available when sampling and 5GB when looing. But I guess it’s very possible something else was running in the background that used up a lot of memory. Either way, I can no longer get it to crash whatever I do. Which is a good thing. Will come crying again if the problem reoccurs.

You’re not alone. I had the same idiosyncratic crashing with various versions of RStudio and numerous attempts to clean install. Usuallly was associated with a predict, fitted, or pp_check call once the model had finished. I suspect its some interaction between RStan and RStudio, but never was able to get diagnostic info. My “solution” was to revert to R 3.6.3 and RStan 2.19, and its working great.

Similarly with me. When using package brms, at apparently random points during post-processing of the fitted model – whether its pairs() plots, pp_check()'s or other access of the posterior sample – I get the old-timey-bomb “R Session Aborted” message in RStudio. I’m running R v.4.0.2, brms v.2.13.5, rstan v.2.21.2. The upshot is that now brms is unusable. Is the only solution to revert to earlier R v.3.x and earlier versions of brms & rstan?

This is related to the issue https://github.com/stan-dev/rstan/issues/844. See also

I had/have the same problem. It crashed when I fitted several (complex) models in a row. I found the following workaround which reduced the crashing signficantly. Instead of using the function brm directly, you use a combination of make_standata, sampling and then fit an empty brm_model via brm.

sdata <- make_standata(brmsformula, data = brmsdata)
stanfit <- sampling(stanmod, data = sdata, warmup=warmup, iter=iter, chains=chains, thin=thin)
brms_fit <- brm(brmsformula, prior = priors, data = brmsdata, empty = TRUE)
brms_fit$fit <- stanfit
brms_fit <- rename_pars(brms_fit)

This method is described in the manual on page 27 (https://cran.r-project.org/web/packages/brms/brms.pdf)

If it crashes when running loo, you can set the argument pointwise to TRUE e.g.

loo(fit1, fit2, pointwise = TRUE)

This will be slower, but much preferred to having R crash. I’ve had to do this many times when comparing large model fits.