Loo(x, moment_match=TRUE) causes R to crash

Hi everyone, I would like to perform model comparison between 5 Hierarchical GAMMs using brms() and as part of it, I need to perform Leave One Out Cross Validation, which is performed by the loo() function in brms.

Below is all my code leading to my RStudio console crashing… I read that running the models and loo() in the R terminal while specifying only 1 core, tended to resolve the issue, but that has not worked for me either. I really need to be able to perform this analysis as it’s part of my dissertation.

Here is the code:

rm(list=ls(all=TRUE))
library(mgcv)
library(brms)
library(readxl)
library(cmdstanr)
library(bayesplot)
library(ggplot2)
library(marginaleffects)
library(tidyverse)
library(tidybayes)
library(loo)
library(performance)

# Load data
data <- read_excel("~/data.xlsx", sheet = "data1")

# Specify categorical variables as factors
data$ind <- as.factor(data$ind)
data$town <- as.factor(data$town)

fit.G <- brm(bf(prop.fruit ~ s(time.index, m=2, bs="tp") + 
                   (1|ind) +
                   (1|town),
                 zi ~ s(time.index) +
                   (1|ind) +
                   (1|town)),
              family = zero_inflated_beta(link = "logit"),
              prior = NULL,
              data=data,
              chains=4, 
              iter=8000, 
              warmup=4000, 
              cores=4, 
              seed=1234, 
              backend="cmdstanr")

loo.G <- loo(fit.G, moment_match = TRUE)

After running this code, my RStudio experiences a fatal error and crashes (see image below). Please, I would really appreciate any recommendations, as I feel really close to finishing this analysis and only have this as the last obstacle in the way.

1 Like

Did you try to add save_pars = save_pars(all = TRUE) to your model formula? Brms manual states that loo_moment_match cannot be computed if that parametr is set to FALSE.

2 Likes

Your suggestion fixed the issue, thank you so much Hanz!

3 Likes