R session aborted in summary() function

I need to fit 500 datasets to a specific model using brms package. Here are the codes.

for (n in 1:500) {
  data_pathname <- paste0(workpath2,n,".dat")
  data <- read.table(data_pathname)

  model <- brm(bf(Y~1+X+(1+X|c|group),sigma~1+(1|c|group)),
               data,inits=0,cores=4,sample_prior=T)
  
  result <- summary(model)
}

It works well sometimes. But more often R session aborted. The same thing happened both on Rstudio and RGui. The RAM usage of my server keeps lower than 15% when R running. I find the error happened when the summary() function was called.
How can I solve? Appreciate for any reply.

One thing you can try is to add a print(n) or print(data_pathname) and see if your loop always fails for a specific dataset, or if it's random. If it's always the same dataset, you can run it separately and explore the content of model etc.

Also wondering, is it on purpose that you are overwriting result in each iteration of the loop? As written, at the end of running the entire loop you'll have the exact same result as only running for n=500.

Appreciated. I just add a print(n)beforeresult <- summary(model) according to your advice. It didn't help, and now R session aborted before printed n. So now I realize that the error may happened after model <- brm() but not exactly a fault of summary().
It's not always the same dataset. For example, when R aborted, I restart the session and reset 1:500 in for() to 4:500 etc. After few cycles when it absorted again, I restart and reset to 6:500 .
In addition, you are right to find it same as only running for n=500 . Indeed, some parameters of result are assigned to a matrix output as followed.

if(all(result$fixed[,5]<1.1) && all(result$random$group[,5]<1.1))
  {
    output[n,1] <- result$fixed[1,1] #gamma00
    output[n,2] <- result$fixed[1,2] #gamma00_se
    ......
   } else {
    output[n,40] <- "unconverged"
  }
write.table(output,output_name,F,T)

The sentences after result <- summary(model) are omitted in my description for simplified purposes. Sorry for causing confusion.

1 Like

R session aborting, is typically a more 'involved' issue, than this forum is capable of directly addressing, this is because it doesn't typically relate to poorly written code, or use of code or anything else, but on the quality of the compiled code being relied on, and possibly its interaction with a given target OS.
In such circumstances, the R version, and OS platform you are using, as well as the specific versions of the packages you are using become relevant.
You can use sessionInfo() to gather this info in an easy copy and pasteable form.
You can share your issue with the maintainers of the affected package.
In this case the best venue is Issues · paul-buerkner/brms · GitHub
Here is an example of a similar issue, that was reported and resolved.
"R session aborted" when using update() on m1 mac · Issue #1236 · paul-buerkner/brms (github.com)

Good luck !

1 Like

Thanks for apply. A good news is that I have found the reason. It's exactly a compiling bug of rstan and brms .
More discussions can be found on [here](Windows R session crashes when assigning multiple runs to same output · Issue #844 · stan-dev/rstan (github.com)).

I solve it by using cmdstanr instead of rstan . Details about cmdstanr are here.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.