BRM function suddenly stopped running and throws a range of different errors

Hi forum,

I have already opened a similar thread here: Brms model doesn't run after custom family (hurdle_gamma()) specified, but haven’t received any replies yet and I am getting quite desperate.

I have been working with different brm models for a while now and never had any problems. Then all of a sudden (I believe this happened after I specified “family = hurdle_gamma()”), the code stopped running entirely and I got a number of different error messages (see above link for a selection).

Now I’ve tried to run an old model which has always run just fine:

HALO_bayes$m1 <-brm(formula = HALOAccuracies ~ HALOlg10JerksZ*HALOOwnStimDiff_jerkZ*HALOIntervention*HALOConditions + 
                      (1 + HALOlg10JerksZ*HALOOwnStimDiff_jerkZ*HALOConditions*HALOIntervention || HALOVidIDs) +
                      (1 + HALOlg10JerksZ*HALOOwnStimDiff_jerkZ*HALOConditions*HALOIntervention || HALOSubs), 
                    data = HALOdat, 
                    warmup = 1000, iter = 5000, 
                    options(mc.cores = parallel::detectCores()),
                    chains = 4, control = list(adapt_delta = .99), 
                    prior = prior1, sample_prior = TRUE,
                    save_all_pars = TRUE)

I didn’t change a thing in the code. Again, I now receive a bunch of random errors, the first one being:

Error: Argument 'family' is invalid.

Then, when I specify “family = gaussian()”, which should not be necessary as it is the default in brm, I receive the following error:

Error in formula.default(object, env = baseenv()) : invalid formula
In addition: Warning message:
Argument 'autocor' should be specified within the 'formula' argument. See ?brmsformula for help. 

I then check ?brmsformula and from what I can understand, ‘autocor’ does not need to be specified. I also can’t see anything wrong about my formula (especially since the model ran fine just a few days ago with this very same formula).

Any suggestions as to what is happening here would be massively appreciated. I am completely lost and at my limits with googling error messages and trying to find the root of the problem.

Hmm, if these problems came out of nowhere then I think we assume:

  1. Something is messed up with your R session
  2. Something is messed up with your brms install
  3. Something went wrong with your R install
  4. Something updated on your system and is conflicting with the brms/R install

To figure out if it’s just something with your Rsession, start a new one from the terminal (open a terminal, go to an empty folder somewhere, and type ‘R’) and try to run a basic brms model there. Just use the code here: https://github.com/paul-buerkner/brms#how-to-use-brms.

If none of that is working even in a separate Rsession, it might be something attached to your account on the computer. If you can switch to another account, do and try things there.

If nothing is still working, uninstall and reinstall brms and see if anything changes.

1 Like

(not trying to imply all those errors, just that’s the order)

What may be happening is that you set options(mc.cores = parallel::detectCores()) inside the brm() call without specifying the argument name, so it is interpreted as if you are assigning the family argument (or the autocor argument, in the second case).

Try with using cores=options(mc.cores = parallel::detectCores()) and see if that’s enough to move on from these errors.

2 Likes

Hi, thanks for your suggestions. I have just reinstalled everything anew, and unfortunately the problems persist. I followed this guide: https://ryanhomer.github.io/posts/build-openmp-macos-catalina-complete and got stuck when trying to build Fortran using this line of code:
> install.packages("bayesQR", type = "source")

which didn’t work for me. I should have maybe mentioned that I’m using Catalina, which seems to be problematic in many cases with rstan. I have also tried re-installing rstan from source, as recommended somewhere else, which again didn’t work.

That webpage says:

You can also test to make sure your configuration can build Fortran code against the OpenMP libraries by installing an appropriate R package, e.g.: bayesQR .

So I’m interpreting this as optional. I don’t think you need openmp to get brms running. If your R is installed this far, what happens when you try to install rstan or brms?

Hi, thanks for your input, you’re right that may have been the problem with the specific errors I described in this topic. Unfortunately, once I specify the argument name, I get a different error:

Error in withCallingHandlers(expr, warning = function(w) if (inherits(w,  : 
  'list' object cannot be coerced to type 'double'

If I just rerun the code, it first looks like Stan actually starts compiling and I get until the point of gradient evaluation. But then no sampling happens and I’m stuck here for eternity:

SAMPLING FOR MODEL '087c63bef2da3a96cdd4d3ddfe6bd491' NOW (CHAIN 4).
Chain 1: 
Chain 1: Gradient evaluation took 0.024026 seconds
Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 240.26 seconds.
Chain 1: Adjust your expectations accordingly!
Chain 1: 
Chain 1: 
Chain 2: 
Chain 2: Gradient evaluation took 0.023271 seconds
Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 232.71 seconds.
Chain 2: Adjust your expectations accordingly!
Chain 2: 
Chain 2: 
Chain 1: Iteration:    1 / 5000 [  0%]  (Warmup)
Chain 3: 
Chain 3: Gradient evaluation took 0.022433 seconds
Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 224.33 seconds.
Chain 3: Adjust your expectations accordingly!
Chain 3: 
Chain 3: 
Chain 2: Iteration:    1 / 5000 [  0%]  (Warmup)
Chain 3: Iteration:    1 / 5000 [  0%]  (Warmup)
Chain 4: 
Chain 4: Gradient evaluation took 0.022109 seconds
Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 221.09 seconds.
Chain 4: Adjust your expectations accordingly!
Chain 4: 
Chain 4: 
Chain 4: Iteration:    1 / 5000 [  0%]  (Warmup)

I’m really at my wits’ end with this issue…

brms seems to install fine. When I try to installI rstan I get a lot of warnings, among these the most frequent ones are:

In file included from sparse_extractors.cpp:2:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/csr_extract_u.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/meta.hpp:9:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/mat/meta/append_return_type.hpp:4:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:14:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/Sparse:33:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/IterativeLinearSolvers:46:
/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/src/Core/util/ReenableStupidWarnings.h:10:30: warning: pragma diagnostic pop could not pop, no matching push [-Wunknown-pragmas]
    #pragma clang diagnostic pop

… and

/Library/Frameworks/R.framework/Versions/4.0/Resources/library/BH/include/boost/random/linear_congruential.hpp:140:20: warning: overlapping comparisons always evaluate to false [-Wtautological-overlap-compare]
        if(_x <= 0 && _x != 0) {
           ~~~~~~~~^~~~~~~~~~
/Library/Frameworks/R.framework/Versions/4.0/Resources/library/BH/include/boost/random/additive_combine.hpp:82:16: note: in instantiation of member function 'boost::random::linear_congruential_engine<unsigned int, 40014, 0, 2147483563>::seed' requested here
        _mlcg1.seed(seed_arg);

and also returns this like 100 times:

/usr/local/opt/llvm/bin/clang++ -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"../inst/include" -I"../inst/include/boost_not_in_BH" -I"." -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -DBOOST_NO_AUTO_PTR -D_REENTRANT -DSTAN_THREADS -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/BH/include' -I'/Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include' -I'/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppParallel/include' -isystem "/usr/local/opt/llvm/include" -isysroot "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk" -fPIC -Wall -g -O2 -c stanc.cpp -o stanc.o

Does the example brms model here run: https://github.com/paul-buerkner/brms#how-to-use-brms ?

yes that one runs without a problem…

Okay then you probably want:

cores = parallel::detectCores()

directly (no need for the options thing).

So the whole command:

HALO_bayes$m1 <-brm(formula = HALOAccuracies ~ HALOlg10JerksZ*HALOOwnStimDiff_jerkZ*HALOIntervention*HALOConditions + 
                      (1 + HALOlg10JerksZ*HALOOwnStimDiff_jerkZ*HALOConditions*HALOIntervention || HALOVidIDs) +
                      (1 + HALOlg10JerksZ*HALOOwnStimDiff_jerkZ*HALOConditions*HALOIntervention || HALOSubs), 
                    data = HALOdat, 
                    warmup = 1000, iter = 5000, 
                    cores = parallel::detectCores(),
                    chains = 4, control = list(adapt_delta = .99), 
                    prior = prior1, sample_prior = TRUE,
                    save_all_pars = TRUE)

Given that “10 leapfrog steps per transition would take …” over 200 seconds, I would expect the whole model to take a good few hours to fit. Do you just need to let it run for longer? Is there a reason for adapt_delta = 0.99, a smaller value like 0.8 and 0.9 would likely speed up the fitting?

2 Likes

Thanks, unfortunately that doesn’t change anything… It still looks like it is about to start sampling but doesn’t ever start.

Try adding refresh=1 to the argument list, in order to print a message at each iteration.

How much data are you using? Presumably it’s quite large: if so, try using a smaller subset while you are developing the model.

Given that “10 leapfrog steps per transition would take …” over 200 seconds, I would expect the whole model to take a good few hours to fit.

It does take around 4-5 hours, because it’s quite a hefty model. I have set adapt_delta this high because I previously had convergence problems. The problem is if after 2-3 hours the model doesn’t converge and I have to start again, I lose more time than running it with these settings for 4-5 hours.

It actually does run now! It only seems to take much longer to start up, much longer than it used to take.

I tried a very similar model with all your previous suggestions for changing model specifications, on a different laptop, and here the model still doesn’t run. This is why I think my problem is MAC OS-Catalina-Stan related, as on the other computer it only started running after I updated to the newest Catalina and reinstalled R & all packages.

It does print out messages after every 400 or so iterations. Only it takes a looong time to even start sampling and printing out those messages.

I actually don’t have that much data for such a complicated model, which is why I’m afraid that splitting up the dataset would again lead to convergence problems.

Are all terms implied by the interaction lg10JerksZ*allConditions*lg10OwnStimDiff_jerkZ definitely within – rather than between – subject (which is what I assume HALOSubs refers to), and within – rather than between – “items” (which is what I assume HALOVidIDs) refers to? If you have included between-subject or between-item predictors as group-level effects they are not informed by the data and will slow down the model fit and may result in divergences.

Are all terms implied by the interaction lg10JerksZ*allConditions*lg10OwnStimDiff_jerkZ definitely within – rather than between – subject (which is what I assume HALOSubs refers to), and within – rather than between – “items” (which is what I assume HALOVidIDs ) refers to? If you have included between-subject or between-item predictors as group-level effects they are not informed by the data and will slow down the model fit and may result in divergences.

I’m not quite sure I understand - aren’t mixed models appropriate to model both within- and between subjects population level effects? My data comes from a mixed factorial design where I compare “lg10JerksZ” and “g10OwnStimDiff_jerkZ” both within subjects (i.e. across “allConditions”) and between subjects.

So, while I managed to get the brm model running on one dataset, I still struggle with a different dataset. The model specifications are exactly the same, and the data structures are exactly the same (it’s basically a replication study). Again, I get this message:

Compiling Stan program...
make cmd is
  make -f '/Library/Frameworks/R.framework/Resources/etc/Makeconf' -f '/Library/Frameworks/R.framework/Resources/share/make/shlib.mk' -f '/Users/bianca/.R/Makevars' CXX='$(CXX14) $(CXX14STD)' CXXFLAGS='$(CXX14FLAGS)' CXXPICFLAGS='$(CXX14PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX14LDFLAGS)' SHLIB_LD='$(SHLIB_CXX14LD)' SHLIB='file26126d4a1cd.so' OBJECTS='file26126d4a1cd.o'

make would use
/usr/local/opt/llvm/bin/clang++ -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   -isystem "/usr/local/opt/llvm/include" -isysroot "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk"   -fPIC  -Wall -g -O2  -c file26126d4a1cd.cpp -o file26126d4a1cd.o
if test  "zfile26126d4a1cd.o" != "z"; then \
	  echo /usr/local/opt/llvm/bin/clang++ -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/opt/llvm/lib" -L"/usr/local/Cellar/gettext/0.21/lib" --sysroot="/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk" -o file26126d4a1cd.so file26126d4a1cd.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; \
	  /usr/local/opt/llvm/bin/clang++ -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/opt/llvm/lib" -L"/usr/local/Cellar/gettext/0.21/lib" --sysroot="/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk" -o file26126d4a1cd.so file26126d4a1cd.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

Can anybody tell me what this means? Could this be a path problem or something to do with the Makevars file? Once again I’m clueless…